I have been using the Crypto lib in Node for doing Pub Key exchange. So far I have just been using .getDiffieHellman('modp5')
generate a new public / private key for each new connection. This methodology works great for computing a secret for use with AES encryption.
However, I would like to be able to save a Private Key into a file, and then Load it upon next execution. It seems like the way to do this would be .getPrivateKey()
and then .setPrivateKey()
but using .setPrivateKey()
on a crypto.createDiffieHellman
class generated by .getDiffieHellman('modp5')
does not work. This is actually stated in the documentation:
The returned object mimics the interface of objects created by crypto.createDiffieHellman() above, but will not allow to change the keys (with diffieHellman.setPublicKey() for example).
Knowing this, if I have generated a Private Key using the following code:
var crypto = require('crypto');
var Key = crypto.getDiffieHellman('modp5');
Key.generateKeys();
var PrvKey = Key.getPrivateKey();
save_to_file(PrvKey);
How do I go about loading that same Private Key at a later date?
var crypto = require('crypto');
var PrvKey = load_from_file();
var Key = crypto.createDiffieHellman(prime, [encoding]);
Key.setPrivateKey( PrvKey )
I looked at the RFC2412 that was specified in the crypto documentation and found that the prime for group 5 is listed as:
2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804}
241031242692103258855207602219756607485695054850245994265411 694195810883168261222889009385826134161467322714147790401219 650364895705058263194273070680500922306273474534107340669624 601458936165977404102716924945320037872943417032584377865919 814376319377685986952408894019557734611984354530154704374720 774996976375008430892633929555996888245787241299381012913029 459299994792636526405928464720973038494721168143446471443848 8520940127459844288859336526896320919633919
FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1 29024E088A67CC74020BBEA63B139B22514A08798E3404DD EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245 E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F 83655D23DCA3AD961C62F356208552BB9ED529077096966D 670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF
I attempted to take this prime and put it into .createDiffieHellman(prime, [encoding])
. However, after doing so, .setPrivateKey()
complained about an incorrect key length.
Any insight would be appreciated.