在节点上生成AES密钥 [英] Generate AES key on node
问题描述
我正在处理一个使用自定义协议来加密通信的旧版应用程序。像这样在旧版Java应用程序中生成随机AES密钥:
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();
我一直在密码,没有运气。我该如何在nodejs上生成此密钥?
I've been looking for solutions on crypto with no luck. How can I generate this key on nodejs?
推荐答案
该代码可能没有您想象的那么多。它只是生成16个(128/8)安全随机字节,然后在其周围包装一个密钥对象。因此,使用nodejs,您只需生成16个字节并向算法提供原始密钥数据即可。
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.
如果要使用生成的密钥,请确保创建一个二进制编码 getEncoded()
方法返回的字节中的字符串或缓冲区。如果您要求密钥必须是过程中某个地方的文本字符串,则可以使用十六进制编码/解码。
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.
请参见 randomBytes()
和 createCipheriv()
获取信息。
See randomBytes()
and createCipheriv()
for information.
AES密钥只是具有加密强度的随机字节,而DES(奇偶校验位)和RSA(质数计算)密钥不是。
AES keys are just cryptographically strong random bytes, DES (parity bits) and RSA (prime number calculation) keys are not.
这篇关于在节点上生成AES密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!