JSEncrypt
A tiny (18.5kB gzip), zero dependency, JavaScript library to perform both synchronous and asynchronous OpenSSL RSA Encryption, Decryption, and Key Generation in both the Browser and Node.js.
Get started now View on GitHub
Quick Start
JSEncrypt is a tiny (18kb gzip), zero dependency, JavaScript library to perform both synchronous and asynchronous OpenSSL RSA Encryption, Decryption, and Key Generation in both the Browser and Node.js. Itβs designed to work seamlessly with OpenSSL-generated RSA key pairs, but is also capable of generating new keys (less secure)
Installation
npm install jsencrypt
Basic Usage
Browser (Script Tag)
<script src="https://cdn.jsdelivr.net/npm/jsencrypt/bin/jsencrypt.min.js"></script>
<script>
// Create the encryption object
const crypt = new JSEncrypt();
// Here you can either use a private key from OpenSSL,
// or generate one using the following code.
const privateKey = crypt.getPrivateKey();
// Set your RSA private key
crypt.setPrivateKey(privateKey);
// Encrypt data with the public key
const encrypted = crypt.encrypt('Hello World!');
// Decrypt data with the private key
const decrypted = crypt.decrypt(encrypted);
</script>
ES6 Module Import
import { JSEncrypt } from 'jsencrypt';
// Create the encryption object
const crypt = new JSEncrypt();
// Here you can either use a private key from OpenSSL,
// or generate one using the following code.
const privateKey = crypt.getPrivateKey();
// Set your RSA private key
crypt.setPrivateKey(privateKey);
// Encrypt data with the public key
const encrypted = crypt.encrypt('Hello World!');
// Decrypt data with the private key
const decrypted = crypt.decrypt(encrypted);
Node.js (CommonJS)
const JSEncrypt = require('jsencrypt');
// Create the encryption object
const crypt = new JSEncrypt();
// Here you can either use a private key from OpenSSL,
// or generate one using the following code.
const privateKey = crypt.getPrivateKey();
// Set your RSA private key
crypt.setPrivateKey(privateKey);
// Encrypt data with the public key
const encrypted = crypt.encrypt('Hello World!');
// Decrypt data with the private key
const decrypted = crypt.decrypt(encrypted);
Features
β‘ Tiny & Fast
Just 18.5 kB gzipped - minimal impact on your bundle size.
π OpenSSL Compatible
Works seamlessly with OpenSSL-generated RSA key pairs in PEM format.
π Browser & Node.js
Runs in both browser environments and Node.js applications.
π¦ Zero Dependencies
Lightweight library with no external dependencies.
π Secure Encryption
Implements proper RSA encryption/decryption algorithms.
π Well Documented
Comprehensive documentation with examples and best practices.
π§ͺ Thoroughly Tested
Extensively tested codebase ensuring reliability.
Real-World Example
Hereβs a complete example showing how to use JSEncrypt with OpenSSL keys:
1. Generate RSA Keys with OpenSSL
# Generate private key
openssl genrsa -out private.pem 2048
# Extract public key
openssl rsa -pubout -in private.pem -out public.pem
2. Use in JavaScript
import { JSEncrypt } from 'jsencrypt';
// Initialize
const crypt = new JSEncrypt();
// Load your keys
crypt.setPrivateKey(`-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA...
-----END RSA PRIVATE KEY-----`);
// Encrypt sensitive data
const originalText = 'Secret message';
const encrypted = crypt.encrypt(originalText);
// Decrypt when needed
const decrypted = crypt.decrypt(encrypted);
console.log(decrypted === originalText); // true
Why JSEncrypt?
When choosing an RSA encryption library for JavaScript, you need a solution thatβs reliable, secure, and fits seamlessly into your development workflow. JSEncrypt delivers on all fronts.
JSEncrypt stands out by providing enterprise-grade RSA encryption capabilities without the complexity and security concerns that come with heavy dependencies.
Key Benefits
β‘ Tiny & Fast - Just 18.5 kB gzipped with minimal impact on bundle size and fast execution performance
π Universal Compatibility - Works seamlessly in both Node.js server environments and browser applications without any modifications or polyfills
π¦ Zero Dependencies - No external dependencies means better security posture, easier CVE management, and reduced bundle size for your applications
β‘ Flexible Execution - Supports both synchronous and asynchronous JavaScript patterns, giving you the flexibility to integrate with any codebase architecture
π OpenSSL Compatible - Direct support for PEM-formatted keys generated with OpenSSL, eliminating the need for key format conversions
π‘οΈ Proven Security - Built on Tom Wuβs battle-tested jsbn library without modifying the core cryptographic algorithms
π Production Ready - Lightweight, well-tested, and used by thousands of developers worldwide
Get Started
Ready to add secure RSA encryption to your project?
Try it now: Check out our interactive demo to see JSEncrypt in action, or dive into the documentation to learn more.