In this we are going to see a basic example of cryptoanalysis or decoding playfair, vigenère cipher in Python Programming Language.
#For Playfair Cipher
key = input("Enter key: ")
key = key.replace(" ", "")
key = key.upper()
def matrix(x, y, initial):
return [[initial for i in range(x)] for j in range(y)]
result = list()
for c in key: # storing key
if c not in result:
if c == 'J':
result.append('I')
else:
result.append(c)
flag = 0
for i in range(65, 91): # storing other character
if chr(i) not in result:
if i == 73 and chr(74) not in result:
result.append("I")
flag = 1
elif flag == 0 and i == 73 or i == 74:
pass
else:
result.append(chr(i))
k = 0
my_matrix = matrix(5, 5, 0) # initialize matrix
for i in range(0, 5): # making matrix
for j in range(0, 5):
my_matrix[i][j] = result[k]
k += 1
def locindex(c): # get location of each character
loc = list()
if c == 'J':
c = 'I'
for i, j in enumerate(my_matrix):
for k, l in enumerate(j):
if c == l:
loc.append(i)
loc.append(k)
return loc
def encrypt(): # Encryption
msg = str(input("\nEnter message: "))
msg = msg.upper()
msg = msg.replace(" ", "")
i = 0
for s in range(0, len(msg) + 1, 2):
if s < len(msg) - 1:
if msg[s] == msg[s + 1]:
msg = msg[:s + 1] + 'X' + msg[s + 1:]
if len(msg) % 2 != 0:
msg = msg[:] + 'X'
print("Cipher Text: ", end=' ')
while i < len(msg):
loc = list()
loc = locindex(msg[i])
loc1 = list()
loc1 = locindex(msg[i + 1])
if loc[1] == loc1[1]:
print("{}{}".format(my_matrix[(loc[0] + 1) % 5][loc[1]], my_matrix[(loc1[0] + 1)% 5][loc1[1]]), end=' ')
elif loc[0] == loc1[0]:
print("{}{}".format(my_matrix[loc[0]][(loc[1] + 1) % 5], my_matrix[loc1[0]][(loc1[1] + 1) % 5]), end=' ')
else:
print("{}{}".format(my_matrix[loc[0]][loc1[1]], my_matrix[loc1[0]][loc[1]]), end=' ')
i = i + 2
def decrypt(): # decryption
msg = str(input("\nEnter Cipher Text: "))
msg = msg.upper()
msg = msg.replace(" ", "")
print("Plain Text: ", end=' ')
i = 0
while i < len(msg):
loc = list()
loc = locindex(msg[i])
loc1 = list()
loc1 = locindex(msg[i + 1])
if loc[1] == loc1[1]:
print("{}{}".format(my_matrix[(loc[0] - 1) % 5][loc[1]], my_matrix[(loc1[0] - 1)% 5][loc1[1]]), end=' ')
elif loc[0] == loc1[0]:
print("{}{}".format(my_matrix[loc[0]][(loc[1] - 1) % 5], my_matrix[loc1[0]][(loc1[1] - 1) % 5]), end=' ')
else:
print("{}{}".format(my_matrix[loc[0]][loc1[1]], my_matrix[loc1[0]][loc[1]]), end=' ')
i = i + 2
def main():
while (1):
print("\nChoices:\n1.Encrypt plaintext \n2.Decrypt Cipher: \n3.Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
encrypt()
elif choice == 2:
decrypt()
elif choice == 3:
exit()
else:
print("Incorrect choice. Exiting...")
main()
#For Vigenère Cipher
def getKey(pt, key):
key = list(key)
if len(pt) == len(key):
return(key)
else:
for i in range(len(pt) - len(key)):
key.append(key[i % len(key)])
return("" . join(key))
def encipher(pt, key):
ct = []
for i in range(len(pt)):
x = (ord(pt[i]) + ord(key[i])) % 26
x += ord('A')
ct.append(chr(x))
return("" . join(ct))
def decipher(ct, key):
pt = []
for i in range(len(ct)):
x = (ord(ct[i]) - ord(key[i]) + 26) % 26
x += ord('A')
pt.append(chr(x))
return("" . join(pt))
def main():
pt = input("Enter plaintext: ")
keyword = input("Enter keyword: ")
key = getKey(pt, keyword)
cipher = encipher(pt, key)
print("Encrypted message:", cipher)
print("Decrypted message:", decipher(cipher, key))
main()
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP