T
T
Ternick2022-03-24 00:36:29
Encryption
Ternick, 2022-03-24 00:36:29

How to decrypt and encrypt aes-128-cbc just like CryptoJS?

Encrypted like this:

CryptoJS.AES.encrypt('Plain Text', 'password').toString()

Result:
U2FsdGVkX1/lXDct6Gdc+rxKuhSvS6035ZYKeh1DLaY=

It's easy to decrypt using the same CryptoJS. The question is how to decrypt it using Crypto.Cipher.AES.
From my observations, the ciphertext has an interesting structure, in particular the prefix Salted__, and further, as far as I understand, n bytes of the hash go. And then comes the encrypted text itself.

My version:
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import unpad
from Crypto.Cipher import AES
from base64 import b64decode

#CBC mode with random IV

iv =  get_random_bytes(16)

def decrypt(enc, key, iv):
    enc = b64decode(enc)
    cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, b64decode(iv))
    return unpad(cipher.decrypt(enc), 16)


But the problem is that CryptoJS does not use alignment as far as I understand.

Just as interesting is the question of how to encrypt the text in the same way as CryptoJS.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question