How to generate RSA key pair in Node.JS?

Very straight forward process. Not complicated at all, I would say that the most difficult part would be to actually tweak the options, however the code below actually works and provides you the time it took to generate the key pair as well, just for informational purposes.

Run the following code in Node.js.

const crypto = require('crypto');

const RSA = 'rsa';

const passphrase = 'I had learned that some things are best kept secret.';

let options = {

    modulusLength: 1024 * 2,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
    },
    privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: passphrase
    }

};

let start = Date.now();

let myCallback = (err, publicKey, privateKey) => {

    if (!err) {

        console.log('\n');
        console.log(publicKey);
        console.log(privateKey);

        let end = Date.now();
        console.log("\n> Process completed successfully in " + (end - start) + " milliseconds.");

    } else {
        throw err;

    }

};

crypto.generateKeyPair(RSA, options, myCallback);
Your console output should be similar to this:
Z:\Code\NodeJS\generateRSAKeyPair>node generateRSAKeyPair.js


-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArZUXyCfBOiIIJxhvw+gg
GOqxlsvMBpMzdRO52pQkEVmnmvlCJ0VDab0Vkg8f4mTmN6VtFIB53PBHX8a0SqR6
d5xEeklJCL49Qk3SwTHnfSVIXC8MLcR7cCSGq9J28BgG7xqGJqRU3V0Zg1YCUMal
EKIn8ykT5JlwJsdo8fAa4mx0aUYrpl8lhGx6U3Lu8IHgbFnqiBGGeDyOZwkB3wN4
5t20bbinqegin68LHwjTWph+xQrJ1vGNaM9354cPgQ1Dq5V8w9KTVQ4lSLQC8+24
thlaHkLlS/ZdaNWUhkBmBlA7Q8DMAc5diVHhtPWlymOxRd5uavO+Uo0jWZYSLqWA
hQIDAQAB
-----END PUBLIC KEY-----

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFLTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIaszMX1zAxX8CAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEq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dRDc8F2/8ILy4XDwC2niyWZbd2gAH8
wWAeEOoskjVdI5gqt4OR3MoPKBfwKqe0Q3+Za+UAnui0l7IlzKW640cnSeAPSrgZ
aEWCDtBCXR1wU7tGj8S+L86xtJz5f7ZwJ/IqL9eujIq/mPWtlTC2i2uIv0dS2aP8
wD3yDUz5wfkhKu36PEcYCK3gkuRJzxdZ6XZ++yYjCtkaXYdbHHIGbGkeNCycugKv
+3Ram1YpA8T5JWKOk/YWDwT9VcizKL84MGI6CPU4+KH89aETtSeifc7JHJ8WiiGj
AfPfQlNMHBAvd5rdA156XNgQBffse6229Jf3K94qpQ253up2LiLiwBdg740ZZUuP
9KgINeRBEX49vJiq1znOFgjPkER4UoC+k1/k3CIDju/jZ5T7QHiYXLWXzEFirTg4
N2Ttkf5J9BrhRthAI0iFrTs2/Md8jtlqDcQqi7GpiScx6OUyFxin7QtUGaqxAgRh
ERBOuktZKcSkEUkg1Z6PiAvcoL4VjleC3wbEcFvk88aKpUTm4Y35wDoHeehdzIWG
gWBFkWLlVjMGrZ0GAhNgEVmdj8x4XPuBrUdp4UXv5vqb
-----END ENCRYPTED PRIVATE KEY-----


> Process completed successfully in 255 milliseconds.

Z:\Code\NodeJS\generateRSAKeyPair>

Note that this same code can be used to generate a key pair with DSA or EC instead of RSA but that is coming in another post.

That’s it.

Sources:

  • https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback
  • https://nodejs.org/api/crypto.html#crypto_keyobject_export_options
  • https://stackoverflow.com/questions/8520973/how-to-create-a-pair-private-public-keys-using-node-js-crypto

Leave a Comment

Your email address will not be published. Required fields are marked *