node-rsa vs jsencrypt vs asn1.js vs crypto vs crypto-js vs openpgp
JavaScript Cryptography Libraries
node-rsajsencryptasn1.jscryptocrypto-jsopenpgpSimilar Packages:

JavaScript Cryptography Libraries

These libraries provide various cryptographic functions and utilities for JavaScript applications, enabling developers to implement secure data transmission, encryption, and decryption. They cater to different cryptographic needs, from simple encryption to complex public key infrastructures, ensuring data integrity and confidentiality in web applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
node-rsa3,091,6981,3801.44 MB14 months agoMIT
jsencrypt537,5436,805901 kB146a year agoMIT
asn1.js0190-436 years agoMIT
crypto034-139 years agoISC
crypto-js016,408487 kB2793 years agoMIT
openpgp05,96617.4 MB324 months agoLGPL-3.0+

Feature Comparison: node-rsa vs jsencrypt vs asn1.js vs crypto vs crypto-js vs openpgp

Encryption Algorithms

  • node-rsa:

    node-rsa provides a robust implementation of RSA encryption and decryption, supporting key generation and management, making it suitable for secure communications.

  • jsencrypt:

    jsencrypt specifically implements RSA encryption, which is suitable for encrypting small pieces of data, particularly for secure key exchange.

  • asn1.js:

    asn1.js does not provide encryption algorithms directly but is essential for encoding and decoding ASN.1 structures, which are often used in cryptographic protocols.

  • crypto:

    The crypto module includes various encryption algorithms such as AES, DES, and RSA, allowing developers to choose the appropriate algorithm for their security needs.

  • crypto-js:

    crypto-js offers a wide range of cryptographic algorithms including AES, SHA-1, SHA-256, and HMAC, making it versatile for client-side encryption tasks.

  • openpgp:

    openpgp implements the OpenPGP standard, providing advanced encryption algorithms and features such as public key cryptography, digital signatures, and key management.

Ease of Use

  • node-rsa:

    node-rsa provides a clear API for RSA operations, making it accessible for developers needing to implement public key cryptography without extensive background knowledge.

  • jsencrypt:

    jsencrypt has a simple API for RSA encryption, making it easy to implement secure data transmission without deep cryptographic knowledge.

  • asn1.js:

    asn1.js requires understanding of ASN.1 structures, which may have a steeper learning curve for those unfamiliar with the format.

  • crypto:

    The crypto module is straightforward for Node.js developers, as it integrates seamlessly with the platform's APIs, but may require familiarity with cryptographic concepts.

  • crypto-js:

    crypto-js is user-friendly and designed for ease of use in browser environments, with simple APIs for common cryptographic tasks.

  • openpgp:

    openpgp has a more complex API due to its comprehensive feature set, which may require a deeper understanding of cryptographic principles.

Performance

  • node-rsa:

    node-rsa is optimized for RSA operations, but performance can vary based on key size and operation complexity, making it suitable for moderate workloads.

  • jsencrypt:

    jsencrypt is efficient for small data encryption but may not be suitable for large data due to RSA's inherent limitations on data size.

  • asn1.js:

    asn1.js is lightweight and optimized for encoding and decoding ASN.1 structures, but it does not perform encryption, so performance depends on the encryption library used alongside it.

  • crypto:

    The crypto module is highly optimized for performance in Node.js, leveraging native bindings for cryptographic operations, making it suitable for high-performance applications.

  • crypto-js:

    crypto-js is designed for performance in the browser, but its performance may not match that of native implementations for large data sets or complex operations.

  • openpgp:

    openpgp is feature-rich, but its performance may be impacted by the complexity of operations, especially with large keys or data.

Security Features

  • node-rsa:

    node-rsa provides strong RSA encryption and key management features, ensuring secure data transmission and storage, suitable for sensitive applications.

  • jsencrypt:

    jsencrypt offers RSA encryption, which is secure for small data but should be used with caution regarding key management and data size limitations.

  • asn1.js:

    asn1.js focuses on encoding and decoding ASN.1 data structures, which are crucial for secure data formats but does not provide direct security features.

  • crypto:

    The crypto module offers a range of security features such as secure hashing, HMAC, and various encryption algorithms, making it a robust choice for secure applications.

  • crypto-js:

    crypto-js provides strong encryption and hashing algorithms, but it is important to ensure proper implementation to avoid vulnerabilities.

  • openpgp:

    openpgp implements the OpenPGP standard, providing advanced security features such as digital signatures, key management, and strong encryption, making it ideal for secure communications.

