Node.js - RSA Encryption And Decryption Example
The RSA algorithm (Rivest-Shamir-Adleman) is a cryptographic algorithm that is used for specific security services or purposes, which enables public-key encryption and is widely used to secure sensitive data, particularly when it is being sent over an insecure network such as the HTTP. A public key is shared publicly, while a private key is secret and must not be shared with anyone. The following illustration highlights how asymmetric cryptography works: Program to demonstrate RSA encryption and decryption in Node.js, const crypto = require( "crypto" ) // Generating RSA key pairs(public and private key) const { publicKey, privateKey } = crypto.generateKeyPairSync( "rsa" , { // The length of our RSA keys is 3072 bits modulusLength: 3072 , }) //print RSA public key and private key console.log( publicKey.export({ type: "pkcs1" , format: "pem" , }), privateKey.export({ type: "pkcs1" , fo