CryptoJs

yuhuo2022-11-19开发库工具库
参考链接

Hash算法

没有秘钥的摘要算法,不可逆转。

算法名位数字符串长度
MD512832
RIPEMD16016040
SHA116040
SHA224(SHA2)22456
SHA256(SHA2)25664
SHA384(SHA2)38496
SHA512(SHA2)512128
SHA3512128

说明

4位二进制可以使用1个十六进制的字符串表示,所以位数是字符串长度的4倍。

输出的位数增加,安全性上升,速度下降。

import MD5 from "crypto-js/md5";
import RIPEMD160 from "crypto-js/ripemd160";
import SHA1 from "crypto-js/sha1";
import SHA224 from "crypto-js/sha224";
import SHA256 from "crypto-js/sha256";
import SHA384 from "crypto-js/sha384";
import SHA512 from "crypto-js/sha512";
import SHA3 from "crypto-js/sha3";

var cipher1 = MD5("明文").toString();
var cipher2 = RIPEMD160("明文").toString();
var cipher3 = SHA1("明文").toString();
var cipher4 = SHA224("明文").toString();
var cipher5 = SHA256("明文").toString();
var cipher6 = SHA384("明文").toString();
var cipher7 = SHA512("明文").toString();
// SHA3默认512位
var cipher8 = SHA3("明文").toString();
// SHA3指定输出位数
var cipher9 = SHA3("明文", { outputLength: 256 }).toString();

HMAC算法

HMAC = Hash-based Message Authentication Code(哈希运算消息认证码)。

使用秘钥的摘要算法,不可逆转。

import HmacMD5 from "crypto-js/hmac-md5";
import HmacRIPEMD160 from "crypto-js/hmac-ripemd160";
import HmacSHA1 from "crypto-js/hmac-sha1";
import HmacSHA224 from "crypto-js/hmac-sha224";
import HmacSHA256 from "crypto-js/hmac-sha256";
import HmacSHA384 from "crypto-js/hmac-sha384";
import HmacSHA512 from "crypto-js/hmac-sha512";
import HmacSHA3 from "crypto-js/hmac-sha3";

var cipher1 = HmacMD5("明文", "秘钥").toString();
var cipher2 = HmacRIPEMD160("明文", "秘钥").toString();
var cipher3 = HmacSHA1("明文", "秘钥").toString();
var cipher4 = HmacSHA224("明文", "秘钥").toString();
var cipher5 = HmacSHA256("明文", "秘钥").toString();
var cipher6 = HmacSHA384("明文", "秘钥").toString();
var cipher7 = HmacSHA512("明文", "秘钥").toString();
var cipher8 = HmacSHA3("明文", "秘钥").toString();

PBKDF2算法

使用秘钥并多次迭代的摘要算法,不可逆转。

import PBKDF2 from "crypto-js/pbkdf2";

var cipher = PBKDF2("明文", "秘钥", {
    keySize: 128 / 32, // 默认128位
    iterations: 1000, // 迭代次数,默认1次,正常至少应该1000次
}).toString();

对称加密算法

可逆转,加密和解密的秘钥相同。

import AES from "crypto-js/aes";
import TripleDES from "crypto-js/tripledes";
import RC4 from "crypto-js/rc4";
import Rabbit from "crypto-js/rabbit";
import Utf8 from "crypto-js/enc-utf8";
import ECB from "crypto-js/mode-ecb";

let text = "明文";
// 字符串密钥
let key = "字符串密钥";
// 字节数组密钥(必须16个字节的整数倍,数字字母占1字节,汉字占3字节)
let keyBytes = Utf8.parse("0123456789abcdef一二三四五6");

// CBC模式(默认),使用字符串密钥,每次生成密文不同
var cipher1 = AES.encrypt(text, key).toString();
var plain1 = AES.decrypt(cipher1, key).toString(Utf8);

// ECB模式,使用字节数组密钥,生成密文固定
var cipher2 = AES.encrypt(text, keyBytes, { mode: ECB }).toString();
var plain2 = AES.decrypt(cipher2, keyBytes, { mode: ECB }).toString(Utf8);

var cipher3 = TripleDES.encrypt(text, key).toString();
var plain3 = TripleDES.decrypt(cipher3, key).toString(Utf8);

var cipher4 = TripleDES.encrypt(text, keyBytes, { mode: ECB }).toString();
var plain4 = TripleDES.decrypt(cipher4, keyBytes, { mode: ECB }).toString(Utf8);

var cipher5 = RC4.encrypt(text, key).toString();
var plain5 = RC4.decrypt(cipher5, key).toString(Utf8);

var cipher6 = RC4.encrypt(text, keyBytes, { mode: ECB }).toString();
var plain6 = RC4.decrypt(cipher6, keyBytes, { mode: ECB }).toString(Utf8);

// Rabbit不支持ECB模式
var cipher7 = Rabbit.encrypt(text, key).toString();
var plain5 = Rabbit.decrypt(cipher7, key).toString(Utf8);

非对称加密算法

可逆转,加密和解密的秘钥不同。相关算法有:RSA,DSA,ECC(没在 CryptoJs 中)。

Last Updated 2024/4/23 17:36:57