如何在node.js中使用带有DiffieHellman私钥的crypto.createSign?

时间:2014-08-18 03:36:26

标签: node.js cryptography sign diffie-hellman

我尝试使用DH私钥与节点crypto lib签署消息。我遇到了一个我似乎无法解决的错误:{

var crypto = require("crypto");

var bob = crypto.getDiffieHellman("modp17");

bob.generateKeys();

var sign = crypto.createSign("RSA-SHA256");

sign.write("hello world");

var message = sign.sign(bob.getPrivateKey());

错误

140735140705040:error:0906D06C:PEM routines:PEM_read_bio:no start line:../deps/openssl/openssl/crypto/pem/pem_lib.c:703:Expecting: ANY PRIVATE KEY
Error: SignFinal error
    at Sign.sign (crypto.js:398:27)
    at repl:1:18
    at REPLServer.self.eval (repl.js:110:21)
    at repl.js:249:20
    at REPLServer.self.eval (repl.js:122:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)

2 个答案:

答案 0 :(得分:1)

您从getPrivateKey()获得的私钥不是PEM格式,即what sign() is expecting

答案 1 :(得分:0)

您可以在此链接中按照@mscdex的答案将私钥设置为PEM格式:

Unable to sign a file with nodejs crypto

您需要asn1.jsbn.js模块。

我希望这可以帮助别人。