0% found this document useful (0 votes)
2K views

Implementation of Product Cipher Using Substitution and Transposition Technique

This document describes experiments implementing product ciphers using substitution and transposition techniques in Python. It first implements a Vigenere cipher and product cipher using Vigenere. It then implements rail fence cipher and product cipher using rail fence. The code takes plaintext and key as input, encrypts the text using the techniques, then decrypts it back to the original plaintext. The outputs of the experiments show the encrypted, decrypted and original texts.

Uploaded by

Sonam Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Implementation of Product Cipher Using Substitution and Transposition Technique

This document describes experiments implementing product ciphers using substitution and transposition techniques in Python. It first implements a Vigenere cipher and product cipher using Vigenere. It then implements rail fence cipher and product cipher using rail fence. The code takes plaintext and key as input, encrypts the text using the techniques, then decrypts it back to the original plaintext. The outputs of the experiments show the encrypted, decrypted and original texts.

Uploaded by

Sonam Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT-2

IMPLEMENTATION OF PRODUCT CIPHER USING SUBSTITUTION AND


TRANSPOSITION TECHNIQUE

Name: Sonam Gupta (201903015)


Roll no.: 14
Date of Performance: 30/07/2021
Date of Submission: 05/08/2021

1 a] Vigenere Cipher:
Aim: To write a python program to implement vigenere cipher
encryption.

Theory:

Vigenere Cipher is a method of encrypting alphabetic text. It uses a


simple form of polyalphabetic substitution. A polyalphabetic cipher is
any cipher based on substitution, using multiple substitution alphabets
.The encryption of the original text is done using the Vigenère square or
Vigenère table.
 The table consists of the alphabets written out 26 times in different
rows, each alphabet shifted cyclically to the left compared to the
previous alphabet, corresponding to the 26 possible Caesar Ciphers.
 At different points in the encryption process, the cipher uses a
different alphabet from one of the rows.
 The alphabet used at each point depends on a repeating keyword.

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

1b] Product Cipher Encryption and Decryption using Vigenere


Aim: To write a python program to implement product cipher encryption
and decryption using vigenere cipher.

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))

def productText(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))
vigenereText = "". join(cipher_text)
print("Vigenere Text while Encryption:",vigenereText)
cipher_text = []
for i in range(len(vigenereText)):
x = (ord(vigenereText[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(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:

2a] Railfence Technique


Aim: To write a python program for the implementation of encryption
and decryption using railfence technique.
Theory:
In the rail fence cipher, the plain text is written downwards and
diagonally on successive "rails" of an imaginary fence, then moving up
when we reach the bottom rail. When we reach the top rail, the message
is written downwards again until the whole plaintext is written out. The
message is then read off in rows.

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

2b] Product Cipher Encryption and Decryption using Railfence


Aim: To write a python program for the implementation of product
cipher using railfence technique.
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

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

Conclusion: By doing this experiment I get to know about different


ciphering techniques like vigenere cipher which is also known as
polyalphabetic cipher and railfenece cipher which is one of the type of
transposition techniques and also using both techniques how to generate
product cipher is also learned.

You might also like