Im trying to decrypt with Pycrypto an encrypted text with CryptoJS, and I would like to know how is generated the by default IV value from CryptoJS?An example code solving the decryption would be great.
This is the decryption code with JS:
var crypto_test = require('crypto-js');
var bytes = crypto_test.AES.decrypt(encrypted_text, 'test_key');
This is the equivalent Python version that Im trying to create:
BS = 32
key = 'test_key'
dkey = hashlib.sha256(key.encode()).digest()
encodedEncrypted = base64.b64decode(encrypted_text)
#block_size is 16
cipher = AES.new(dkey, AES.MODE_CBC, encodedEncrypted[:AES.block_size])
decrypted = _unpad(cipher.decrypt((encodedEncrypted[AES.block_size:])).decode('utf-8')
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
From the documentation of CryptoJS I found:
- If a passphrase has been used instead of a key then It will use AES 256 by default.
- The mode is CBC.
- The padding is PKCS7.
However Pycrypto by default is forcing me to use a 16 bytes IV not a 32 as the documentation from CryptoJS proposed..so Im a bit confused on the correct way to do the decryption here.. Also I saw in some documentation that CryptoJS applies to sha256 to passphrase..but still not sure about that ..
That code will not crash but will give an empty text..