Question

In: Computer Science

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

Solutions

Expert Solution

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


Related Solutions

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...
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