0

Looking at JSBN, RSAGenerate(B,E) takes the bit length B and the public key E as arguments. Does that mean it the public key can only be generated separately and be provided as an argument? Does that also mean Forge can't generate the key pair like BigInt http://www.leemon.com/crypto/BigInt.html?

Thanks

Kar
  • 6,063
  • 7
  • 53
  • 82

1 Answers1

1

RSAGenerate doesn't take a public key, but a public key exponent in hex. Note that you have to choose this carefully because it has to be coprime to φ(n). A good value is 10001 (in hex) for compatibility with other implementations.

The public key can be created from the private key by setting n and e:

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

The forge docs contain three different examples how an RSA key pairs are generated with the same public exponent as above:

// 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);
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • But is it the user who has to choose `E` carefully? Doesn't the library generate an *n* s.t. `E` is coprime to φ(*n*)? – Kar Dec 13 '14 at 09:32
  • Yes, the user has to choose it, but it is generally set to 0x10001 for compatibility with other implementations. – Artjom B. Dec 13 '14 at 09:36
  • I see. So would it be safe to stick to 0x10001? – Kar Dec 13 '14 at 10:02
  • Yes, it is. See here for more information: http://security.stackexchange.com/questions/2335/should-rsa-public-exponent-be-only-in-3-5-17-257-or-65537-due-to-security-c note that 0x10001 is 65537 – Artjom B. Dec 13 '14 at 10:12