简体   繁体   English

Node.js和Webcrypto之间的RSA加密

[英]RSA crypto between Node.js and webcrypto

I'm really stuck on this! 我真的很坚持! What i'm trying to do: 我正在尝试做的是:

  • Generate a RSA keypair using ursa/webcrypto 使用ursa / webcrypto生成RSA密钥对
  • Send the public key as PEM over the wire 通过电线将公钥作为PEM发送
  • 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 发回密文,并使用私钥在另一端将其解密

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: 现在,如果尝试使用ursa进行加密部分,或者以某种方式在浏览器中得到未定义的DOMException,或者在浏览器中进行加密时在Node.js中得到此错误:

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)

I made a minimal code example at https://github.com/jvanveen/crypto-nodebrowser that shows the issue. 我在https://github.com/jvanveen/crypto-nodebrowser上做了一个最小的代码示例,以显示该问题。 Any help on this is welcome! 欢迎对此提供任何帮助!

Are you able to tell us both the code you are running and the browser it is running in? 您能否同时告诉我们您正在运行的代码和正在运行的浏览器?

Some browsers may only do RSA-OAEP with SHA-1 but from the brief description you have given it is having issues with the decryption. 某些浏览器可能仅对SHA-1执行RSA-OAEP,但从您提供的简短描述来看,解密存在问题。 I think more details are needed. 我认为需要更多细节。

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. Openssl的OAEP填充使用SHA1进行了硬编码,因此,如果您想使用RSA-OAEP-256,则ursa的openssl绑定不适合。 I couldn't find a way to make this work with node-rsa, but Node-forge is quite straightforward here. 我找不到使它与node-rsa一起工作的方法,但是Node-forge在这里非常简单。 Just use something like: 只需使用类似:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM