将AES键与Node.js Crypto库中的RSA包装
我正在创建一个随机AES密钥,用于使用节点crypto
库来加密数据包,如下所示:
const createKey = () => {
return new Promise((resolve, reject) => {
crypto.generateKey("aes", {length: 256}, (err, key) => {
if (err) {
reject(err);
}
resolve(key.export().toString());
});
});
};
然后,我使用此键使用crypto.createciperiv
加密数据包。
//gets an AES key for this encryption
const secretKey = await createKey();
//encrypt data packet using AES key
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
最后,我想将AES密钥包装在将发送到服务器的RSA公共密钥中,因此数据在静止时进行加密。
我的问题是,我看不到使用node.js crypto
库将AES密钥用RSA键包裹的方法,以便在发送之前附加到数据。我知道使用subtleCrypto
类中的一种方式存在浏览器中的HTTPS上下文中的类,但是我希望使用Node crypto
。是否有一种方法可以包装AES密钥并使用Node crypto
将加密的字符串/数据附加到发送到服务器的数据包,或者我需要重写我的代码以使用所有subtleCrypto ?
I am creating a random AES key for encrypting a data packet using the Node Crypto
library like below:
const createKey = () => {
return new Promise((resolve, reject) => {
crypto.generateKey("aes", {length: 256}, (err, key) => {
if (err) {
reject(err);
}
resolve(key.export().toString());
});
});
};
I then use this key to encrypt a data packet using Crypto.createCiperiv
.
//gets an AES key for this encryption
const secretKey = await createKey();
//encrypt data packet using AES key
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
Finally, I want to wrap the AES key in an RSA public key to be sent to a server, so the data is encrypted at rest.
My problem is that I do not see a way to wrap the AES key with an RSA key using the Node.js Crypto
library to append to the data before being sent. I know a way exists using the SubtleCrypto
class available in https context in the browser, but I was hoping to use Node Crypto
. Is there a way to wrap the AES key and append the encrypted string/data to the packet sent to the server using Node Crypto
or do I need to rewrite my code to use all SubtleCrypto
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论