可以使用JSBN / Forge生成RSA私钥/公钥对吗?

时间:2014-12-13 08:52:31

标签: javascript encryption rsa jsbn

查看JSBN,RSAGenerate(B,E)将位长B和公钥E作为参数。这是否意味着公钥只能单独生成并作为参数提供?这是否也意味着Forge无法生成像BigInt http://www.leemon.com/crypto/BigInt.html那样的密钥对?

由于

1 个答案:

答案 0 :(得分:1)

RSAGenerate不使用公钥,而是使用十六进制的公钥指数。请注意,您必须谨慎选择,因为它必须是φ( n )的互质。一个好的值是10001(十六进制),以便与其他实现兼容。

可以通过设置 n e 从私钥创建公钥:

var pubkey = new RSAKey();
pubkey.n = privKey.n;
pubkey.e = privKey.e;

forge docs包含三个不同的示例,说明如何使用与上述相同的公共指数生成RSA密钥对:

// generate an RSA key pair synchronously
var keypair = rsa.generateKeyPair({bits: 2048, e: 0x10001});
// generate an RSA key pair asynchronously (uses web workers if available)
// use workers: -1 to run a fast core estimator to optimize # of workers
rsa.generateKeyPair({bits: 2048, workers: 2}, function(err, keypair) {
  // keypair.privateKey, keypair.publicKey
});
// generate an RSA key pair in steps that attempt to run for a specified period
// of time on the main JS thread
var state = rsa.createKeyPairGenerationState(2048, 0x10001);
var step = function() {
  // run for 100 ms
  if(!rsa.stepKeyPairGenerationState(state, 100)) {
    setTimeout(step, 1);
  }
  else {
    // done, turn off progress indicator, use state.keys
  }
};
// turn on progress indicator, schedule generation to run
setTimeout(step);