In: Computer Science
Question
The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”. Write Java or Python code to implement either Monoalphabetic cipher or Hill cipher or Transposition Cipher (Encryption and Decryption) and test your code on given plaintext. User may enter value of key at the command prompt, if required.
I have implemented the Monoalphabetic cipher using Caesar Cipher.
Code(Python 3.7.4)
#Program to implement Monoalphabetic cipher
import numpy as np
#each letter of a given word is replaced by a letter shifted n
number of positions.
def cypher(ptext,n):
ctext = ""
for i in range(len(ptext)):
char = ptext[i]
if
(char.isupper()):
ctext += chr((ord(char) + n-65) % 26 + 65)
else:
ctext += chr((ord(char) + n - 97) % 26 + 97)
return ctext
#using the cyclic property of modulo,instead of writing a
seperate decrypt fuction we call
#the encrypt fuction with 26-n to decrypt the encypted text.
def decrypt(ctext,n):
ptext = cypher(ctext,26-n)
return ptext
text = "Feistel cipher structure uses the same algorithm for both
encryption and decryption"
print ("The plain text is: " + text)
k = 7 #define a shift of 7 positions
#removing spaces and putting the words into an array
res = text.split()
#Encrypting each word in the array
mod26 = []
for word in res:
temp = cypher(word, k)
mod26.append(temp)
#print(mod26)
#Merging the encypted words back into a string
encrypted = ''
for word in mod26:
encrypted = encrypted + word + ' '
print ("The encypted text is: " + encrypted)
#removing spaces and putting the encrypted words into an
array
res = encrypted.split()
#decrypting each word in the array
plain = []
for word in res:
temp = decrypt(word, k)
plain.append(temp)
#print(plain)
#Converting the decrypted array back into a string
decrypted = ''
for word in plain:
decrypted = decrypted + word + ' '
print ("The decrypted text is: " + decrypted)
Output:
python cypher.py
The plain text is: Feistel cipher structure uses the same algorithm
for both encryption and decryption
The encypted text is: Mlpzals jpwoly zaybjabyl bzlz aol zhtl
hsnvypaot mvy ivao lujyfwapvu huk kljyfwapvu
The decrypted text is: Feistel cipher structure uses the same
algorithm for both encryption and decryption