2

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 !

Julien
  • 699
  • 3
  • 14
  • 30
  • 2
    A hash is a one-way function, it cannot be decrypted. All you can do is to re-hash the plaintext and check that the hashes match. – rossum May 04 '22 at 13:20
  • 1
    AES encyrption is symmetric key algorithm, means same key can be use to decrypt the encrypted value. you need to have same key, not any random text as Key. – kus May 04 '22 at 19:22
  • Ok thanks for your answers ! Considering that this base64 text is the encrypted value and I have a key, how could I decrypt it ? – Julien May 05 '22 at 06:50

1 Answers1

2

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.

nCessity
  • 735
  • 7
  • 23
  • 1
    Thank you very much for this explanation that helped me a lot !! All what you wrote is very clear and it really makes me want to work and improve on all that stuff ! – Julien May 05 '22 at 14:54
  • Hey, I am new to this field. Can you tell me what do I pass in "key"? – Harshit verma Jan 13 '23 at 07:08
  • 1
    @Harshitverma for that particular example you can pass any (python) `bytes` object with a length of 128, 192, or 256 bits. If you want to learn what a "key" is in general, I would recommend to search and read about "symmetric encryption" on the internet. As a *very* rough explanation: A "key" is some *kind of password* for opening an encrypted message. In the case of *symmetric* encryption, both the sender and the recipient need to know the same password for encryption and decryption. – nCessity Jan 15 '23 at 22:15