In: Computer Science
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘decryptECB(key, ciphertext)’ that accepts key and ciphertext and returns a plaintext.
from base64 import b64decode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Util.Padding import unpad
# We assume that the password was securely shared beforehand
password = input ("Enter the password to decrypt the message: ")
key= pad(password.encode(), 16)
ciphertext = input ("Paste the cipher text here: ")
try:
ct = b64decode(ciphertext)
cipher = AES.new(key, AES.MODE_ECB)
pt = unpad(cipher.decrypt(ct), AES.block_size)
print("The decrypted message was: ", pt)
except ValueError:
print("Incorrect decryption")
# Please install 'pycryptodome' before running this script by using the below command
# pip install pycryptodome
from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES
def encryptECB(_key, _msg):
cipher = AES.new(_key, AES.MODE_ECB)
ct = cipher.encrypt(_msg)
return ct
def decryptECB(_key, _ciphertext):
decipher = AES.new(_key, AES.MODE_ECB)
pt = decipher.decrypt(_ciphertext)
pt = unpad(pt, 16)
return pt
password = input("Enter the password to decrypt the message: ")
key = pad(password.encode('utf8'), 16)
msg = input("Paste the Message here: ")
msg = pad(msg.encode('utf8'), 16)
try:
# encrypting message
ciphertext = encryptECB(key, msg)
# decrypting message
plaintext = decryptECB(key, ciphertext)
# print decrypted message
print(plaintext)
except ValueError:
print("Incorrect decryption")