0% found this document useful (0 votes)
51 views15 pages

LAB - MANUAL - Cryptography Vineet

The document describes a laboratory manual for an Introduction to Cryptography lab course. It contains 10 experiments on implementing various encryption algorithms like Shift Cipher, Polyalphabetic Substitution Cipher, Caesar Cipher, Additive Cipher, Multiplicative Cipher, Affine Cipher, Playfair Cipher, Vernam Cipher, DES and AES. Each experiment has program code snippets and sample outputs to encrypt and decrypt messages.

Uploaded by

chintuojha7
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)
51 views15 pages

LAB - MANUAL - Cryptography Vineet

The document describes a laboratory manual for an Introduction to Cryptography lab course. It contains 10 experiments on implementing various encryption algorithms like Shift Cipher, Polyalphabetic Substitution Cipher, Caesar Cipher, Additive Cipher, Multiplicative Cipher, Affine Cipher, Playfair Cipher, Vernam Cipher, DES and AES. Each experiment has program code snippets and sample outputs to encrypt and decrypt messages.

Uploaded by

chintuojha7
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
You are on page 1/ 15

NOIDA INSTITUTE OF ENGINEERING AND

TECHNOLOGY GREATER NOIDA-201306


(An Autonomous Institute under affiliation of AKTU)
School of Computer Sciences & Engineering in
Emerging Technologies

B.Tech CSE(AI)

LAB MANUAL
of
Introduction to Cryptography Lab (ACS0451)
(4th Semester)

Affiliated to Dr. A.P.J Abdul Kalam Technical University, Uttar Pradesh, Lucknow

Submitted to: Ms Monika Mehra submitted by: Vineet chaudhary

:Mr Praveen Kumar Tomar :2201331520119


INDEX

Course Code ACSAI0453

Course Title Introduction to Cryptography Lab


Sr. No. Name of Experiment Date signature
1 Implementing Shift Cipher

2 Implementing Polyalphabetic Substitution Cipher.

3. Implementing Caesar Cipher

4 Implementing Additive Cipher (Mono-alphabetic Substitution Cipher)

5 Implementing Multiplicative Cipher (Mono-alphabetic Substitution


Cipher)
6 Implementing Affine Cipher (Mono-alphabetic Substitution Cipher)

7 Implementing Playfair Cipher

8 Implementing Vernam Cipher.

9 Implementing Symmetric Key Encryption Standards (DES)

10 Implementing Symmetric Key Encryption Standards (AES)


PROGRAM 1 PROGRAM TO IMPLEMENT SHIFT CIPHER
a=input("Enter the statement you want to encode: ")
for i in a: s=ord(i) if s==32:
j=chr(s)
elif s==65:
j=chr(108)
elif s==66:
j=chr(109)
elif s==67:
j=chr(122)
elif s==68:
j=chr(97) elif
s==69: j=chr(98)
elif
s==70:
j=chr(100)
elif s==71:
j=chr(99) elif
s==72:
j=chr(103)
elif s==73:
j=chr(110)
elif s==74:
j=chr(102)
elif s==75:
j=chr(101)
elif s==76:
j=chr(104)
elif s==77:
j=chr(105)
elif s==78:
j=chr(117) elif
s==79: j=chr(106)
elif s==80:
j=chr(115) elif
s==81:
j=chr(119)
elif s==82: j=chr(107)
elif s==83:
j=chr(121)
elif s==84:
j=chr(118) elif
s==85: j=chr(120)
elif s==86:
j=chr(111) elif
s==87:
elif s==88:
j=chr(113)
elif s==89:
j=chr(116)
elif s==90:
j=chr(112)
print(j,end="")

Enter the statement you want to encode: HELLO I AM BOY gbhhj


