3 min read

Encrypt By RSA Algorithm

Public and private keys protecting and opening a message capsule.

Encrypt By RSA Algorithm

npm version 

RSA is a public-key cryptosystem that is widely used for secure data transmission. It is also one of the oldest. The acronym RSA comes from the surnames of Ron Rivest, Adi Shamir and Leonard Adleman, who publicly described the algorithm in 1977. Wikipedia

RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e. Public Key and Private Key. As the name describes that the Public Key is given to everyone and Private key is kept private.

  1. Installation
  2. Usage

Installation

  npm i encrypt-rsa

  // OR

  yarn add encrypt-rsa

Usage

import NodeRSA from 'encrypt-rsa'

//OR

const NodeRSA = require('encrypt-rsa').default

const fs = require('fs') // Or import * as fs from 'fs';

const nodeRSA = new NodeRSA()

// create public and private keys

const { privateKey, publicKey } = nodeRSA.createPrivateAndPublicKeys()

// to save your keys
fs.writeFileSync('./private-key', privateKey)
fs.writeFileSync('./public-key', publicKey)

// you must have 'public-key' file the key you want to encrypt by it

const encryptedText = nodeRSA.encryptStringWithRsaPublicKey({
  text: 'hello',
  keyPath: path.join(__dirname, './public-key'),
})

console.log({ encryptedText })

//result:  {
//   encryptedText: 'QAoJBAj7ZqYR9Qb9vFGfpjBVY7BP0MtlPywyxMSodA7WmOmOn0glOlrLxUqjJrmaKsqxdJxZadEMAM8+6gLNhwcLtbFPRLQEUTSHk2NNhehsPOESoNjwbXOj5Y+zBCSkjVuW6MRkdaTZeGXi0sii1OqvIQGmOaOR2xzEdDj2eD8='
// }

// you must have 'private-key' file the key you want to decrypt by it

const decryptedText = nodeRSA.decryptStringWithRsaPrivateKey({
  text: encryptedText,
  keyPath: path.join(__dirname, './private-key'),
})

console.log({ decryptedText })
// result: { decryptedText: 'hello' }

for more information about RSA: https://simple.wikipedia.org/wiki/RSA_algorithm

  • Engineering

    De Duplicated

    This is package for deleting the duplicated elements in array, there are contain two methods, first method called simpleArray, this method deduplicated the…

    4 min read ↗
  • Engineering

    Error Handler E2

    [](https://travis-ci.org/miladezzat/error-handler-e2) [](https://badge.fury.io/js/error-handler-e2) 

    2 min read ↗
  • Engineering

    Memoz

    Memoz is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported, Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps.

    3 min read ↗