-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeCipher.py
39 lines (35 loc) · 1.23 KB
/
DeCipher.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
import enchant
dictionary = enchant.Dict("en_US")
def deciph(cipher_text):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
result = ""
# splitted phrases
itr=0
splitted_cipher = cipher_text.split(' ')
for l in range(len(splitted_cipher)):
key = 0
decipher = ""
while itr<26:
splitted_cipher_character = list(splitted_cipher[l])
for i in range(len(splitted_cipher_character)):
pos = alphabet.find(splitted_cipher_character[i])
newpos = (pos + key) % 26
newchar = alphabet[newpos]
splitted_cipher_character[i] = newchar
# print(splitted_cipher_character)
decipher = ''.join(map(str, splitted_cipher_character))
if dictionary.check(decipher):
# print(True)
# print(decipher)
# print("key: +" + str(key))
result += " " + decipher
break
key += 1
itr+=1
return result,key
def main():
while True:
cipher_message = input("Enter the sceret code:")
decipher_message,ans_key =deciph(cipher_message)
print("Decipher:"+decipher_message+"\nKey:"+ str(ans_key))
main()