forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair_cipher.py
137 lines (118 loc) · 3.54 KB
/
playfair_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
msg, new_msg = "", ""
a = [[0 for i in range(5)]for i in range(5)]
def input_f(text, key):
inp = []
lst_inp = key.split()
for item in lst_inp:
inp += item
a = key_table(inp)
c_msg = text
msg = ""
for i in c_msg:
msg += i
new_msg = "".join([i for i in c_msg])
if 'j' in msg:
msg = msg.replace('j', 'i')
if 'J' in msg:
msg = msg.replace('J', 'I')
chunks = converted_chunks(msg)
return chunks, a
def key_table(inp):
key = list(dict.fromkeys(inp))
k = 0
x = 65
a = [[0 for i in range(5)]for i in range(5)]
for i in range(5):
for j in range(5):
if k < len(key):
a[i][j] = key[k].upper()
k += 1
elif not any(chr(x) in y for y in a) and chr(x) != 'J':
a[i][j] = chr(x)
x += 1
else:
while any(chr(x) in y for y in a) or chr(x) == 'J':
x += 1
a[i][j] = chr(x)
return a
def converted_chunks(msg):
msg_list = []
for item in msg:
msg_list += item
chunks = []
i = 0
while i < len(msg_list):
if i == len(msg_list)-1:
chunks.append([msg_list[i].upper(), 'X'])
elif msg_list[i] != msg_list[i + 1]:
chunks.append([msg_list[i].upper(), msg_list[i+1].upper()])
i += 1
else:
chunks.append([msg_list[i].upper(), 'X'])
i += 1
return chunks
def find_coord(c, a):
for i in range(5):
for j in range(5):
if a[i][j] == c:
return i, j
def check_num_spl(item):
return item[0].isalpha() and item[1].isalpha()
def encrypt(txt, key):
spl = ['!','@','#','$','%','^','&','*','(',')','<','~',':',';','<','>','?','/']
for i in key:
if i in spl:
key = key.replace(i,"")
if key == "":
key = "ABCD"
chunks, a = input_f(txt, key)
enc_text = ""
for item in chunks:
if not check_num_spl(item):
enc_text += item[0] + item[1]
else:
st = ""
x1, y1 = find_coord(item[0], a)
x2, y2 = find_coord(item[1], a)
if y1 == y2:
enc_text += a[(x1+1)%5][y1] + a[(x2+1)%5][y1]
elif x1 == x2:
enc_text += a[x1][(y1+1)%5] + a[x2][(y2+1)%5]
else:
st += a[x2][y1] + a[x1][y2]
enc_text += st[::-1]
return enc_text
def decrypt(txt, key):
spl = ['!','@','#','$','%','^','&','*','(',')','<','~',':',';','<','>','?','/']
for i in key:
if i in spl:
key = key.replace(i,"")
if key == "":
key = "ABCD"
chunks, a = input_f(txt, key)
dec_text = ""
for item in chunks:
if not check_num_spl(item):
dec_text += item[0] + item[1]
else:
st = ""
x1, y1 = find_coord(item[0], a)
x2, y2 = find_coord(item[1], a)
if y1 == y2:
dec_text += a[x1-1][y1] + a[x2-1][y1]
elif x1 == x2:
dec_text += a[x1][y1-1] + a[x2][y2-1]
else:
dec_text += a[x1][y2] + a[x2][y1]
if 'X' in dec_text:
dec_text = dec_text.replace('X', '')
return dec_text
def main():
text = input("Enter your text:").lower()
key = input("Enter your key:").lower()
encrypted = encrypt(text, key)
decrypted = decrypt(encrypted, key)
print(f"Encrypted Message: {encrypted}")
print(f"Decrypted Message: {decrypted}")
if __name__ == "__main__":
main()