Professional Documents
Culture Documents
LAB - MANUAL - Cryptography Vineet
LAB - MANUAL - Cryptography Vineet
LAB - MANUAL - Cryptography Vineet
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
Enter the number through which you want to encrypt the message3
Enter the statmentHELLO
Encrypted message: KHOOR
Decrypted Message HELLO
cipher
return cipher
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))
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))
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))
10