将AES键与Node.js Crypto库中的RSA包装

发布于 2025-01-27 17:48:46 字数 1108 浏览 2 评论 0原文

我正在创建一个随机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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文