Generate AES key on node

荒凉一梦 提交于 2019-12-24 04:16:08

问题


I'm dealing with a legacy application that uses a custom protocol to cipher communication. Random AES keys are generated in legacy Java app like this:

keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
keygen.generateKey().getEncoded();

I've been looking for solutions on crypto with no luck. How can I generate this key on nodejs?


回答1:


That code probably does not do as much as you think. It simply generates 16 (128 / 8) secure random bytes, then wraps a key object around it. So with nodejs, you simply generate 16 bytes and feed the algorithm the raw key data.

If you want to use the generated key, then make sure you create a binary encoded string or buffer from the bytes returned by the getEncoded() method. You could use hexadecimal encoding/decoding if you require the key to be a textual string somewhere in the process.

See randomBytes() and createCipheriv() for information.

AES keys are just cryptographically strong random bytes, DES (parity bits) and RSA (prime number calculation) keys are not.



来源:https://stackoverflow.com/questions/21367907/generate-aes-key-on-node

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