Question
Encrypting and Decrypting file using AES & CBC in Python I'm having issues with my code. I am supposed to encrypt and decrypt a file
Encrypting and Decrypting file using AES & CBC in Python
I'm having issues with my code. I am supposed to encrypt and decrypt a file using the Encryption and Decryption methods I have below. I can encrypt and decrypt a string just fine, but I'm having an issue encrypting a file. The problem is that it is not padding correctly. The padding is supposed to be 128 bits but it is outputting much more. I need help to fix this issue, as well as decrypting the file. My code is in bold. After is the sample output
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import padding
#Function to encrypt an arbitray message def Encryption(message, key): #Returns error is key is less than 32 bytes if len(key) < 32: return "Error. Key must be 32 bytes" #Encodes message to bytes message = bytes(message, 'utf-8') iv = os.urandom(16)
#sequence of instructions to pad the message if it less than 128 bits padder = padding.PKCS7(128).padder() padded_data = padder.update(message) + padder.finalize() message = padded_data #generating the cipertext cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) encryptor = cipher.encryptor() ciphertext =encryptor.update(message) + encryptor.finalize() #returning the ciphertext and iv return (ciphertext,iv)
#Function to decrypt an arbitray message def Decryption(ciphertext, iv, key): cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) #A decrypting CipherContext instance. decryptor = cipher.decryptor()
plaintext = decryptor.update(ciphertext) + decryptor.finalize() #unpadding the plaintext
try: unpadder = padding.PKCS7(128).unpadder() plaintext = unpadder.update(plaintext) + unpadder.finalize() return plaintext except: return plaintext
def EncryptFile(file): #32 byte key fileKey = os.urandom(32) #reads file as stream of bytes with open(file) as f: data = f.read() fileData = Encryption(data, fileKey) fileExtension = os.path.splitext(file)[1] fileData += (fileKey, fileExtension)
return fileData
def DecryptFile(file, iv, key, ext): with open(file, 'rb') as f: data = f.read() plaintext = Decryption(data, iv, key) return plaintext
#key holding 32 bytes key = os.urandom(32)
#Displaying an arbitrary message message = "Taylor" print("Original Message:", message, ' ')
#Displaying the encrypting the message print("Encrypted Message: ") ciphertext = Encryption(message, key) print(ciphertext, ' ')
#Displaying the original message (plaintext) print("Decrypted Message: ") plaintext = Decryption(ciphertext[0], ciphertext[1], key).decode('utf-8') print (plaintext, ' ')
#Encryprting file file_path = os.path.abspath("cecs378.txt") print("Encrypted File: ") ciphertext, iv, fileKey, fileExtension = EncryptFile(file_path) print(EncryptFile(file_path), ' ')
Output:
Original Message: SampleMessage
Encrypted Message: (b'\xc5|\x9b\x81O\xab\xe8ENh\x9c\x06#N\x8e=', b'\xff\x97\xefp\x19\x15\x1dP/8\xcd\xb6!\x8f(\xe7')
Decrypted Message: SampleMessage
Encrypted File: (b'\x08\x9b\x13\xbaOm\xb6y\xe4\x0f\x87Kw\x87\x95\x14', b'\x04\xe0\xf9$\xed\xac~&9\xc3\xf4\xf1\xb88\x82x', b'C?y\x89L\xf3IlB\xee\xbf{\x99\xe9\xc5\xb1\xdeh\xe2\x14\x1e\xd2@.\xea(]\xe0u\x8c\x95+', '.txt')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started