This Node.js module provides functions for RSA encryption and decryption using public and private keys. It utilizes the crypto
module for cryptographic operations and supports loading keys from environment variables or default paths.
To use this module, make sure you have Node.js installed. Then, install the required dependencies:
npm install rsa-crypto-node
-
Create a
.env
file in the root of your project. -
Add the paths to your public and private key files as environment variables:
PUBLIC_KEY_PATH=./keys/public_key.pem PRIVATE_KEY_PATH=./keys/private_key.pem
import rsaEncryption from 'rsa-crypto-node';
const textToEncrypt = 'Hello, World!';
const encryptedText = rsaEncryption.encryptWithPublicKey(textToEncrypt);
console.log('Encrypted:', encryptedText);
const encryptedText = '...'; // Replace with the actual encrypted text
const decryptedText = rsaEncryption.decryptWithPrivateKey(encryptedText);
console.log('Decrypted:', decryptedText);
const textToEncrypt = 'Hello, World!';
const encryptedText = rsaEncryption.encryptWithPrivateKey(textToEncrypt);
console.log('Encrypted with Private Key:', encryptedText);
const encryptedText = '...'; // Replace with the actual encrypted text
const decryptedText = rsaEncryption.decryptWithPublicKey(encryptedText);
console.log('Decrypted with Public Key:', decryptedText);
Note: The last two methods (encrypting with the private key and decrypting with the public key) are non-standard use cases and may have security implications. Please use them cautiously based on your specific requirements.
To run tests, make sure you have Mocha and Chai installed:
npm install mocha chai --save-dev
then run test
npm test
This project is licensed under the MIT License - see the LICENSE file for details.