Question

In: Computer Science

4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify...

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")

Solutions

Expert Solution


# 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")

Related Solutions

Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the...
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...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
Modify the Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase...
Modify the Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9 to 8, 8 to...
What are the common key and block sizes used by AES? Decrypt the following message. What...
What are the common key and block sizes used by AES? Decrypt the following message. What is the message? What is the cipher? What is the key? Who is the author? ZW ERKLIV YRJ DRUV REP FEV KYZEX CVJJ JLJTVGKZSCV KYRE RCC FKYVIJ FW VOTCLJZMV GIFGVIKP, ZK ZJ KYV RTKZFE FW KYV KYZEBZEX GFNVI TRCCVU RE ZUVR, NYZTY RE ZEUZMZULRC DRP VOTCLJZMVCP GFJJVJJ RJ CFEX RJ YV BVVGJ ZK KF YZDJVCW; SLK KYV DFDVEK ZK ZJ UZMLCXVU, ZK WFITVJ ZKJVCW...
// If you modify any of the given code, the return types, or the parameters, you...
// If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // You are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of employees struct employeeList {    struct employee* employee;    struct employeeList* next; } *list = NULL;              ...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
Why is the Cipher Block Chaining (CBC) mode of operation considered preferable to the Electronic Code...
Why is the Cipher Block Chaining (CBC) mode of operation considered preferable to the Electronic Code Book (ECB) mode? Is it possible to perform encryption operations in parallel on multiple blocks of plaintext in the CBC mode? How about decryption?
Why is the Cipher Block Chaining (CBC) mode of operation considered preferable to the Electronic Code...
Why is the Cipher Block Chaining (CBC) mode of operation considered preferable to the Electronic Code Book (ECB) mode? Is it possible to perform encryption operations in parallel on multiple blocks of plaintext in the CBC mode? How about decryption?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT