I'm working with Python and the Pycryptodome library.
I have a string beginning with 951bd9...[and so on]
.
Let's suppose I have a key"ThisIsARandomText"
.
My question is : could I decrypt that string with an AES algorithm and that key ?
I think I don't get it all and I would be pleased to have some explanations to improve.
Thanks you very much for your help !
I see two problems here: First, if you want to use pycryptodome, you need to decode the Base64 string for the processing.
The second one is more complex. You know that the base64 string is being encrypted with an AES. But it is also necessary to know (or to guess) the block cipher mode of operation. Depending on the mode, you might also need a nonce for the decryption. In other cases, it is necessary to know the initialization vector.
As you don't provide a nonce here, I give you an example for pycryptodome with a base64-encoded message using an OpenPGP mode. Luckily, OpenPGP stores the encrypted IV into the first 18 bytes of the encrypted message.
import base64
from Crypto.Cipher import AES
input_data = b'This is secret message'
key = b'Sixteen byte key'
## ENCRYPTION
encryption_cipher = AES.new(key, AES.MODE_OPENPGP)
# use a nonce, e.g when the mode is AES.MODE_EAX
#nonce = encryption_cipher.nonce
ciphertext = encryption_cipher.encrypt(input_data)
b64_ciphertext = base64.b64encode(ciphertext).decode()
print("Base64 of AES-encrypted message: ", b64_ciphertext)
## DECRYPTION
unb64_ciphertext = base64.b64decode(b64_ciphertext.encode())
iv = unb64_ciphertext[0:18]
unb64_ciphertext = unb64_ciphertext[18:]
decryption_cipher = AES.new(key, AES.MODE_OPENPGP, iv=iv)#, nonce=nonce)
output_data = decryption_cipher.decrypt(unb64_ciphertext)
print("Decrypted message: ", output_data)
This code was taken from the pycryptodome docs. I adapted it to your use case and included the handling of the Base64 input string.