npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

encrypt-rsa

v3.3.0

Published

This is a little module use to encrypt and decrypt strings with RSA keys (public and private keys)

Downloads

34,583

Readme

NodeRSA

NodeRSA is a library that provides easy-to-use methods for RSA encryption and decryption. It allows generating RSA key pairs, encrypting, and decrypting strings with RSA public and private keys. This library is ideal for secure data transmission, authentication systems, and any application requiring cryptographic security.

Installation

npm install encrypt-rsa
// OR
yarn add encrypt-rsa

Usage

Importing the Library

import NodeRSA from 'encrypt-rsa';

Creating an Instance

You can create an instance of the NodeRSA class with optional public and private keys and modulus length.

const nodeRSA = new NodeRSA(publicKey, privateKey, modulusLength);

Generating RSA Key Pairs

To generate a new pair of RSA keys:

const { publicKey, privateKey } = nodeRSA.createPrivateAndPublicKeys(modulusLength);
console.log('Public Key:', publicKey);
console.log('Private Key:', privateKey);

Encrypting and Decrypting Strings

Encrypting with RSA Public Key

const text = "Hello, World!";
const encryptedString = nodeRSA.encryptStringWithRsaPublicKey({ text, publicKey });
console.log('Encrypted:', encryptedString);

Decrypting with RSA Private Key

const decryptedString = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedString, privateKey });
console.log('Decrypted:', decryptedString);

Encrypting with RSA Private Key

const text = "Hello, World!";
const encryptedString = nodeRSA.encrypt({ text, privateKey });
console.log('Encrypted with Private Key:', encryptedString);

Decrypting with RSA Public Key

const decryptedString = nodeRSA.decrypt({ text: encryptedString, publicKey });
console.log('Decrypted with Public Key:', decryptedString);

API

NodeRSA Class

Constructor

constructor(publicKey?: string, privateKey?: string, modulusLength?: number)
  • publicKey: Optional. The RSA public key.
  • privateKey: Optional. The RSA private key.
  • modulusLength: Optional. The modulus length for the RSA key pair (default is 2048).

Methods

  1. createPrivateAndPublicKeys(modulusLength: number = this.modulusLength): returnCreateKeys
    • Generates a new pair of RSA keys.
    • modulusLength: Optional. The modulus length for the RSA key pair (default is the instance's modulus length).
    • Returns an object containing the publicKey and privateKey.
  2. encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string
    • Encrypts a string with the given RSA public key.
    • args: Object containing text and optionally publicKey.
    • Returns the encrypted string in base64 format.
  3. decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string
    • Decrypts a string with the given RSA private key.
    • args: Object containing text and optionally privateKey.
    • Returns the decrypted string.
  4. encrypt(args: parametersOfEncryptPrivate): string
    • Encrypts a string with the given RSA private key.
    • args: Object containing text and optionally privateKey.
    • Returns the encrypted string in base64 format.
  5. decrypt(args: parametersOfDecryptPublic): string
    • Decrypts a string with the given RSA public key.
    • args: Object containing text and optionally publicKey.
    • Returns the decrypted string.

Types

  1. parametersOfEncrypt
{
  text: string;
  publicKey?: string;
}
  1. parametersOfDecrypt
{
  text: string;
  privateKey?: string;
}
  1. parametersOfEncryptPrivate
{
  text: string;
  privateKey?: string;
}
  1. parametersOfDecryptPublic
{
  text: string;
  publicKey?: string;
}
  1. returnCreateKeys
{
  publicKey: string;
  privateKey: string;
}

Utilities

  1. convertKetToBase64(key: string): string Converts a given key to base64 format.

Helper Functions

  1. encode Encodes a string to base64.

  2. decode Decodes a base64 string.

Use Cases

Secure Data Transmission

NodeRSA can be used to securely transmit sensitive data over insecure channels. Encrypt data with the recipient's public key before sending it. Only the recipient can decrypt the data with their private key.

// Sender
const encryptedMessage = nodeRSA.encryptStringWithRsaPublicKey({ text: "Sensitive data", publicKey: recipientPublicKey });
// Send `encryptedMessage` to the recipient

// Recipient
const decryptedMessage = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedMessage, privateKey: recipientPrivateKey });
console.log('Decrypted Message:', decryptedMessage);

Authentication Systems

NodeRSA can be used in authentication systems to encrypt credentials and sensitive information.

// Encrypting user credentials
const encryptedCredentials = nodeRSA.encryptStringWithRsaPublicKey({ text: "username:password", publicKey: serverPublicKey });

// Decrypting credentials on the server
const decryptedCredentials = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedCredentials, privateKey: serverPrivateKey });
console.log('Decrypted Credentials:', decryptedCredentials);

Buffer Encryption/Decryption Methods:

  1. encryptBufferWithRsaPublicKey: Converts a buffer to Base64 and then encrypts the Base64 string.
  2. decryptBufferWithRsaPrivateKey: Decrypts the Base64 string and converts it back to a buffer.

Example Usage

const nodeRSA = new NodeRSA();

// Generate keys
const { publicKey, privateKey } = nodeRSA.createPrivateAndPublicKeys();

// Example buffer
const buffer = Buffer.from('This is some binary data');

// Encrypt the buffer
const encryptedBuffer = nodeRSA.encryptBufferWithRsaPublicKey(buffer, publicKey);
console.log('Encrypted Buffer:', encryptedBuffer);

// Decrypt back to buffer
const decryptedBuffer = nodeRSA.decryptBufferWithRsaPrivateKey(encryptedBuffer, privateKey);
console.log('Decrypted Buffer:', decryptedBuffer.toString());  // should log: 'This is some binary data'

Digital Signatures

Although not directly covered by the current implementation, RSA can also be used for creating and verifying digital signatures to ensure data integrity and authenticity.

Contribution

We welcome contributions to the NodeRSA library! If you'd like to contribute, please follow these steps:

  1. Fork the repository on GitHub.
  2. Clone your forked repository to your local machine.
git clone [email protected]:miladezzat/encrypt-rsa.git
  1. Create a new branch for your feature or bugfix.
git checkout -b feature/your-feature-name
  1. Make your changes to the codebase.
  2. Commit your changes with a clear and descriptive commit message.
git commit -m "Description of your feature or fix"
  1. Push your changes to your forked repository.
git push origin feature/your-feature-name
  1. Create a pull request on the original repository. Be sure to include a detailed description of your changes and the problem they solve.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Reporting Issues

If you encounter any issues, please report them using the GitHub issue tracker. Include details about the problem and your environment (OS, Node.js version, etc.).

Thank you for contributing to NodeRSA!