Dear community,
I’ve created a code to Encrypt/Decrypt lists of words using a custom dictionary. Although the Encrypt function work as expected, the Decrypt option is causing me some problems, I’ll explain why.
The entire code:
#Dictionary
crypt = {
"a": "100",
"b": "101",
"c": "102",
"d": "103",
"e": "104",
"f": "105",
"g": "106",
"h": "107",
"i": "108",
"j": "109",
"k": "110",
"l": "111",
"m": "112",
"n": "113",
"o": "114",
"p": "115",
"q": "116",
"r": "117",
"s": "118",
"t": "119",
"u": "120",
"v": "121",
"w": "122",
"x": "123",
"y": "124",
"z": "125",
}
#Encrypt Function
def encrypt():
encrypted = ''
words = input('Enter text to encrypt:\n').lower()
for char in words:
if char in crypt:
encrypted += crypt[char]
else:
encrypted += char
print(encrypted)
#Decrypt Function
def decrypt():
decrypted = ''
code = input('Enter the code:\n')
inv_crypt = {v: k for k, v in crypt.items()}
for n1, n2, n3 in zip(code[::3], code[1::3], code[2::3]):
splitted = (n1+n2+n3)
if splitted in inv_crypt:
decrypted += inv_crypt[splitted]
else:
decrypted += splitted
print(decrypted)
#Ask user for encrypt or decrypt
while True:
print('Select an option\n', '1 to Encrypt\n', '2 to Decrypt\n', '3 to Exit\n')
option = input()
if option == '1':
encrypt()
break
elif option == '2':
decrypt()
break
elif option == '3':
break
else:
print('No correct option selected\n')
As I mentioned, the Encrypt function works properly. Running the code in the terminal would look like this:
Select an option
1 to Encrypt
2 to Decrypt
3 to Exit
1
Enter text to encrypt:
dog, cat, horse
The output at the end will be
103114106, 102100119, 107114117118104
Which correspond to the words dog, cat, horse encrypted with the custom dictionary. So far, so good.
The problem comes with the Decrypt function.
As you can notice, all the letters has an equivalent of three digit number, therefore it’s necessary to divide the numbers in groups of three digits. Additionally I inverted the dictionary to convert the keys in values and the values in keys, taking into account that for decryption the groups of three digits will be the necessary keys we will use to find the equivalent letter (value now) in the dictionary.
For Decrypt, running the code in the terminal would look like this:
Select an option
1 to Encrypt
2 to Decrypt
3 to Exit
2
Enter the code:
103114106, 102100119, 107114117118104
The output at the end will be
dog, 102100119, 10711411711810
As you can see, the word dog is decrypted but not the words cat and horse. Additionally the last digit (4) is lost.
I think the problem lies in the function where I divide the numbers into groups of three digits, the code that perform this function is
for n1, n2, n3 in zip(code[::3], code[1::3], code[2::3]):
splitted = (n1+n2+n3)
If we print(splitted) the result is
103
114
106
, 1
021
001
19,
10
711
411
711
810
Which is an error, because the output should look something like this:
103
114
106
,102
100
119
,107
114
117
118
104
But I haven’t managed to organize the information in this way.
May someone give me a hand?
Thank you in advance.