0% found this document useful (0 votes)
195 views2 pages

Cryptogtaphy Fundamentals Lab Assignment - 3 AES Code:: 17BCE0147 Vijay Surya

This document contains Python code to encrypt and decrypt files using AES encryption. It defines encrypt and decrypt functions that take a key and filename as arguments. The encrypt function writes the encrypted file in ciphertext format, while the decrypt function recovers the original plaintext file. A getKey function generates an AES encryption key from a user-provided password using SHA256 hashing. The main function runs an interactive prompt that allows the user to choose encrypting or decrypting a file, providing the necessary inputs.

Uploaded by

Surya Vjkumar
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)
195 views2 pages

Cryptogtaphy Fundamentals Lab Assignment - 3 AES Code:: 17BCE0147 Vijay Surya

This document contains Python code to encrypt and decrypt files using AES encryption. It defines encrypt and decrypt functions that take a key and filename as arguments. The encrypt function writes the encrypted file in ciphertext format, while the decrypt function recovers the original plaintext file. A getKey function generates an AES encryption key from a user-provided password using SHA256 hashing. The main function runs an interactive prompt that allows the user to choose encrypting or decrypting a file, providing the necessary inputs.

Uploaded by

Surya Vjkumar
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/ 2

17BCE0147 VIJAY SURYA

CRYPTOGTAPHY FUNDAMENTALS
LAB ASSIGNMENT - 3
AES

Code:

import os
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto import Random
def encrypt(key, filename):
chunksize =64 * 1024
outputFile = "(encrypted)" + filename
filesize = str(os.path.getsize(filename)).zfill(16)
IV = Random.new().read(16)
encryptor = AES.new(key, AES.MODE_CBC, IV)

with open(filename, "rb") as infile:


with open(outputFile, "wb") as outfile:
outfile.write(filesize.encode("utf-8"))
outfile.write(IV)

while True:
chunk = infile.read(chunksize)

if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk +=b ' ' * (16 – (len(chunk) % 16))
outfile.write(encryptor.encrypt(chunk))

#Decryption of files
def decrypt(key, filename):
chunksize =64 * 1024
outputFile = filename[11:]

with open(filename, ‘rb’) as infile:


filesize = int(infile.read(16))
IV = infile.read(16)

decryptor = AES.new(key, AES.MODE_CBC, IV)

with open(outputFile, ‘wb’) as outfile:


while True:
chunk = infile.read(chunksize)

if len(chunk) == 0:
break

outfile.write(decryptor.decrypt(chunk))
outfile.truncate(filesize)
17BCE0147 VIJAY SURYA
def getKey(password):
hasher = SHA256.new(password.encode(‘utf-8’))
return hasher.digest()

def Main():
choice = input(“Would you like to (E)ncrypt or (D)ecrypt?: “)
if choice == ‘E’ or choice == ‘e’:
filename = input(“File to encrypt: “)
password = input(“Password: “)
encrypt(getKey(password), filename)
print(“Done.”)
elif choice == ‘D’ or choice == ‘d’:
filename = input(“File to decrypt: “)
password = input(“Password: “)
decrypt(getKey(password), filename)
print(“Done.”)
else:
print(“No Option selected, closing…”)

if __name__ == ‘__main__’:
Main()

You might also like