
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
encrypt-rsa
Advanced tools
This is a little module use to encrypt and decrypt strings with RSA keys (public and private keys)
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.
npm install encrypt-rsa
// OR
yarn add encrypt-rsa
Importing the Library
import NodeRSA from 'encrypt-rsa';
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);
To generate a new pair of RSA keys:
const { publicKey, privateKey } = nodeRSA.createPrivateAndPublicKeys(modulusLength);
console.log('Public Key:', publicKey);
console.log('Private Key:', privateKey);
const text = "Hello, World!";
const encryptedString = nodeRSA.encryptStringWithRsaPublicKey({ text, publicKey });
console.log('Encrypted:', encryptedString);
const decryptedString = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedString, privateKey });
console.log('Decrypted:', decryptedString);
const text = "Hello, World!";
const encryptedString = nodeRSA.encrypt({ text, privateKey });
console.log('Encrypted with Private Key:', encryptedString);
const decryptedString = nodeRSA.decrypt({ text: encryptedString, publicKey });
console.log('Decrypted with Public Key:', decryptedString);
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).createPrivateAndPublicKeys(modulusLength: number = this.modulusLength): returnCreateKeys
modulusLength
: Optional. The modulus length for the RSA key pair (default is the instance's modulus length).publicKey
and privateKey
.encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string
args
: Object containing text
and optionally publicKey
.decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string
args
: Object containing text
and optionally privateKey
.encrypt(args: parametersOfEncryptPrivate): string
args
: Object containing text
and optionally privateKey
.decrypt(args: parametersOfDecryptPublic): string
args
: Object containing text
and optionally publicKey
.{
text: string;
publicKey?: string;
}
{
text: string;
privateKey?: string;
}
{
text: string;
privateKey?: string;
}
{
text: string;
publicKey?: string;
}
{
publicKey: string;
privateKey: string;
}
convertKetToBase64(key: string): string
Converts a given key to base64 format.encode
Encodes a string to base64.
decode
Decodes a base64 string.
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);
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);
encryptBufferWithRsaPublicKey
: Converts a buffer to Base64 and then encrypts the Base64 string.decryptBufferWithRsaPrivateKey
: Decrypts the Base64 string and converts it back to a buffer.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'
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.
We welcome contributions to the NodeRSA library! If you'd like to contribute, please follow these steps:
git clone git@github.com:miladezzat/encrypt-rsa.git
git checkout -b feature/your-feature-name
git commit -m "Description of your feature or fix"
git push origin feature/your-feature-name
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.
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!
FAQs
This is a little module use to encrypt and decrypt strings with RSA keys (public and private keys)
The npm package encrypt-rsa receives a total of 6,229 weekly downloads. As such, encrypt-rsa popularity was classified as popular.
We found that encrypt-rsa demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.