Community and Support

  • node-rsa:

    node-rsa has a growing community and good documentation, providing support for developers implementing RSA encryption in Node.js applications.

  • jsencrypt:

    jsencrypt has a smaller user base, but it is straightforward to use, and community support is available through forums and GitHub.

  • asn1.js:

    asn1.js has a smaller community, which may result in limited resources and support compared to more widely used libraries.

  • crypto:

    The crypto module is part of Node.js, benefiting from extensive documentation and community support due to its integral role in the ecosystem.

  • crypto-js:

    crypto-js has a decent community and documentation, making it accessible for developers looking for client-side cryptographic solutions.

  • openpgp:

    openpgp has a strong community and extensive documentation, making it a reliable choice for developers needing robust encryption solutions.

How to Choose: node-rsa vs jsencrypt vs asn1.js vs crypto vs crypto-js vs openpgp

  • node-rsa:

    Choose node-rsa if you require a comprehensive RSA library for Node.js that supports key generation, encryption, and decryption. It is suitable for applications that need to manage public and private keys securely and perform RSA operations efficiently.

  • jsencrypt:

    Use jsencrypt when you need to implement RSA encryption in your JavaScript applications. It is straightforward to use for encrypting small amounts of data and is particularly useful for secure data transmission over the web.

  • asn1.js:

    Choose asn1.js if you need to handle ASN.1 (Abstract Syntax Notation One) encoding and decoding, particularly for applications that require interoperability with protocols that use ASN.1, such as X.509 certificates or SNMP.

  • crypto:

    Select the built-in crypto module if you are working in a Node.js environment and require a comprehensive set of cryptographic functionalities including hashing, signing, and encryption. It is well-integrated with Node.js and provides a robust set of features for secure communications.

  • crypto-js:

    Opt for crypto-js if you need a lightweight, browser-compatible library for cryptographic functions. It is particularly useful for client-side applications that require hashing, encryption, and decryption without relying on server-side capabilities.

  • openpgp:

    Select openpgp if you need to implement OpenPGP standard encryption and decryption. It is ideal for applications that require strong security features such as digital signatures and key management, making it suitable for secure email and file encryption.

README for node-rsa

Node-RSA

npm version CI npm downloads license

RSA library for Node.js and browsers.

  • Pure TypeScript
  • Works in Node.js and modern browsers (no Buffer/crypto polyfills needed)
  • Generating keys
  • Encrypting and decrypting, with long-message support
  • Signing and verifying

Table of contents

Example

import NodeRSA from 'node-rsa';

const key = new NodeRSA({ b: 2048 });

const text = 'Hello RSA!';
const encrypted = key.encrypt(text, 'base64');
console.log('encrypted:', encrypted);
const decrypted = key.decrypt(encrypted, 'utf8');
console.log('decrypted:', decrypted);

Installing

npm install node-rsa

Requires Node.js >= 20. For browsers, any bundler with conditional-exports support (Vite, Webpack 5, Rollup, esbuild, Parcel) picks the browser entry automatically.

Testing

npm test

Usage

Create instance

import NodeRSA from 'node-rsa';

const key = new NodeRSA([keyData, [format]], [options]);
  • keyData — string | Uint8Array | object — key data in one of the supported formats, or a generation spec.
  • format — string — format id for importing the key. See Import/Export.
  • options — object — additional settings (below).

Options

You can pass options as the second/third constructor argument, or later via key.setOptions().

  • environment — 'node' or 'browser'. Auto-detected from the loaded bundle; the option mainly exists to force the pure-JS engine on Node (setOptions({ environment: 'browser' })), bypassing the node:crypto fast path — useful if you need PSS with a custom MGF.
  • bigIntImpl — 'native' or 'jsbn'. The browser bundle defaults to native ES2020 BigInt; the Node bundle uses jsbn. Switch only before importing/generating a key; switching on a populated instance throws.
  • signingScheme — scheme used for sign / verify. One of 'pss' (default), 'pkcs1', or a 'scheme-hash' shorthand (e.g. 'pkcs1-sha512'). Object form is also accepted: { scheme: 'pss', hash: 'sha256', saltLength?: number, mgf?: MaskGenerationFunction }. Default hash is sha256.
  • encryptionScheme — padding scheme for encrypt / decrypt. One of 'pkcs1_oaep' (default) or 'pkcs1'. Object form: { scheme: 'pkcs1_oaep', hash: 'sha1', mgf?, label? }. Default OAEP hash is sha1.

Note: Supported hash algorithms are 'md5', 'ripemd160', 'sha1', 'sha256', 'sha512' in both environments, plus 'md4', 'sha224', 'sha384' on Node. 'md4' additionally requires running Node with --openssl-legacy-provider.

