Using RSA with Node.js
We're finally ready to start writing some code to use public-key cryptography with Node.js!
In this section, we're going to learn how to generate RSA key pairs in Node.js and encrypt and decrypt messages with RSA. We'll then look at how to create a hybrid scheme based on RSA and a symmetric cipher such as AES to encrypt messages of any length.
Generating an RSA key pair
You can generate an RSA key pair with Node.js using the crypto.generateKeyPair
function, as shown in this example:
5.1: Generate an RSA key pair (rsa-gen-keypair.js)
const crypto = require('crypto') const fs = require('fs') const util = require('util') const generateKeyPair = util.promisify(crypto.generateKeyPair) const writeFile = util.promisify(fs.writeFile) ;(async function() { const keyPair = await generateKeyPair('rsa', { modulusLength: 4096, ...