Question: Python encryption using EBC and CBC. Use the sample code below to encrypt an image called test.bmp using EBC, then use the sample code again
Python encryption using EBC and CBC. Use the sample code below to encrypt an image called "test.bmp" using EBC, then use the sample code again to encrypt the image using CBC.
def encrypt_bmp(key, iv, mode, in_filename, out_filename=None, chunksize=1024): if not out_filename: out_filename = in_filename + '.enc' if mode == AES.MODE_ECB: # no iv is needed for ECB! encryptor = AES.new(key, AES.MODE_ECB) elif mode == AES.MODE_CBC: encryptor = AES.new(key, AES.MODE_CBC, iv) else: print("invalid mode, program stopped") return with open(in_filename, 'rb') as infile: # extract bmp header header = infile.read(54) # open output file with open(out_filename, 'wb') as outfile: outfile.write(header) while True: chunk = infile.read(chunksize) if len(chunk) == 0: break elif len(chunk) % 16 != 0: # #pad the last block with spaces chunk += ' ' * (16 len(chunk) % 16) outfile.write(encryptor.encrypt(chunk)) Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
