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

回眸只為那壹抹淺笑 提交于 2020-01-01 06:29:18

问题


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 Node.js's standard libraries?


回答1:


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




回答2:


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.




回答3:


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')



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/5345719/is-aes-the-same-in-libraries-pycrypto-node-js-crypto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!