-
Notifications
You must be signed in to change notification settings - Fork 0
/
playfair.py
91 lines (72 loc) · 2.59 KB
/
playfair.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
def generate_key_table(key):
key_table = []
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
for char in key:
if char not in key_table:
key_table.append(char.upper())
for char in alphabet:
if char not in key_table:
key_table.append(char)
return [key_table[i:i+5] for i in range(0, len(key_table), 5)]
def find_position(key_table, char):
for i in range(5):
for j in range(5):
if key_table[i][j] == char:
return i, j
return -1, -1
def encrypt_pair(key_table, a, b):
row1, col1 = find_position(key_table, a)
row2, col2 = find_position(key_table, b)
if row1 == row2:
return key_table[row1][(col1 + 1) % 5] + key_table[row2][(col2 + 1) % 5]
elif col1 == col2:
return key_table[(row1 + 1) % 5][col1] + key_table[(row2 + 1) % 5][col2]
else:
return key_table[row1][col2] + key_table[row2][col1]
def decrypt_pair(key_table, a, b):
row1, col1 = find_position(key_table, a)
row2, col2 = find_position(key_table, b)
if row1 == row2:
return key_table[row1][(col1 - 1) % 5] + key_table[row2][(col2 - 1) % 5]
elif col1 == col2:
return key_table[(row1 - 1) % 5][col1] + key_table[(row2 - 1) % 5][col2]
else:
return key_table[row1][col2] + key_table[row2][col1]
def encrypt_playfair(text, key):
key_table = generate_key_table(key)
text = text.upper().replace('J', 'I')
i = 0
pairs = []
while i < len(text):
a = text[i]
b = text[i+1] if i+1 < len(text) else 'X'
if a == b:
b = 'X'
i -= 1
pairs.append(a+b)
i += 2
ciphertext = ''
for a, b in pairs:
ciphertext += encrypt_pair(key_table, a, b)
return ciphertext
def decrypt_playfair(ciphertext, key):
key_table = generate_key_table(key)
ciphertext = ciphertext.upper().replace('J', 'I')
i = 0
pairs = []
while i < len(ciphertext):
a = ciphertext[i]
b = ciphertext[i+1] if i+1 < len(ciphertext) else 'X'
pairs.append(a+b)
i += 2
plaintext = ''
for a, b in pairs:
plaintext += decrypt_pair(key_table, a, b)
return plaintext
if __name__ == "__main__":
text = "HELLO"
key = "KEYWORD"
encrypted_text = encrypt_playfair(text, key)
print(f"Encrypted text: {encrypted_text}")
decrypted_text = decrypt_playfair(encrypted_text, key)
print(f"Decrypted text: {decrypted_text}")