0% found this document useful (0 votes)
34 views3 pages

Is Exp 3

This document outlines a worksheet for a student named Mihir Garg, focusing on implementing the Hill cipher in an Information Security Lab. It includes a Python program that generates a key, constructs a key matrix, and encrypts a message using the Hill cipher algorithm. The program prompts the user for input and displays the generated key and ciphertext as output.

Uploaded by

Mihir Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Is Exp 3

This document outlines a worksheet for a student named Mihir Garg, focusing on implementing the Hill cipher in an Information Security Lab. It includes a Python program that generates a key, constructs a key matrix, and encrypts a message using the Hill cipher algorithm. The program prompts the user for input and displays the generated key and ciphertext as output.

Uploaded by

Mihir Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Worksheet 3

Student Name: MIHIR GARG UID:19BCS1384


Branch: CSE Section/Group: 19BCS_IS_5A
Semester: 6th Date of Performance: -
Subject Name: Information Security Lab Subject Code: CSB-372

Aim: Code to implement Hill cipher


PROGRAM:

import random
import numpy

def generateKey(s):
list1=[]
for i in range(65,91):
list1.append(chr(i))

for i in range(97,123):
list1.append(chr(i))

key=""
for i in range(s):
random.shuffle(list1)
a = random.choice(list1)
key+=a
return key

def getKeyMatrix(key,l):
k=0
for i in range(l):
for j in range(l):
keyMatrix[i][j] = ord(key[k]) % 65
k += 1

def encrypt(messageVector,l):
for i in range(l):
for j in range(1):
cipherMatrix[i][j] = 0
for x in range(l):
cipherMatrix[i][j] += (keyMatrix[i][x] * messageVector[x][j])
cipherMatrix[i][j] = cipherMatrix[i][j] % 26

def HillCipher(message, key):


leng=len(message)
getKeyMatrix(key,leng)
for i in range(leng):
messageVector[i][0] = ord(message[i]) % 65
encrypt(messageVector,leng)

CipherText = []
for i in range(leng):
CipherText.append(chr(cipherMatrix[i][0] + 65))

print("Ciphertext: ", "".join(CipherText))

str1=input("Enter the text you want to encypt --> ") # 4


lenOfMessage=len(str1)
keyMatrix = [[0] * lenOfMessage for i in range(lenOfMessage)]
messageVector = [[0] for i in range(lenOfMessage)]
cipherMatrix = [[0] for i in range(lenOfMessage)]

key = generateKey(lenOfMessage*lenOfMessage) # key must generate of 4*4


print("Key Generated : " + key)
HillCipher(str1, key)

OUTPUT:

You might also like