n li mjt
a=input("Enter the statement you want to decode:
")
for i in a: s=ord(i) if s==32:
j=chr(s)
elif s==108:
j=chr(65)
elif s==109:
j=chr(66)
elif s==122:
j=chr(67) elif
s==97: j=chr(68)
elif
s==98:
j=chr(69)
elif s==100:
j=chr(70) elif
s==99:
j=chr(71)
elif s==103:
j=chr(72)
elif s==110:
j=chr(73) elif
s==102: j=chr(74)
elif s==101:
j=chr(75) elif
s==104:
j=chr(76)
elif s==105:
j=chr(77)
elif s==117:
j=chr(78) elif
s==106:
j=chr(79)
elif s==115:
j=chr(80)
elif s==119:
j=chr(81)
elif s==107:
j=chr(82) elif
s==121:
j=chr(83)
elif s==118:
j=chr(84)
elif s==120:
j=chr(85)
elif s==111:
j=chr(86)
elif s==114:
j=chr(87)
elif s==113:
j=chr(88)
elif s==116:
j=chr(89)
elif s==112:
j=chr(90)
print(j,end="")

Enter the statement you want to decode: gbhhj n li mjt HELLO


I AM BOY

PROGRAM 2 PROGRAM TO IMPLEMENT POLYALPHABETIC SUBSTITUTION CIPHER (ONE TIME


PAD CIPHER) x=int(input("Enter the number through which you want to encrypt the
message")) s=input("Enter the statment") c="" n=""
for i in
s:
if i!=" ":
d=ord(i) d=d+x d=chr(d)
c=c+d
else:
c=c+i
print("Encrypted message: ",c)
for j in
c:
if j!=" ":
k=ord(j) k=k-x k=chr(k)
n=n+k
else:
n=n+j
print("Decrypted Message",n)

Enter the number through which you want to encrypt the message3
Enter the statmentHELLO
Encrypted message: KHOOR
Decrypted Message HELLO

s=input("Enter the text")


k=input("Enter the Key")
b=[] z=[] add=[] f=""
for i in s: l=ord(i)
l=l-65
b.append(l)
print(b) for
j in k:
m=ord(j) m=m-
97
z.append(m)
print(z)
for a in
range(len(b)):
d=b[a]+z[a] if d>26: d=d-
26 add.append(d)
else: add.append(d)
print(add)
for
e in add:
a=97+e
f=f+chr(a)
print(f)

Enter the texthello


Enter the Keyapple
[39, 36, 43, 43, 46]
[0, 15, 15, 11, 4]
[13, 25, 32, 28, 24] nz
}y
PROGRAM 3 PROGRAM TO IMPLEMENT CAESAR CIPHER
# Encryption part def
encrypt(message, key):
cipher = "" for i in
message:
if i.isupper(): cipher += chr((ord(i) + key -
65) % 26 + 65) elif
i.islower():
cipher += chr((ord(i) + key - 97) % 26 + 97)
else:
cipher+=" " return

cipher

message = input("Enter the message:")


print("Cipher:", encrypt(message, 3))

# Decryption part def


decrypt(cipher, key):
message = "" for i in
cipher:
if i.isupper(): message += chr((ord(i) - key -
65) % 26 + 65) elif
i.islower():
message += chr((ord(i) - key - 97) % 26 + 97)
else:
message+=" "
return message
cipher = input("Enter the cipher:")
print("Message: ", decrypt(cipher, 3))

Enter the message:hello sir


Cipher: khoor vlu
Enter the cipher:khoor vlu Message:
hello sir

PROGRAM 4 PROGRAM TO IMPLEMENT ADDITIVE CIPHER (MONOALPHABETIC SUBSTITUTION CIPHER)


# Encryption part def encrypt(message, key): cipher =
"" for i in message: if i.isupper(): cipher +=
chr((ord(i) + key -
65) % 26 + 65) elif i.islower(): cipher
+= chr((ord(i) + key - 97) % 26 + 97)
else: cipher+="
"

return cipher

message = input("Enter the message:") key = input("Enter


the key numeric value or any alphabet:") if key.isupper():
key = ord(key) - 65
elif key.islower():
key = ord(key) - 97
else:
key = int(key)
print("Cipher:", encrypt(message, key))
# Decryption part def decrypt(cipher, key): message =
"" for i in cipher: if i.isupper(): message +=
chr((ord(i) - key -
65) % 26 + 65) elif
i.islower():
message += chr((ord(i) - key - 97) % 26 + 97)
else: message+="
" return message
cipher = input("Enter the cipher:") key =
input("Enter the key numeric value or any
alphabet:") if key.isupper(): key = ord(key) -
65
elif key.islower():
key = ord(key) - 97
else:
key = int(key)
print("Message", decrypt(cipher, key))

Enter the message:hello everyone


Enter the key numeric value or any alphabet:3
Cipher: khoor hyhubrqh
Enter the cipher:khoor hyhubrgh
Enter the key numeric value or any alphabet:3
Message hello everyode

PROGRAM 5 PROGRAM TO IMPLEMENT MULTIPLICATIVE CIPHER (MONOALPHABETIC SUBSTITUTION CIPHER)


#Encryption def check(c): if c.isupper(): c=ord(c)-65 elif c.islower(): c=ord(c)-97
else: return c c=int(c)
def
encrypt(message,k):
cipher="" for i in message:
if
i.isupper():
cipher+=chr(((ord(i)-65)*k)%26+65) elif i.islower():
cipher+=chr(((ord(i)-97)*k)%26+97)
else: return
cipher
cipher+="
"
message=input("Enter the message:") k=input("Enter the keys,
numeric value or any alphabet:") k=check(k)
print("Cipher:",encrypt(message,k))

#Decryption part def


getCoeff(d): for i in
range(1,26): j=int(1)
eqn=int(1) while(eqn>=1):
eqn=26*i-d*j if eqn==1:
return -j j=j+1 def
decrypt(cipher,k): message=""
k=getCoeff(k) for i
in cipher: if
i.isupper():
message+=chr(((ord(i)-65)*k)%26+65) elif i.islower():
message+=chr(((ord(i)-97)*k)%26+97)
else: return
message
message+="
"
cipher=input("Enter the cipher:") k=input("Enter the key,
numeric value or any alphabet :") k=check(k)
print("Message:",decrypt(cipher,k))
Enter the message:hello everyone
Enter the keys, numeric value or any alphabet:3 Cipher: vmhhq mlmzuqnm
Enter the cipher:vmhhq mlmzuqnm
Enter the key, numeric value or any alphabet :3 Message: hello everyone

PROGRAM 6 PROGRAM TO IMPLEMENT AFFINE CIPHER (MONOAPHABETIC SUBSTITUTION CIPHER)


#Encryption def check(c): if c.isupper(): c=ord(c)-65 elif c.islower():
c=ord(c)-97 else:
c=int(c)
return c

def encrypt(message,a,b): cipher="" for i in message: if


i.isupper(): cipher+=chr(((ord(i)-65)*a+b)%26+65) elif
i.islower(): cipher+=chr(((ord(i)-97)*a+b)%26+97) else:
cipher+=" "
return cipher

message=input("Enter the message:")


#’a’ is multiplicative key #’b’ is additive key a,b=input("Enter the two keys, numeric
value or any alphabet separated spaces:").split() a=check(a) b=check(b)
print("Cipher:",encrypt(message,a,b))

#Decryption part def getCoeff(a):


for i in range(1,26): j=int(1)
eqn=int(1)
while(eqn>=1): eqn=26*ia*j
if eqn==1: return -j j=j+1
def decrypt(cipher,a,b): message="" a=getCoeff(a)
for i in cipher: if
i.isupper(): message+=chr(((ord(i)-65-b)*a)%26+65)
elif i.islower(): message+=chr(((ord(i)-97-
b)*a)%26+97)
else:

message+="”
return message
cipher=input("Enter the cipher:") a,b=input("Enter the two keys, numeric value or
any
alphabet seperated by spaces:").split() a=check(a) b=check(b)
print("Message:",decrypt(cipher,a,b))

Enter the message:hello everyone


Enter the two keys, numeric value or any alphabet separated spaces:3 5
Cipher: armmv rqrezvsr
Enter the cipher:armmv rqrezvsr
Enter the two keys, numeric value or any alphabet seperated by spaces:3 5
Message: hello everyone
PROGRAM 7 PROGRAM TO IMPLEMENT PLAYFAIR CIPHER def
convertPlainTextToDiagraphs (plainText):
for s in range(0,len(plainText)+1,2): if
s<len(plainText)-1:
if plainText[s]==plainText[s+1]:
plainText=plainText[:s+1]+'X'+plainText[s+1:] if
len(plainText)%2 != 0:
plainText = plainText[:]+'X'
return plainText
def generateKeyMatrix
(key):
matrix_5x5 = [[0 for i in range (5)] for j in range(5)]
simpleKeyArr = [] for c in key:
if c not in simpleKeyArr:
if c == 'J':
simpleKeyArr.append('I')
else:
simpleKeyArr.append(c)
is_I_exist = "I" in simpleKeyArr for
i in range(65,91):
if chr(i) not in simpleKeyArr:
if i==73 and not is_I_exist:
simpleKeyArr.append("I")
is_I_exist = True
elif i==73 or i==74 and is_I_exist:
pass
else:
simpleKeyArr.append(chr(i))
index = 0 for i in range(0,5):
for j in range(0,5):
matrix_5x5[i][j] = simpleKeyArr[index]
index+=1
return matrix_5x5 def indexLocator
(char,cipherKeyMatrix): indexOfChar =
[] if char=="J": char = "I" for i,j in
enumerate(cipherKeyMatrix): for k,l in
enumerate(j): if char == l:
indexOfChar.append(i) indexOfChar.append(k) return indexOfChar
def encryption (plainText,key): cipherText = [] keyMatrix =
generateKeyMatrix(key) i = 0 while i < len(plainText):
n1 = indexLocator(plainText[i],keyMatrix) n2 = indexLocator(plainText[i+1],keyMatrix)
if n1[1] == n2[1]:
i1 = (n1[0] + 1) % 5 j1 = n1[1] i2 = (n2[0] + 1) % 5 j2 = n2[1]
cipherText.append(keyMatrix[i1][j1])
cipherText.append(keyMatrix[i2][j2]) cipherText.append(", ")
elif n1[0]==n2[0]: i1= n1[0] j1= (n1[1] + 1) % 5 i2= n2[0] j2= (n2[1] +
1) % 5 cipherText.append(keyMatrix[i1][j1])
cipherText.append(keyMatrix[i2][j2]) cipherText.append(", ") else:
i1 = n1[0] j1 = n1[1] i2 = n2[0] j2 = n2[1]
cipherText.append(keyMatrix[i1][j2]) cipherText.append(keyMatrix[i2][j1])
cipherText.append(", ") i += 2
return cipherText
def
main():
key = input("Enter key: ").replace(" ","").upper() plainText =input("Plain
Text: ").replace(" ","").upper() convertedPlainText =
convertPlainTextToDiagraphs(plainText) cipherText = "
".join(encryption(convertedPlainText,key)) print(cipherText) if name ==
" main ": main()

Enter key: hello Plain Text: enemy


O K , L K , Z Y
8

PROGRAM PROGRAM TO IMPLEMENT VERNAM CIPHER vernam_dict =


dict((i, chr(i + 96)) for i in range(1, 27)) def
vernam_encrypt(plain, key):
plain = plain.lower() ckey = ''.join([(key[i % len(key)]) for i in
range(len(list(plain)))]) print(ckey) cipher = '' for i in
range(len(plain)):
if plain[i] == ' ':
cipher += ' '
else:
cipher += vernam_dict[(ord(plain[i]) + ord(ckey[i])) % 26]
print(cipher, plain)

print(vernam_encrypt('mountains are bae', 'hello'))

hellohellohellohe edqjssxjo sga


ast mountains are bae None

class
Solution:
def solve(self, text, key):
cip = [] start = ord('a') for l, k in
zip(text, key): shift = ord(k) - start pos =
start + (ord(l) - start + shift) % 26
cip.append(chr(pos))
return ''.join([l for l in cip])
ob = Solution() text = input("Enter
the plain text") key = input("Enter
the key")
print(ob.solve(text, key))

Enter the plain


texthello Enter the
keyenemy lrpxm

9
PROGRAM PROGRAM TO IMPLEMENT ADVANCED ENCRYPTION STANDARD
#AES
# !pip install pycrypto
import base64
import hashlib
from Crypto.Cipher import AES from
Crypto import Random BLOCK_SIZE =
16 pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE -
len(s) % BLOCK_SIZE) unpad = lambda s: s[:-ord(s[len(s) - 1:])] def
encrypt(plain_text, key): private_key =
hashlib.sha256(key.encode("utf-8")).digest() plain_text =
pad(plain_text) print("After padding:", plain_text) iv
= Random.new().read(AES.block_size) cipher = AES.new(private_key,
AES.MODE_CBC, iv) return base64.b64encode(iv +
cipher.encrypt(plain_text)) def decrypt(cipher_text, key):
private_key =
hashlib.sha256(key.encode("utf-8")).digest() cipher_text =
base64.b64decode(cipher_text) iv = cipher_text[:16]
cipher = AES.new(private_key, AES.MODE_CBC, iv)
return unpad(cipher.decrypt(cipher_text[16:]))
message=input("Enter message to encrypt: "); key =
input("Enter encryption key: ") encrypted_msg =
encrypt(message, key) print("Encrypted Message:",
encrypted_msg) decrypted_msg =
decrypt(encrypted_msg, key)
print("Decrypted Message:", bytes.decode(decrypted_msg))

PROGRAM PROGRAM TO IMPLEMENT Data Encryption Standard


# DES
# !pip install pyDes import pyDes
def encrypt(): data =
input("Enter the text : ")
k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None,
padmode=pyDes.PAD_PKCS5) d = k.encrypt(data) return d def
decrypt(): d=encrypt() print(f"Encrypted: {d}" )
k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None,
padmode=pyDes.PAD_PKCS5) print(f"Decrypted: {k.decrypt(d)}")
decrypt()

10

You might also like