如何在 Node.js 中使用公钥加密数据?
在加密中,我只看到用于进行数字签名的签名者/验证者和使用对称密钥加密的密码/解密。
如何使用公钥加密数据?
In crypto, I see only Signer/Verifier for doing digital signature and Cipher/Decipher with symmetric key encryption.
How do I encrypt data with public key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如官方 Nodejs api 文档中所述:
crypto.publicEncrypt(key, buffer)
所以答案是:
As mentioned in the official nodejs api docs here:
crypto.publicEncrypt(key, buffer)
So the answer is:
您可能对我的NaCl 绑定感兴趣。从它的 API 来看:
You might be interested in my NaCl bindings. From its API:
另一种方法是使用加密消息语法 (CMS)。它不是一个纯粹的 Node.js 解决方案,但您可能已经拥有所需的所有工具。下面是使用 OpenSSL 的示例:
生成 x509 证书(收件人)和私钥文件(在 Bash 中):
从标准输入加密/解密消息(在 Bash 中):
您可以使用 -in/-out 参数来处理文件。以下是可用于 Node.js 的示例:
在 Linux 上,您可能已经安装了 OpenSSL。您可以通过安装 Git Bash 在 Windows 上获取 OpenSSL,尽管您可以还可以使用内置的 PowerShell 命令。您需要生成 PFX 证书(使用New- SelfSignedCertificate)或安装现有证书(也可以使用 OpenSSL 生成)。将证书安装到证书存储中后,您可以使用以下命令进行加密/解密:
下面是如何使用 OpenSSL 从同一私钥生成 .pem 和 PFX 证书的示例,并使消息在 OpenSSL 和 PowerShell 之间可互换。
生成带有扩展名的证书(Windows 上需要):
上面的代码片段仅适用于较新版本的 OpenSSL (1.1.1)。否则,您需要一个单独的文件来定义扩展。然后生成 PFX 证书(使用一些密码保护它):
然后将该 PFX 文件复制到您的 Windows 计算机。您应该能够通过 PowerShell (Import-PfxCertificate) 或手动安装(单击它并按照向导操作,使用所有默认值)。为了使消息可互换,请在使用 OpenSSL 时使用
-inform \ -outform
参数。例如:顺便说一句,CmsMessage 命令将在 PowerShell Core 7.1 上可用,所以你也可以在 Linux/Mac 上使用它(现在处于预览状态,稳定版本将于 2020 年 12 月发布)。
Yet another approach is using Cryptographic Message Syntax (CMS). It's not a pure Node.js solution, but you likely have all tools you need in the box. Below is the example using OpenSSL:
Generate x509 certificate (recipient) and private key files (in Bash):
Encrypt/Decrypt message from standard input (in Bash):
You can use -in/-out parameters to work with files. Below is an example you can use for Node.js:
On Linux you'll likely have OpenSSL installed already. You can get OpenSSL on Windows by installing Git Bash, although you can also use built-in PowerShell commands. You'll need to generate a PFX certificate (using New-SelfSignedCertificate) or install existing one (can be generated with OpenSSL too). Once the certificate installed in the certificate store, you can use below commands for encryption/decryption:
Below is an example how to generate .pem and PFX certificates from the same private key using OpenSSL, and make messages interchangeable between OpenSSL and PowerShell.
Generate certificate with extensions (that's required on Windows):
The above snippet will work only for newer versions of OpenSSL (1.1.1). Otherwise you need a separate file to define extensions. Then generate a PFX certificate (protect it with some password):
Then copy that PFX file to your Windows machine. You should be able to install it via PowerShell (Import-PfxCertificate) or manually (click on it and follow wizard, use all defaults). In order to make messages interchangeable use the
-inform \ -outform
parameter when using OpenSSL. For example:Btw, the CmsMessage commands will be available on PowerShell Core 7.1, so you can use it on Linux/Mac too (it's in preview now, and a stable version will be released in Dec 2020).