forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair-cipher.py
141 lines (114 loc) · 3.4 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
138
139
140
141
# Following is the implementation of Playfair Cipher Encryption Algorithm in Cryptography
# Inputs required from user: Key and Plaintext, both strings
# Example:
# Enter the key: phoebe
# Enter plain text: friends
# Key Matrix generated is:
# p h o e b
# a c d f g
# i k l m n
# q r s t u
# v w x y z
# Cipher text: ctmplgux
keyT = [[0] * 5 for i in range(5)]
# Convert (break) a string into individual letters in a list
def Convert(string):
list1 = []
list1[:0] = string
return list1
# Convert all input string characters into lowercase
def ToLowerCase(plain, ps):
for i in range(ps):
if ord(plain[i]) > 64 and ord(plain[i]) < 91:
plain[i] = chr(ord(plain[i]) + 32)
# Remove spaces if any in the input string
def RemoveSpaces(plain, ps):
count = 0
for i in range(ps):
if plain[i] != " ":
plain[count] = plain[i]
count = count + 1
return count
# Given a key, generate the Key Table
def GenerateKeyTable(key, ks):
chars = [0] * 26
for i in range(len(key)):
if key[i] != "j":
chars[ord(key[i]) - 97] = 2
chars[ord("j") - 97] = 1
i = 0
j = 0
for k in range(len(key)):
if chars[ord(key[k]) - 97] == 2:
chars[ord(key[k]) - 97] -= 1
keyT[i][j] = key[k]
j += 1
if j == 5:
i += 1
j = 0
for k in range(26):
if chars[k] == 0:
keyT[i][j] = chr(k + 97)
j += 1
if j == 5:
i += 1
j = 0
print("Key Matrix generated is:")
for i in range(5):
for j in range(5):
print(keyT[i][j], end="\t")
print()
# Function to search 2 characters at a time in the Key Table and return the positions in an array
def Search(a, b, arr):
if a == "j":
a = "i"
elif b == "j":
b = "i"
for i in range(5):
for j in range(5):
if keyT[i][j] == a:
arr[0] = i
arr[1] = j
elif keyT[i][j] == b:
arr[2] = i
arr[3] = j
def Mod5(a):
return a % 5
# Adding 'z' at the end if odd number of characters
def Prepare(stri, ptrs):
if ptrs % 2 != 0:
stri.append("z")
ptrs = ptrs + 1
return ptrs
# Generating the encrypted characters for each plaintext character
def Encrypt(str, ps):
a = [0] * 4
i = 0
for i in range(0, ps, 2):
Search(str[i], str[i + 1], a)
if a[0] == a[2]:
str[i] = keyT[a[0]][Mod5(a[1] + 1)]
str[i + 1] = keyT[a[0]][Mod5(a[3] + 1)]
elif a[1] == a[3]:
str[i] = keyT[Mod5(a[0] + 1)][a[1]]
str[i + 1] = keyT[Mod5(a[2] + 1)][a[1]]
else:
str[i] = keyT[a[0]][a[3]]
str[i + 1] = keyT[a[2]][a[1]]
# Encryption function for Playfair Cipher
def EncryptByPlayfairCipher(stri, key):
ks = len(key)
ks = RemoveSpaces(key, ks)
ToLowerCase(key, ks)
ps = len(stri)
ToLowerCase(stri, ks)
ps = RemoveSpaces(stri, ps)
ps = Prepare(stri, ps)
GenerateKeyTable(key, ks)
Encrypt(stri, ps)
key1 = input("Enter the key: ")
key = Convert(key1)
stri1 = input("Enter plain text: ")
stri = Convert(stri1)
EncryptByPlayfairCipher(stri, key)
print("Cipher text:", "".join(stri))