crypto-js vs asn1.js vs crypto vs jsencrypt vs node-rsa vs openpgp
加密与解密库
crypto-jsasn1.jscryptojsencryptnode-rsaopenpgp类似的npm包:

加密与解密库

这些库提供了不同的加密和解密功能,适用于处理数据安全、身份验证和信息保护等场景。它们各自有独特的功能和使用场景,开发者可以根据项目需求选择合适的库。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
crypto-js26,049,24216,396487 kB2793 年前MIT
asn1.js0190-436 年前MIT
crypto035-139 年前ISC
jsencrypt06,805901 kB1461 年前MIT
node-rsa01,3771.44 MB25 个月前MIT
openpgp05,97717.4 MB311 个月前LGPL-3.0+

功能对比: crypto-js vs asn1.js vs crypto vs jsencrypt vs node-rsa vs openpgp

加密算法支持

  • crypto-js:

    crypto-js 支持多种对称和非对称加密算法,如 AES、DES、HMAC 等,适合在浏览器中使用。

  • asn1.js:

    asn1.js 主要用于解析和生成 ASN.1 编码的数据,不直接提供加密算法,但可以与其他加密库结合使用。

  • crypto:

    crypto 提供了多种加密算法,包括对称加密(如 AES)、非对称加密(如 RSA)和哈希算法(如 SHA-256)。

  • jsencrypt:

    jsencrypt 专注于 RSA 加密和解密,支持公钥和私钥的生成和使用。

  • node-rsa:

    node-rsa 提供了 RSA 加密、解密和密钥管理功能,支持多种密钥格式和加密选项。

  • openpgp:

    openpgp 实现了 PGP 加密标准,支持多种加密算法,包括 RSA、ElGamal 和对称加密。

使用场景

  • crypto-js:

    crypto-js 适合前端应用需要加密数据时,如用户密码加密和数据保护。

  • asn1.js:

    asn1.js 适合需要处理 ASN.1 数据结构的应用,如网络协议和安全通信。

  • crypto:

    crypto 适合需要高性能加密和哈希计算的 Node.js 应用,如数据存储和传输的安全性。

  • jsencrypt:

    jsencrypt 适合需要在客户端进行简单的 RSA 加密的应用,如保护用户信息。

  • node-rsa:

    node-rsa 适合需要复杂 RSA 加密和密钥管理的后端应用,如安全数据传输和存储。

  • openpgp:

    openpgp 适合需要实现 PGP 加密标准的应用,如安全邮件和文件加密。

学习曲线

  • crypto-js:

    crypto-js 的学习曲线较低,适合前端开发者,使用简单易懂。

  • asn1.js:

    asn1.js 的学习曲线相对较平缓,主要集中在 ASN.1 数据结构的理解上。

  • crypto:

    crypto 的学习曲线较低,Node.js 开发者可以快速上手,利用其内置的加密功能。

  • jsencrypt:

    jsencrypt 的学习曲线非常平缓,适合初学者使用,文档清晰。

  • node-rsa:

    node-rsa 的学习曲线相对较高,需要理解 RSA 加密的原理和密钥管理。

  • openpgp:

    openpgp 的学习曲线较高,涉及 PGP 标准和密钥管理的复杂性。

性能

  • crypto-js:

    crypto-js 在浏览器中性能良好,但相较于 Node.js 的 crypto 库,性能稍逊。

  • asn1.js:

    asn1.js 的性能主要取决于数据解析的复杂性,适合处理小型 ASN.1 数据。

  • crypto:

    crypto 在性能上表现优异,适合高并发和大数据量的加密需求。

  • jsencrypt:

    jsencrypt 在客户端性能良好,但对于大数据量的加密可能会影响性能。

  • node-rsa:

    node-rsa 性能较好,适合需要频繁加密和解密的场景。

  • openpgp:

    openpgp 性能较为复杂,受限于加密算法和数据大小,适合小型数据的加密。

社区支持

  • crypto-js:

    crypto-js 拥有活跃的社区,提供丰富的示例和文档。

  • asn1.js:

    asn1.js 的社区支持相对较小,主要用于特定领域。

  • crypto:

    crypto 是 Node.js 的核心模块,拥有广泛的社区支持和文档。

  • jsencrypt:

    jsencrypt 的社区支持较好,适合初学者,提供简单的使用示例。

  • node-rsa:

    node-rsa 拥有良好的社区支持,提供详细的文档和示例。

  • openpgp:

    openpgp 拥有活跃的社区,支持多种实现和丰富的文档。

如何选择: crypto-js vs asn1.js vs crypto vs jsencrypt vs node-rsa vs openpgp

  • crypto-js:

    选择 crypto-js 如果你需要在浏览器中进行加密,提供了多种加密算法的 JavaScript 实现,适合前端开发者使用。

  • asn1.js:

    选择 asn1.js 如果你需要处理 ASN.1 编码的数据,特别是在与其他系统进行通信时,ASN.1 是一种广泛使用的数据表示格式,适合需要解析和生成 ASN.1 数据的应用。

  • crypto:

    选择 crypto 如果你需要 Node.js 内置的加密功能,提供了多种加密算法和哈希函数,适合需要高性能和安全性的应用。

  • jsencrypt:

    选择 jsencrypt 如果你需要简单的 RSA 加密和解密,适合需要在客户端进行加密操作的应用,使用简单且易于集成。

  • node-rsa:

    选择 node-rsa 如果你需要更灵活的 RSA 密钥管理和加密功能,支持密钥的生成、导入和导出,适合需要复杂加密需求的后端应用。

  • openpgp:

    选择 openpgp 如果你需要实现 PGP 加密标准,适合需要进行安全邮件通信和文件加密的应用,支持密钥管理和签名功能。