Creating an "empty" key

const key = new NodeRSA();

Generate new 2048-bit key

const key = new NodeRSA({ b: 2048 });

Or:

key.generateKeyPair([bits], [exp]);
  • bits — number — key size in bits. 2048 by default.
  • exp — number — public exponent. 65537 by default.

Load key from PEM string

const key = new NodeRSA(
  '-----BEGIN RSA PRIVATE KEY-----\n' +
  'MIIBOQIBAAJAVY6quuzCwyOWzymJ7C4zXjeV/232wt2ZgJZ1kHzjI73wnhQ3WQcL\n' +
  'DFCSoi2lPUW8/zspk0qWvPdtp6Jg5Lu7hwIDAQABAkBEws9mQahZ6r1mq2zEm3D/\n' +
  'VM9BpV//xtd6p/G+eRCYBT2qshGx42ucdgZCYJptFoW+HEx/jtzWe74yK6jGIkWJ\n' +
  'AiEAoNAMsPqwWwTyjDZCo9iKvfIQvd3MWnmtFmjiHoPtjx0CIQCIMypAEEkZuQUi\n' +
  'pMoreJrOlLJWdc0bfhzNAJjxsTv/8wIgQG0ZqI3GubBxu9rBOAM5EoA4VNjXVigJ\n' +
  'QEEk1jTkp8ECIQCHhsoq90mWM/p9L5cQzLDWkTYoPI49Ji+Iemi2T5MRqwIgQl07\n' +
  'Es+KCn25OKXR/FJ5fu6A6A+MptABL3r8SEjlpLc=\n' +
  '-----END RSA PRIVATE KEY-----',
);

Import/Export keys

key.importKey(keyData, [format]);
key.exportKey([format]);
  • keyData — may be:
    • PEM string (or a Uint8Array/Buffer containing one)
    • Uint8Array containing raw DER
    • object with raw key components
  • format — string — format id for import/export.

Format string syntax

scheme-[key_type]-[output_type]

Scheme — node-rsa supports several:

  • 'pkcs1' — public PEM starts with -----BEGIN RSA PUBLIC KEY-----, private with -----BEGIN RSA PRIVATE KEY-----.
  • 'pkcs8' — public PEM starts with -----BEGIN PUBLIC KEY-----, private with -----BEGIN PRIVATE KEY-----.
  • 'openssh' — public starts with ssh-rsa, private with -----BEGIN OPENSSH PRIVATE KEY-----.
  • 'components' — raw modulus/exponent and CRT params. For a private key all components must be present; for a public key only n and e. All components are Uint8Array except e, which may be Uint8Array or a plain number.

Key type — 'private' (default) or 'public'.

Output type:

  • 'pem' — base64 PEM string with header/footer. Used by default.
  • 'der' — Uint8Array of binary DER.

Note: For import, if keyData is a PEM string (or a Uint8Array containing PEM), you can omit format. If it's raw DER, you must specify the format string.

Shortcuts and examples

  • 'private' ≡ 'pkcs1' ≡ 'pkcs1-private' ≡ 'pkcs1-private-pem' — private key, PKCS#1, PEM.
  • 'public' ≡ 'pkcs8-public' ≡ 'pkcs8-public-pem' — public key, PKCS#8, PEM.
  • 'pkcs8' ≡ 'pkcs8-private' ≡ 'pkcs8-private-pem' — private key, PKCS#8, PEM.
  • 'pkcs1-der' ≡ 'pkcs1-private-der' — private key, PKCS#1, binary DER.
  • 'pkcs8-public-der' — public key, PKCS#8, binary DER.

Code example

const keyData = '-----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY-----';
key.importKey(keyData, 'pkcs8');
const publicDer = key.exportKey('pkcs8-public-der');
const privateDer = key.exportKey('pkcs1-der');
const hex = (s: string) =>
  Uint8Array.from(s.match(/.{2}/g)!.map((b) => parseInt(b, 16)));

key.importKey({
  n: hex('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783'),
  e: 65537,
  d: hex('5d2f0dd982596ef781affb1cab73a77c46985c6da2aafc252cea3f4546e80f40c0e247d7d9467750ea1321cc5aa638871b3ed96d19dcc124916b0bcb296f35e1'),
  p: hex('00c59419db615e56b9805cc45673a32d278917534804171edcf925ab1df203927f'),
  q: hex('00aee3f86b66087abc069b8b1736e38ad6af624f7ea80e70b95f4ff2bf77cd90fd'),
  dmp1: hex('008112f5a969fcb56f4e3a4c51a60dcdebec157ee4a7376b843487b53844e8ac85'),
  dmq1: hex('1a7370470e0f8a4095df40922a430fe498720e03e1f70d257c3ce34202249d21'),
  coeff: hex('00b399675e5e81506b729a777cc03026f0b2119853dfc5eb124610c0ab82999e45'),
}, 'components');

