You are on page 1of 2

February 22, 2024

EXPERIMENT 4
Objective: Study of Symmetric Encryption Scheme. Task to be performed:
Implementation of following Techniques:
 Vigenere cipher (Polyalphabetic technique)
code:
#poly alphabetic
#vignere cipher
n=input("enter your plain text ")
k=input("enter key for cipher: ")
if(len(k)<len(n)):
while(len(k)!=len(n)):
k+=k
lst=[]
lst2=[]
et=[]
dt=[]
for i in n:
lst.append(i)
for i in k:
lst2.append(i)
for i in range(len(lst)):
et.append(chr((((ord(lst[i])-97)+(ord(lst2[i])-97))%26)+97))
print(et)
#decrypt
for i in range(len(lst)):
dt.append(chr((((ord(et[i])-97)-(ord(lst2[i])-97))%26)+97))
print(dt)

Output:

 Rail fence cipher (Transposition technique)


code:
def encrypt_rail_fence(text, rails):
fence = [['' for _ in range(len(text))] for _ in range(rails)]

rail, direction = 0, 1

for i, char in enumerate(text):


fence[rail][i] = char
rail += direction

if rail == rails - 1 or rail == 0:


direction *= -1

return ''.join(''.join(row) for row in fence)

def decrypt_rail_fence(cipher_text, rails):


February 22, 2024

fence = [[' ' for _ in range(len(cipher_text))] for _ in range(rails)]

rail, direction, index = 0, 1, 0

for i in range(len(cipher_text)):
fence[rail][i] = '*'
rail += direction

if rail == rails - 1 or rail == 0:


direction *= -1

for i in range(rails):
for j in range(len(cipher_text)):
if fence[i][j] == '*' and index < len(cipher_text):
fence[i][j] = cipher_text[index]
index += 1

plain_text = ''
rail, direction = 0, 1

for i in range(len(cipher_text)):
plain_text += fence[rail][i]
rail += direction

if rail == rails - 1 or rail == 0:


direction *= -1

return plain_text

text_to_encrypt = input("enter plain text ")


rails = int(input("enter no. of rails "))

encrypted_text = encrypt_rail_fence(text_to_encrypt, rails)


decrypted_text = decrypt_rail_fence(encrypted_text, rails)

print("Original Text:", text_to_encrypt)


print("Encrypted Text:", encrypted_text)
print("Decrypted Text:", decrypted_text)

Output:

You might also like