In: Computer Science
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘encryptECB(key, secret_message)’ that accepts key and secret_message and returns a ciphertext.
#Electronic Code
Block AES algorithm, Encryption Implementation
from base64 import b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
secret_message = b" Please send me the fake passport..."
password = input ("Enter password to encrypt your message: ")
key= pad(password.encode(), 16)
cipher = AES.new(key, AES.MODE_ECB)
ct_bytes = cipher.encrypt(pad(secret_message,
AES.block_size))
ciphertext= b64encode(ct_bytes).decode('utf-8')
print(f"the encrypted message is {ciphertext}")
Solution: Code has been modified to create a function to encrypt. The code along with output has been provided below. Comments have been added to depict the code.
#Electronic Code Block AES algorithm, Encryption Implementation
from base64 import b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
#Function Definition
def encryptECB(key,secret_message):
cipher = AES.new(key, AES.MODE_ECB)
ct_bytes = cipher.encrypt(pad(secret_message, AES.block_size))
ciphertext= b64encode(ct_bytes).decode('utf-8')
return ciphertext
#Main program calling the Function to encrypt
if __name__ == "__main__":
secret_message = b" Please send me the fake passport..."
password = input ("Enter password to encrypt your message: ")
key= pad(password.encode(), 16)
ciphertext = encryptECB(key,secret_message)
print(f"the encrypted message is {ciphertext}")
Output
Enter password to encrypt your
message: pass
the encrypted message is
uDJPxZw89nelPt2jlNtizVO1JEmN3wut7NlWXskVU/xdODW0POX+rmAV3q+vsuh3