const publicComponents = key.exportKey('components-public');
console.log(publicComponents);

/*
{ n: Uint8Array(65) [0, 134, 250, 155, 160, 102, 104, 88, 69, 252, 3, 131, 58, ... ],
  e: 65537 }
*/

To import only the public part use 'components-public':

key.importKey({
  n: hex('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783'),
  e: 65537,
}, 'components-public');

Note: Buffer is a Uint8Array subclass on Node, so any code that passes Buffer.from(...) still works — the types document the cross-platform shape.

Properties

Key testing

key.isPrivate();
key.isPublic([strict]);

strict — boolean — if true, returns false when the key pair also contains a private exponent. Default false.

key.isEmpty();

Returns true if the instance has no key data.

Key info

key.getKeySize();

Returns key size in bits.

key.getMaxMessageSize();

Returns the max data size for a single encrypt operation, in bytes (scheme-dependent).

Encrypting/Decrypting

key.encrypt(buffer, [encoding], [source_encoding]);
key.encryptPrivate(buffer, [encoding], [source_encoding]); // encrypt with private key

Returns the encrypted data.

  • buffer — data to encrypt. May be string, Uint8Array (or Buffer on Node), number, plain object, or array. Objects and arrays are JSON-stringified first.
  • encoding — output encoding: 'buffer' (default — returns Uint8Array), 'binary', 'hex', or 'base64'.
  • source_encoding — only used when buffer is a string; how to interpret its bytes. Accepts 'utf8' (default), 'hex', 'base64', 'binary'.
key.decrypt(buffer, [encoding]);
key.decryptPublic(buffer, [encoding]); // decrypt with public key

Returns the decrypted data.

  • buffer — Uint8Array or base64-encoded string.
  • encoding — output: 'buffer' (default, raw Uint8Array), 'utf8', 'hex', 'base64', 'binary', or 'json' (UTF-8 decoded + JSON.parse).

Note: encryptPrivate / decryptPublic always use PKCS#1 v1.5 type-1 padding (deterministic), regardless of the configured encryptionScheme.

Signing/Verifying

key.sign(buffer, [encoding], [source_encoding]);

Returns the signature. All arguments behave like encrypt.

key.verify(buffer, signature, [source_encoding], [signature_encoding]);

Returns true / false.

  • buffer — data that was signed; same shape as for encrypt.
  • signature — Uint8Array or string, as produced by sign.
  • source_encoding — encoding for buffer if it's a string. Default 'utf8'.
  • signature_encoding — encoding of signature. One of 'buffer' (default), 'binary', 'hex', 'base64'.

Browser usage

The browser bundle (dist/index.browser.js) is published as ESM only and contains no Node-builtin imports — you don't need to polyfill Buffer, crypto, or process. Bundlers that honour the "browser" export condition (Vite, Webpack 5, Rollup, esbuild, Parcel) pick it up automatically. The bundle weighs ~114 KB raw / ~28 KB gzipped.

If your bundler doesn't resolve conditional exports, import the browser entry directly:

import NodeRSA from 'node-rsa/dist/index.browser.js';

Security notes

  • PKCS#1 v1.5 encryption (encryptionScheme: 'pkcs1') is vulnerable to Bleichenbacher-style padding-oracle attacks when used to decrypt attacker-controlled ciphertexts. The library closes the internal differential timing channel but cannot eliminate the binary valid/invalid oracle inherent to the scheme. Use the default 'pkcs1_oaep' for new code and for any path that touches attacker-controlled ciphertext.

Migrating

Migrating from 1.x? See MIGRATION.md for the behaviour-change summary and step-by-step walkthrough.

Changelog

Release notes and per-version changes are tracked in CHANGELOG.md.

License

Copyright (c) 2014 rzcoder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Licensing for code used in rsa.ts and jsbn

Copyright (c) 2003-2005 Tom Wu All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

In addition, the following condition applies:

All redistributions must retain an intact copy of this copyright notice and disclaimer.

Acknowledgements

  • Tom Wu — original jsbn BigInteger and RSA implementations
  • Paul Miller — @noble/hashes v2.x, audited synchronous hash functions (MD5, RIPEMD-160, SHA-1/2 family) used by the browser bundle