-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair_alphanumeric.py
86 lines (68 loc) · 2.52 KB
/
playfair_alphanumeric.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
import string
def generate_playfair_matrix(key):
chars = string.ascii_uppercase + string.digits
matrix = []
used = set()
key = ''.join(filter(str.isalnum, key)).upper()
for char in key:
if char not in used and char in chars:
used.add(char)
matrix.append(char)
for char in chars:
if char not in used:
used.add(char)
matrix.append(char)
return [matrix[i:i+6] for i in range(0, 36, 6)]
def find_position(matrix, char):
for r, row in enumerate(matrix):
if char in row:
return r, row.index(char)
return None
def preprocess_text(text):
text = ''.join(filter(str.isalnum, text)).upper()
processed = []
i = 0
while i < len(text):
processed.append(text[i])
if i + 1 < len(text) and text[i] == text[i + 1]:
processed.append('X')
i += 1
if len(processed) % 2 != 0:
processed.append('X')
return [processed[i:i+2] for i in range(0, len(processed), 2)]
def encrypt_digraph(matrix, digraph):
r1, c1 = find_position(matrix, digraph[0])
r2, c2 = find_position(matrix, digraph[1])
if r1 == r2:
return matrix[r1][(c1 + 1) % 6] + matrix[r2][(c2 + 1) % 6]
elif c1 == c2:
return matrix[(r1 + 1) % 6][c1] + matrix[(r2 + 1) % 6][c2]
else:
return matrix[r1][c2] + matrix[r2][c1]
def decrypt_digraph(matrix, digraph):
r1, c1 = find_position(matrix, digraph[0])
r2, c2 = find_position(matrix, digraph[1])
if r1 == r2:
return matrix[r1][(c1 - 1) % 6] + matrix[r2][(c2 - 1) % 6]
elif c1 == c2:
return matrix[(r1 - 1) % 6][c1] + matrix[(r2 - 1) % 6][c2]
else:
return matrix[r1][c2] + matrix[r2][c1]
def playfair_encrypt(key, text):
matrix = generate_playfair_matrix(key)
digraphs = preprocess_text(text)
encrypted_text = ''.join(encrypt_digraph(matrix, dg) for dg in digraphs)
return encrypted_text
def playfair_decrypt(key, text):
matrix = generate_playfair_matrix(key)
digraphs = [text[i:i+2] for i in range(0, len(text), 2)]
decrypted_text = ''.join(decrypt_digraph(matrix, dg) for dg in digraphs)
if decrypted_text.endswith('X'):
decrypted_text = decrypted_text[:-1]
return decrypted_text
key = input("Enter your key: ")
text = input("Enter your text: ")
encrypted_text = playfair_encrypt(key, text)
print("Encrypted Text:", encrypted_text)
decrypted_text = playfair_decrypt(key, encrypted_text)
print("Decrypted Text:", decrypted_text.lower())