开发者

Is AES the same in libraries PyCrypto & Node.JS Crypto

开发者 https://www.devze.com 2023-02-17 18:20 出处:网络
I am beginnging to wonder if the implementation of AES is different across libraries.. Currently i have a plaintext encrypted with PyCrypto.

I am beginnging to wonder if the implementation of AES is different across libraries..

Currently i have a plaintext encrypted with PyCrypto.

Im trying to decrypt the ciphertext with Node.js's Crypto Library..

Basically with PyCrypto..

im using AES-128-CBC with a random generated IV. (which decrypts perfectly in PyCrypto)

However..

On Node.js im doing this

var buf = new Buffer(ciphertext)
var decipher = crypto.createDecipher('aes-128-cbc',aeskey)
buf = decipher.update(buf,'binary', 'binary')
buf += decipher.final('binary')

Which spits out a bunch of Garbage.... ( changing 'binary' to hex/utf8 doesnt help)

As i am using CBC (Cipher Block Chaining)...

i am prepending the IV to the beginning of the ciphertext (16 blocks).. In PyCrypto this works perfectly, similarly to the specification of PGP, CFB usage..

Does anyone know for what reason this is not working???

Am i expecting too much of No开发者_如何学Gode.js's standard libraries?


Documentation does not mention this, but aeskey you're passing to crypto.createDecipher is not the key, but a password, handled to OpenSSL's EVP_BytesToKey function.

To pass the actual raw key data one should use (presently undocumented) crypto.createDecipheriv(cipher, key, iv) function. This applies to ECB mode too, even though there's no IV in ECB.

If this fails, I think, the first step in debugging would be to try with AES KATs to see whenever the decryption code is correct.

I've tripped on a similar issue here: https://github.com/joyent/node/issues/1318


AES is a rijndael standard. It shouldn't be different. You should look into data types and default settings that are hidden. Something must be set different between the two. The key sizes might be different as 128 bit "hello" is padded with zeros I think and a smaller key would start with "hello" but have a smaller padding, therefore different.


The short answer to your question is: Yes, AES is the same in PyCrypto and Node.js' crypto module. Node's crypto is just a wrapper around openssl on your system, and PyCrypto is interoperable with OpenSSL (see http://lists.dlitz.net/pipermail/pycrypto/2010q4/000301.html).

Having said that, there are definitely bugs in the Node crypto module (though I've only experienced problems with base64 encoding, myself). So whether it's a bug or not, the problems you're experiencing are almost certainly happening in the data encoding/decoding stages.

What does your ciphertext look like? Is it a hexadecimal string? If so, then you need to do

buf = decipher.update(buf, 'hex', 'binary')


That's not how IV works in Node, you have to use crypto.createDecipheriv(cipher, key, iv) instead, otherwise you get a default baked-in one. Even in PyCrypto you should be using the third argument to AES.new as the IV, not stuffing it into the bytestream.


Make sure you use the same key and IV in both pycrypto and node.js!! Not only that, but make sure you have the same encoding in both ends:

cipher = AES.new(key.decode('hex'), AES.MODE_CBC, iv.decode('hex'))
text = json.dumps(payload)  
pad = lambda s: s + (16 - len(s) % 16) * '\x07'     
encryptedText = base64.b64encode(cipher.encrypt(pad(text)))

Then in node.js (sorry, no easy access to that code now), also make sure you decode your key and iv to hex

0

精彩评论

暂无评论...
验证码 换一张
取 消