forked from kal179/Beginners-Python-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypting_caesars_cipher.py
44 lines (35 loc) · 1.34 KB
/
decrypting_caesars_cipher.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
import string
# Note: this decryption function is designed to decrypt messages encrypted by encryption function i wrote(avaliable in this repo)
def concat_elements(n):
res = ""
for i in n:
res += i
return res
def decrypt(message, key):
string_chars = list(string.ascii_uppercase) + list(string.ascii_lowercase) + list(string.digits) + list(string.punctuation) + [" "]
try:
splitted_message = list(message)
except TypeError:
return "Expected an string for text!"
for char in splitted_message:
try:
tmp = string_chars[string_chars.index(char) - key]
except IndexError:
tmp_key = (string_chars.index(char) + key) + len(string_chars)
tmp = string_chars[tmp_key]
splitted_message[splitted_message.index(char)] = tmp
final = concat_elements(splitted_message)
return final
def decrypt_generator(message, n):
# range(0, 96) because len(string_chars) == 95
for i in range(0, n + 1):
case = decrypt(message, i)
yield case
# Test
test_case = "lq01Ir1I2xyI1ncrn2*"
result = decrypt(test_case, 9)
print("Decrypted Text: " + result, "\n")
_result = list(decrypt_generator(test_case, 95))
for res in _result:
print("Possible text: " + res)
# Look at ninth result