Node.js和Webcrypto之间的RSA加密 [英] RSA crypto between Node.js and webcrypto

查看:273
本文介绍了Node.js和Webcrypto之间的RSA加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很坚持!我正在尝试做的事情:

I'm really stuck on this! What i'm trying to do:


  • 使用ursa / webcrypto生成RSA密钥对

  • 发送公钥以PEM方式通过电线

  • 在另一端导入公钥,并用其加密一些文本

  • 发回密文,然后使用私钥在另一端解密它。

  • Generate a RSA keypair using ursa/webcrypto
  • Send the public key as PEM over the wire
  • Import the public key on the other end, and encrypt some text with it
  • Send back the ciphertext and decrypt it on the other end using the private key

现在,如果尝试执行以下操作,我会在浏览器中收到未定义的DOMException异常用ursa加密部分,或者在浏览器中进行加密时在Node.js中收到此错误:

Now somehow I get either an undefined DOMException in the browser if I try to do the encryption part with ursa, or I get this error in Node.js when I do the encryption in the browser:

Missing error handler on `socket`.
Error: error:040A1079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
    at Error (native)
    at Object.decrypt (/home/jeroen/projects/crypto-nodebrowser/node_modules/ursa/lib/ursa.js:358:33)
    at Socket.<anonymous> (/home/jeroen/projects/crypto-nodebrowser/server.js:34:50)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at Socket.onevent (/home/jeroen/projects/crypto-nodebrowser/node_modules/socket.io/lib/socket.js:330:8)
    at Socket.onpacket (/home/jeroen/projects/crypto-nodebrowser/node_modules/socket.io/lib/socket.js:290:12)
    at Client.ondecoded (/home/jeroen/projects/crypto-nodebrowser/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/home/jeroen/projects/crypto-nodebrowser/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/home/jeroen/projects/crypto-nodebrowser/node_modules/socket.io-parser/index.js:247:12)

我在 https://github.com上做了一个最小的代码示例/ jvanveen / crypto-nodebrowser 显示了问题。欢迎对此提供任何帮助!

I made a minimal code example at https://github.com/jvanveen/crypto-nodebrowser that shows the issue. Any help on this is welcome!

推荐答案

好,我终于明白了。 Openssl的OAEP填充使用SHA1进行了硬编码,因此,如果您想使用RSA-OAEP-256,则ursa的openssl绑定不适合。我无法找到一种通过node-rsa进行此工作的方法,但是Node-forge在这里非常简单。只需使用以下内容即可:

Ok, I finally got it. Openssl has OAEP padding hardcoded using SHA1, so ursa's openssl bindings are not suitable if you want to use RSA-OAEP-256. I couldn't find a way to make this work with node-rsa, but Node-forge is quite straightforward here. Just use something like:

var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP', {
    md: forge.md.sha256.create()
});

这篇关于Node.js和Webcrypto之间的RSA加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