Implementation of Product Cipher Using Substitution and Transposition Technique
Implementation of Product Cipher Using Substitution and Transposition Technique
1 a] Vigenere Cipher:
Aim: To write a python program to implement vigenere cipher
encryption.
Theory:
Algorithm:
Step-1: Start
Step-2: plain text and keyword is taken as input from user and using that
keyword a function is called which generated key equal to the length of
plain text by repeating that keyword.
Step-3: for encryption function is called which generates cipher text using
plain text and generated key as parameters using formula.
The plaintext(P) and key(K) are added modulo 26.
Ei = (Pi + Ki) mod 26
Encryption is done.
Step-4: similarly for decryption Di = (Ei - Ki + 26) mod 26 this formula
is used.
Step-5: End.
1
Code:
# Vigenere Cipher
## This function generates the key in a cyclic manner until it's length isn't
#equal to the length of original text
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) -
len(key)):
key.append(key[i % len(key)])
return("" . join(key))
# This function returns the encrypted text generated with the help of the
#key
def cipherText(string, key):
cipher_text = []
for i in range(len(string)):
x = (ord(string[i]) +
ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return("" . join(cipher_text))
# This function decrypts the encrypted text and returns the original text
def originalText(cipher_text, key):
orig_text = []
for i in range(len(cipher_text)):
x = (ord(cipher_text[i]) -
ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
# Driver code
if __name__ == "__main__":
string = "IAMANINDIAN"
keyword = "MEC"
key = generateKey(string, keyword)
2
cipher_text = cipherText(string,key)
print("Ciphertext :", cipher_text)
print("Original/Decrypted Text :",originalText(cipher_text, key))
Output:
Ciphertext : UEOMRKZHKMR
Original/Decrypted Text : IAMANINDIAN
Theory:
In cryptography, a product cipher combines two or more transformations
in a manner intending that the resulting cipher is more secure than the
individual components to make it resistant to cryptanalysis. The product
cipher combines a sequence of simple transformations such
as substitution (S-box), permutation (P-box), and modular arithmetic.
Encryption
Virgere Cipher:
The plaintext(P) and key(K) are added modulo 26.
Ei = (Pi + Ki) mod 26
Product Cipher:
EPRi = (Ei + Ki) mod 26
Decryption
DPRi = (EPRi - Ki + 26) mod 26
Di = (DPRi - Ki + 26) mod 26
Algorithm:
Step-1: Start
Step-2: plain text and keyword is taken as input from user and using that
keyword a function is called which generated key equal to the length of
plain text by repeating that keyword.
Step-3: for encryption function is called which generates cipher text using
plain text and generated key as parameters using formula.
The plaintext(P) and key(K) are added modulo 26.
Encryption:
Ei = (Pi + Ki) mod 26
EPRi = (Ei + Ki) mod 26
Step-4: Similarly for decryption formula used is,
Decryption:
DPRi = (EPRi - Ki + 26) mod 26
3
Di = (DPRi - Ki + 26) mod 26
Step-5: End.
Code:
# This function generates the key in a cyclic manner until it's length isn't
equal to the length of original text
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) -
len(key)):
key.append(key[i % len(key)])
return("" . join(key))
# This function decrypts the encrypted text and returns the original text
def originalText(product_text, key):
orig_text = []
for i in range(len(product_text)):
x = (ord(product_text[i]) - ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
vigenereText = "". join(orig_text)
print("Vigenere Text while Decryption:",vigenereText)
orig_text = []
for i in range(len(vigenereText)):
x = (ord(vigenereText[i]) - ord(key[i]) + 26) % 26
x += ord('A')
4
orig_text.append(chr(x))
return("". join(orig_text))
# Driver code
if __name__ == "__main__":
string = "IAMANINDIAN"
keyword = "MEC"
key = generateKey(string, keyword)
product_text = productText(string,key)
print("ProductText :", product_text)
print("Original/Decrypted Text :", originalText(product_text, key))
Output:
Vigenere Text while Encryption: UEOMRKZHKMR
ProductText : GIQYVMLLMYV
Vigenere Text while Decryption: UEOMRKZHKMR
Original/Decrypted Text : IAMANINDIAN
TRANSPOSITION TECHNIQUE:
Algorithm:
Step-1: Start.
Step-2: plain text and keyword is taken as input from user and passed to
railfence function which generated cipher text based upon key.
Step-3: then if key is 2 then every 2nd letter is taken first and then
remaining then cipher text is generated.
5
Step-4: for decryption letters are taken as columnwise so that plain text
gets generated back.
Step-5: End.
Code:
#Encryption:
cipher_text=""
def railfence(plain_text,key):
if key==2:
return (plain_text[::2]+plain_text[1::2])
else:
return "number of rails not supported"
cipher_text= railfence("xie is best ", 2)
print(“Encrypted/cipher text: ”,cipher_text)
#Decryption:
block1=cipher_text[:6:]
print(“Row 1: ”,block1)
block2=cipher_text[6::]
print(“Row 2: ”,block2)
x1=print(“Decrypted/original text:
”,block1[0]+block2[0]+block1[1]+block2[1]+block1[2]+block2[2]+block
1[3]+block2[3]+
block1[4]+block2[4]+block1[5]+block2[5])
Output:
Encrypted/cipher text: xei eti sbs
Row 1: xei et
Row 2: i sbs
Decrypted/original text: xie is best
Algorithm:
Step-1: Start.
Step-2: plain text and keyword is taken as input from user and passed to
railfence function which generated cipher text based upon key.
6
Step-3: then if key is 2 then every 2nd letter is taken first and then
remaining then cipher text is generated. this process is repeated one more
time to generate product cipher.
Step-4: for decryption letters are taken as columnwise. This process is
also repeated once more to generate original text.
Step-5: End.
Code:
cipher_text=""
def railfence(plain_text,key):
if key==2:
temp1 = plain_text[::2]+plain_text[1::2]
print("Encrypting round 1:", temp1)
temp2 = temp1[::2]+temp1[1::2]
print("Encrypting round 2:", temp2)
return temp2
else:
return "number of rails not supported"
cipher_text= railfence("xieisbest ", 2)
print("Encrypted/cipher text: ",cipher_text)
#Decryption:
print("Decryption: ")
print("Round 1:")
block1=cipher_text[:5:]
print("Row 1: ",block1)
block2=cipher_text[5::]
print("Row 2: ",block2)
plain_text =
block1[0]+block2[0]+block1[1]+block2[1]+block1[2]+block2[2]+block1
[3]+block2[3]+block1[4]+block2[4]
print("Round 2:")
block1=plain_text[:5:]
print("Row 1: ",block1)
block2=plain_text[5::]
print("Row 2: ",block2)
plain_text =
block1[0]+block2[0]+block1[1]+block2[1]+block1[2]+block2[2]+block1
[3]+block2[3]+block1[4]+block2[4]
plain_text =
block1[0]+block2[0]+block1[1]+block2[1]+block1[2]+block2[2]+block1
[3]+block2[3]+block1[4]+block2[4]
7
x1=print("Decrypted/original text: ",plain_text)
Output:
Encrypting round 1: xesetiibs
Encrypting round 2: xstiseeib
Encrypted/cipher text: xstiseeib
Decryption:
Round 1:
Row 1: xstis
Row 2: eeib
Round 2:
Row 1: xeset
Row 2: iibs
Decrypted/original text: xieisbest