Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

FACULTY OF ENGINEERING AND TECHNOLOGY

Department of Information Technology


01IT0611 – Information and Network Security

Practical 2

AIM - Implement Playfair Cipher

Code :-

def playfair_cipher(plain_text, key):


plain_text = plain_text.replace(" ", "").upper()
key = key.replace(" ", "").upper()

key_square = ""
for char in key:
if char not in key_square:
key_square += char

for char in "ABCDEFGHIKLMNOPQRSTUVWXYZ":


if char not in key_square:
key_square += char

digraphs = []
for i in range(0, len(plain_text), 2):
if i == len(plain_text) - 1:
digraphs.append(plain_text[i] + "Z")
elif plain_text[i] == plain_text[i+1]:
digraphs.append(plain_text[i] + "X")
plain_text = plain_text[:i+1] + "X" + plain_text[i+1:]
else:
digraphs.append(plain_text[i:i+2])

cipher_text = ""
for digraph in digraphs:
row1, col1 = divmod(key_square.index(digraph[0]), 5)

Pranav Gediya - 92100104066 1


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Information Technology
01IT0611 – Information and Network Security

row2, col2 = divmod(key_square.index(digraph[1]), 5)

if row1 == row2:
cipher_text += key_square[row1*5 + (col1+1)%5]
cipher_text += key_square[row2*5 + (col2+1)%5]
elif col1 == col2:
cipher_text += key_square[((row1+1)%5)*5 + col1]
cipher_text += key_square[((row2+1)%5)*5 + col2]
else:
cipher_text += key_square[row1*5 + col2]
cipher_text += key_square[row2*5 + col1]

return cipher_text

plain_text = input("Enter plain text :")


key = input("Enter the key :")
cipher_text = playfair_cipher(plain_text, key)
print("Ciphertext:", cipher_text)

Output :-

Pranav Gediya - 92100104066 2


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Information Technology
01IT0611 – Information and Network Security

Practical 3.1

AIM : Implement Ceasar cipher.

Code :-

def caesar_cipher(text, shift):


result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
elif char.islower():
result += chr((ord(char) + shift - 97) % 26 + 97)
else:
result += char
return result
a=input("Enter String: ")
b=int(input("Enter Key: "))
encrypted_text = caesar_cipher(a, b)
print(encrypted_text)

Output :-

Pranav Gediya - 92100104066 3


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Information Technology
01IT0611 – Information and Network Security

Practical 3.2

AIM : Implement Hill cipher.

Code :-

import numpy as np

def hill_cipher(plain_text, key):


plain_text = plain_text.upper()
plain_text = [ord(char) - 65 for char in plain_text]
plain_text = np.array(plain_text).reshape(-1, 1)

key = key.upper()
key = [ord(char) - 65 for char in key]
key = np.array(key).reshape(int(np.sqrt(len(key))), -1)

if plain_text.shape[0] % key.shape[0] != 0:
padding = key.shape[0] - plain_text.shape[0] % key.shape[0]
plain_text = np.vstack([plain_text, np.zeros((padding, 1))])

cipher_text = ""
for i in range(0, plain_text.shape[0], key.shape[0]):
block = plain_text[i:i+key.shape[0]]
block = np.dot(key, block) % 26
block = "".join([chr(char + 65) for char in block.flatten()])
cipher_text += block

return cipher_text

plain_text = input("Enter the plain text :")


key = input("Enter the key :")

Pranav Gediya - 92100104066 4


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Information Technology
01IT0611 – Information and Network Security

cipher_text = hill_cipher(plain_text, key)


print("Ciphertext:", cipher_text)

Output :-

Pranav Gediya - 92100104066 5


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Information Technology
01IT0611 – Information and Network Security

Practical 4

AIM : Implement Euclid algorithm to find GCD.

Code :-

def gcd(a, b):


if b == 0:
return a
else:
return gcd(b, a % b)
a=int(input("Enter N1:"))
b=int(input("Enter N2:"))
print("GCD(",a ,",",b,") :",gcd(a,b))

Output :-

Pranav Gediya - 92100104066 6

You might also like