In: Computer Science
Cryptography - please use Python to complete the following task. You don't have to implement AES just import the necessary libraries. Can you fix my code? Thanks!
Here is my code so far:
# Import implementation of the AES encryption
import pyaes
import binascii
import base64
# Convert output to hex format
def getHexFormat(input):
    input = input.upper()
    temp = ""
    for count in range(0, len(input)):
     temp = temp + input[count]
    if count%2 == 1:
     temp = temp + " "
    return temp
# Encryption function
def encrypt():
    # Prompt user input - key and message
    key = input("Please enter a 16 character key: ")
    if (len(str(key)) != 16):
        print("Invalid input, try again.")
        return()
    else:
        keyCipher = bytes(key, 'utf-8')
        keyHex = keyCipher.hex()
        message = input("Please enter a message: ")
        IV = os.urandom(16)
        binascii.hexlify(IV).upper()
        firstEleObj = AES.new(key, AES.MODE_CBC, IV = IV)
        messageCipher = bytes(message, 'utf-8')
        messageHex = messageCipher.hex()
        padded = messageCipher.rjust(80)
        data = firstEleObj.encrypt(padded)
        return(data)
# Print results in hexadecimal format
print("Key in hex:",getHexFormat(keyHex))
print("Message in hex:", getHexFormat(messageHex))
print("Encrypted hex:" , getHexFormat(data.hex()))
Changes.
1. the AES.new function is depricated will need to use pyaes.AESModeOfOperationCBC(key,VI)
2. The AESModeOfOperationCBC(key,VI) function only accepts a 16bit plaintext. to solve this we use a feed().
3. The AESModeOfOperationCBC function accepts key and VI as bytes and not Hex. all the lines not needed are commented out.
4. added length check for message.
5. have also properly indented the getHexFormat()
6. In your main code there was another error at the end you were not calling the encrypt() function.
# my changes 
# Import implementation of the AES encryption
from pyaes  import AES
import binascii
import base64
import os
# Convert output to hex format
def getHexFormat(input):
    input = input.upper()
    temp = ""
    for count in range(0, len(input)):
         temp = temp + input[count]
         if count%2 == 1:
             temp = temp + " "
    return temp
# Encryption function
def encrypt():
    # Prompt user input - key and message
    key = input("Please enter a 16 character key: ")
    
    if (len(key) != 16):
        print("Invalid input, try again.")
        return()
    else:
        
        keyCipher = bytes(key, 'utf-8')
        
        #  keyHex = keyCipher.hex() # no need to convert to hex the aes function accepts  bytes
        
        
        message = input("Please enter a message: ")
        
        
        #message must be from 16 to 50000 , otherwise the aes algo. gives error
        
        if len(message) < 16 or len(message) > 50000:
        
             print("Invalid message length, must be 16 - 50,000 chars long... try again.")
             return 
            
        
        
        IV = os.urandom(16)
        
#         IV = binascii.hexlify(IV).upper() no need to conver to hex.
        
        firstEleObj = pyaes.Encrypter(pyaes.AESModeOfOperationCBC(keyCipher, iv = IV))
        
        
        ciphertext = firstEleObj.feed (message)  
        
        ciphertext += firstEleObj.feed()
        
#         messageCipher = bytes(message, 'utf-8') no need to convert 
        
#         messageHex = messageCipher.hex() 
#         padded = messageCipher.rjust(80)
        
        
        
        # converting data to hex optional 
        
        ciphertext = ciphertext.hex()
        
        
        data = getHexFormat(ciphertext)
        
        return data
    
    
ciphertext = encrypt()    
print(ciphertext)
    
   
Output : I have not changed the logic getHexFormat function.

If you find this useful then please upVote .
********************************************end**********************************