crypto-js的README

crypto-js

JavaScript library of crypto standards.

Discontinued

Active development of CryptoJS has been discontinued. This library is no longer maintained.

Nowadays, NodeJS and modern browsers have a native Crypto module. The latest version of CryptoJS already uses the native Crypto module for random number generation, since Math.random() is not crypto-safe. Further development of CryptoJS would result in it only being a wrapper of native Crypto. Therefore, development and maintenance has been discontinued, it is time to go for the native crypto module.

Node.js (Install)

Requirements:

  • Node.js
  • npm (Node.js package manager)
npm install crypto-js

Usage

ES6 import for typical API call signing use case:

import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';

const message, nonce, path, privateKey; // ...
const hashDigest = sha256(nonce + message);
const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));

Modular include:

var AES = require("crypto-js/aes");
var SHA256 = require("crypto-js/sha256");
...
console.log(SHA256("Message"));

Including all libraries, for access to extra methods:

var CryptoJS = require("crypto-js");
console.log(CryptoJS.HmacSHA1("Message", "Key"));

Client (browser)

Requirements:

  • Node.js
  • Bower (package manager for frontend)
bower install crypto-js

Usage

Modular include:

require.config({
    packages: [
        {
            name: 'crypto-js',
            location: 'path-to/bower_components/crypto-js',
            main: 'index'
        }
    ]
});

require(["crypto-js/aes", "crypto-js/sha256"], function (AES, SHA256) {
    console.log(SHA256("Message"));
});

Including all libraries, for access to extra methods:

// Above-mentioned will work or use this simple form
require.config({
    paths: {
        'crypto-js': 'path-to/bower_components/crypto-js/crypto-js'
    }
});

require(["crypto-js"], function (CryptoJS) {
    console.log(CryptoJS.HmacSHA1("Message", "Key"));
});

Usage without RequireJS

<script type="text/javascript" src="path-to/bower_components/crypto-js/crypto-js.js"></script>
<script type="text/javascript">
    var encrypted = CryptoJS.AES(...);
    var encrypted = CryptoJS.SHA256(...);
</script>

API

See: https://cryptojs.gitbook.io/docs/

AES Encryption

Plain text encryption

var CryptoJS = require("crypto-js");

// Encrypt
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123').toString();

// Decrypt
var bytes  = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);

console.log(originalText); // 'my message'

Object encryption

var CryptoJS = require("crypto-js");

var data = [{id: 1}, {id: 2}]

// Encrypt
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString();

// Decrypt
var bytes  = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

console.log(decryptedData); // [{id: 1}, {id: 2}]

List of modules

  • crypto-js/core
  • crypto-js/x64-core
  • crypto-js/lib-typedarrays

  • crypto-js/md5
  • crypto-js/sha1
  • crypto-js/sha256
  • crypto-js/sha224
  • crypto-js/sha512
  • crypto-js/sha384
  • crypto-js/sha3
  • crypto-js/ripemd160

  • crypto-js/hmac-md5
  • crypto-js/hmac-sha1
  • crypto-js/hmac-sha256
  • crypto-js/hmac-sha224
  • crypto-js/hmac-sha512
  • crypto-js/hmac-sha384
  • crypto-js/hmac-sha3
  • crypto-js/hmac-ripemd160

  • crypto-js/pbkdf2

  • crypto-js/aes
  • crypto-js/tripledes
  • crypto-js/rc4
  • crypto-js/rabbit
  • crypto-js/rabbit-legacy
  • crypto-js/evpkdf

  • crypto-js/format-openssl
  • crypto-js/format-hex

  • crypto-js/enc-latin1
  • crypto-js/enc-utf8
  • crypto-js/enc-hex
  • crypto-js/enc-utf16
  • crypto-js/enc-base64

  • crypto-js/mode-cfb
  • crypto-js/mode-ctr
  • crypto-js/mode-ctr-gladman
  • crypto-js/mode-ofb
  • crypto-js/mode-ecb

  • crypto-js/pad-pkcs7
  • crypto-js/pad-ansix923
  • crypto-js/pad-iso10126
  • crypto-js/pad-iso97971
  • crypto-js/pad-zeropadding
  • crypto-js/pad-nopadding

Release notes

4.2.0

Change default hash algorithm and iteration's for PBKDF2 to prevent weak security by using the default configuration.

Custom KDF Hasher

Blowfish support

4.1.1

Fix module order in bundled release.

Include the browser field in the released package.json.

4.1.0

Added url safe variant of base64 encoding. 357

Avoid webpack to add crypto-browser package. 364

4.0.0

This is an update including breaking changes for some environments.

In this version Math.random() has been replaced by the random methods of the native crypto module.

For this reason CryptoJS might not run in some JavaScript environments without native crypto module. Such as IE 10 or before or React Native.

3.3.0

Rollback, 3.3.0 is the same as 3.1.9-1.

The move of using native secure crypto module will be shifted to a new 4.x.x version. As it is a breaking change the impact is too big for a minor release.

3.2.1

The usage of the native crypto module has been fixed. The import and access of the native crypto module has been improved.

3.2.0

In this version Math.random() has been replaced by the random methods of the native crypto module.

For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before.

If it's absolute required to run CryptoJS in such an environment, stay with 3.1.x version. Encrypting and decrypting stays compatible. But keep in mind 3.1.x versions still use Math.random() which is cryptographically not secure, as it's not random enough.

This version came along with CRITICAL BUG.

DO NOT USE THIS VERSION! Please, go for a newer version!

3.1.x

The 3.1.x are based on the original CryptoJS, wrapped in CommonJS modules.