-
Notifications
You must be signed in to change notification settings - Fork 0
/
decryption.py
56 lines (40 loc) · 2.27 KB
/
decryption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Imports
from rich.console import Console
from string import ascii_lowercase, ascii_uppercase
from errors import *
import time, getpass, strings, os
# Setup environment
user = getpass.getuser()
console = Console()
# Base :class:`Decryption`
class Decryption:
"""Generates decrypted string form a given encrypted string input which is encrypted from :file:`./encryption.py`"""
def __init__(self, word):
self.word = word
self.newWordList = []
def decrypt(self):
"""
Decrypts the input string and returns the original string.
The process is the exact opposite of :class:`Encryption`.
Raises:
NotShuffledError: Raised when string is not shuffled.
"""
for l in self.word:
if l not in strings.canNotDecrease and l not in strings.assigned.values() and l not in strings.reversedAssigned.values():
self.newWordList.append(ascii_lowercase[ascii_lowercase.index(l) - 3]) if not l.isupper() else self.newWordList.append(ascii_uppercase[ascii_uppercase.index(l) - 3])
if l in strings.canNotDecrease: self.newWordList.append(strings.canNotDecreaseDict[str(l)])
if l in strings.assigned.values(): self.newWordList.append(strings.reversedAssigned[str(l)])
if l in strings.reversedAssigned.values(): self.newWordList(strings.assigned(str(l)))
# Checking if reserved.txt exists
if not os.path.exists('reserved.txt'):
with open('reserved.txt', 'w') as f: f.write('')
# Checking for non shuffled words but provided to decrypt
with open('reserved.txt', 'r') as f:
# try-except clause to not break out of the loop
try:
# Custom errors \( ̄︶ ̄*\))
if self.word in f.read(): raise NotShuffledError("String not shuffled, please provide a valid string which is shuffled from ./encryption.py. The below decrypted string is not valid.\n")
# Printing the error to not break out of the loop
except NotShuffledError as notShuffled: print(notShuffled)
print(''.join(self.newWordList))
self.newWordList.clear()