-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHomophic_Cipher.py
More file actions
31 lines (27 loc) · 909 Bytes
/
Homophic_Cipher.py
File metadata and controls
31 lines (27 loc) · 909 Bytes
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
key_table = {'A':'D9', 'B':'X', 'C':'S', 'D':'F', 'E':'Z721', 'F':'E', 'G':'H', 'H':'C', 'I':'V3',
'J':'I', 'K':'T', 'L':'P', 'M':'G', 'N':'A5', 'O':'Q0', 'P':'L', 'Q':'K', 'R':'J',
'S':'R4', 'T':'6U', 'U':'O', 'V':'W', 'W':'M', 'X':'Y', 'Y':'B', 'Z':'N'}
import random
s = input()
def encrypt(s):
st = ""
for item in s:
if item.upper() in key_table:
st += random.choice(key_table[item.upper()])
else:
st += item
return st
def getkey(val):
for k, v in key_table.items():
if val in v:
return k
def decrypt(s):
words = s.split()
res = ""
for word in words:
for letter in word:
res += getkey(letter)
res += " "
return res
print("Encryption of message:{} => {}".format(s, encrypt(s)))
print("Decryption of message:{} => {}".format(encrypt(s), decrypt(encrypt(s))))