如何使用NodeJS中的公钥加密数据?

tee*_*pap 11 encryption public-key-encryption node.js

在加密中,我只看到Signer/Verifier用于进行数字签名和密码/解密用对称密钥加密.

如何使用公钥加密数据?

msp*_*ars 9

对于那些在谷歌搜索时遇到这个问题的人来说,另一个问题是它已经回答了在node.js中使用Public Key加密数据的效果很好.


Jal*_*ini 5

var encrypted = crypto.publicEncrypt(publicKey, buffer);
Run Code Online (Sandbox Code Playgroud)

  • 尽管您的代码片段可能会解决问题,但您应该描述代码的用途(它如何解决问题)。此外,您可能需要查看 https://stackoverflow.com/help/how-to-answer (5认同)
  • 感谢您的回答!尽管此代码可以为问题提供答案,但建议对如何解决该问题进行解释。请使用有关此代码如何以及为什么解决此问题的解释来更新答案。 (4认同)
  • 虽然此代码片段可能是解决方案,但 [包括解释](//meta.stackexchange.com/questions/114762/explaining-entirely-‌ code-based-answers) 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而那些人可能不知道您提出代码建议的原因。 (3认同)
  • 不鼓励仅代码答案,因为它们不会为未来的读者提供太多信息,请对您所写的内容提供一些解释 (3认同)
  • 是否有一项研究将遵守情况与请求数量联系起来? (2认同)

the*_*ejh 2

您可能对我的NaCl 结合感兴趣。从它的 API 来看:

// Encrypt and sign
box(message, nonce, pubkey, privkey)

// Decrypt and validate
unbox(box, nonce, pubkey, privkey)
// Generates a new keypair, returns {private: <buffer>, public: <buffer>}
boxKeypair()

// Lengths of nonces and public and private keys in bytes
// { nonce: x, pubkey: x, privkey: x }
lengths.box
Run Code Online (Sandbox Code Playgroud)