CryptoJS-为JavaScript提供了各种各样的加密算法

支持的算法包括:
MD5,SHA-1,SHA-256,AES,Rabbit,MARC4,HMAC,HMAC-MD5,HMAC-SHA1,HMAC-SHA256,PBKDF2

var CryptoJS = require("cryptojs");
CryptoJS.require.Hmac(); //如果需要计算HMAC值,必须引用Hmac模块。

摘要算法

MD5/HmacMD5

用框架内置的MD5同样的结果。

CryptoJS.require.MD5();
F.echo(CryptoJS.MD5("anlige"), true);
F.echo(CryptoJS.HmacMD5("anlige", "lilith"), true);

SHA1/HmacSHA1

用框架内置的SHA1同样的结果。

CryptoJS.require.SHA1();
F.echo(CryptoJS.SHA1("anlige"), true);
F.echo(CryptoJS.HmacSHA1("anlige", "lilith"), true);

SHA3/HmacSHA3

CryptoJS.require("sha3","x64-core"); //需要x64-core模块支持
F.echo(CryptoJS.SHA3("anlige"), true);
F.echo(CryptoJS.HmacSHA3("anlige", "lilith"), true);

SHA224/HmacSHA224

CryptoJS.require("sha224","sha256"); //需要sha256模块支持
F.echo(CryptoJS.SHA224("anlige"), true);
F.echo(CryptoJS.HmacSHA224("anlige", "lilith"), true);

SHA256/HmacSHA256

CryptoJS.require("sha256");
F.echo(CryptoJS.SHA256("anlige"), true);
F.echo(CryptoJS.HmacSHA256("anlige", "lilith"), true);

SHA384/HmacSHA384

CryptoJS.require("sha384","x64-core","sha512");
F.echo(CryptoJS.SHA384("anlige"), true);
F.echo(CryptoJS.HmacSHA384("anlige", "lilith"), true);

SHA512/HmacSHA512

CryptoJS.require("sha512","x64-core");
F.echo(CryptoJS.SHA512("anlige"), true);
F.echo(CryptoJS.HmacSHA512("anlige", "lilith"), true);

PBKDF2

CryptoJS.require.PBKDF2();
var result = CryptoJS.PBKDF2("anlige", "lilith", {keySize: 128/32,iterations: 10});
F.echo(result);

EvpKDF

CryptoJS.require.EvpKDF();
var result = CryptoJS.EvpKDF("anlige", "lilith", {keySize: 128/32,iterations: 10});
F.echo(result);

RIPEMD160

CryptoJS.require("ripemd160");
var result = CryptoJS.RIPEMD160("anlige");
F.echo(result);

编码算法

Base64

CryptoJS.require("enc-base64");
//编码
var src = CryptoJS.enc.Utf8.parse("anlige");
var base64string = CryptoJS.enc.Base64.stringify(src);
F.echo(base64string.toString());

//解码
var base64string = CryptoJS.enc.Base64.parse("YW5saWdl");
F.echo(CryptoJS.enc.Utf8.stringify(base64string));
}

加密算法

RC4

CryptoJS.require.RC4().Format.Hex();
var key = CryptoJS.enc.Utf8.parse("password");
//加密
var srcs = CryptoJS.enc.Utf8.parse("anlige");
F.echo(CryptoJS.RC4.encrypt(srcs, key).toString(CryptoJS.format.Hex));
//解密
var srcs = CryptoJS.enc.Hex.parse("9e9b54672b5b");
var decryptdata = CryptoJS.RC4.decrypt(
    CryptoJS.lib.CipherParams.create({ ciphertext:srcs}),
    key
); 
//解密
F.echo(decryptdata.toString(CryptoJS.enc.Utf8));

RabbitLegacy

CryptoJS.require.Padding().Mode();
CryptoJS.require.RabbitLegacy().Format.Hex();
var key = CryptoJS.enc.Utf8.parse("f2c20576f726c641");
var iv  = CryptoJS.enc.Utf8.parse("6f726c64f2c20572");
var cfg={
    iv: iv,
    mode:CryptoJS.mode.CFB,
    padding:CryptoJS.pad.Pkcs7,
    format:CryptoJS.format.Hex
};
//加密
var srcs = CryptoJS.enc.Utf8.parse("anlige测试");
F.echo(CryptoJS.RabbitLegacy.encrypt(srcs, key, cfg).toString(), true);
//解密
var srcs = CryptoJS.enc.Hex.parse("71d7f64fb887f384057c4798");
var decryptdata = CryptoJS.RabbitLegacy.decrypt(
    CryptoJS.lib.CipherParams.create({ ciphertext:srcs}),
    key,
    cfg
);
F.echo(decryptdata.toString(CryptoJS.enc.Utf8));

Rabbit

CryptoJS.require.Padding().Mode();
CryptoJS.require.Rabbit().Format.Hex();
CryptoJS.require.RabbitLegacy();
var key = CryptoJS.enc.Utf8.parse("f2c20576f726c641");
var iv  = CryptoJS.enc.Utf8.parse("6f726c64f2c20572");
var cfg={
    iv: iv,
    mode:CryptoJS.mode.CFB,
    padding:CryptoJS.pad.Pkcs7,
    format:CryptoJS.format.Hex
};
//加密
var srcs = CryptoJS.enc.Utf8.parse("anlige测试");
F.echo(CryptoJS.Rabbit.encrypt(srcs, key, cfg).toString(), true);
//解密
var srcs = CryptoJS.enc.Hex.parse("fe82ea8e0d3582e60871c394");
var decryptdata = CryptoJS.RabbitLegacy.decrypt(
    CryptoJS.lib.CipherParams.create({ ciphertext:srcs}),
    key,
    cfg
);
F.echo(decryptdata.toString(CryptoJS.enc.Utf8));

AES

mode支持:CBC,CFB,CTR,CTRGladman,ECB,OFB
padding支持:ZeroPadding,NoPadding,AnsiX923,Iso10126,Iso97971,Pkcs7

CryptoJS.require.Padding().Mode();
CryptoJS.require.AES().Format.Hex();
var key = CryptoJS.enc.Utf8.parse("f2c20576f726c641");
var iv  = CryptoJS.enc.Utf8.parse("6f726c64f2c20572");
var cfg={
    iv: iv,
    mode:CryptoJS.mode.CBC,
    padding:CryptoJS.pad.Pkcs7,
    format:CryptoJS.format.Hex
};
//加密
var srcs = CryptoJS.enc.Utf8.parse("anlige测试");
F.echo(CryptoJS.AES.encrypt(srcs, key, cfg).toString(), true);
//解密
var srcs = CryptoJS.enc.Hex.parse("2783726e20d0d334520cbbbb9cd95d9b");
var decryptdata = CryptoJS.AES.decrypt(
    CryptoJS.lib.CipherParams.create({ ciphertext:srcs}),
    key,
    cfg
);
F.echo(decryptdata.toString(CryptoJS.enc.Utf8));

DES

mode支持:CBC,CFB,CTR,CTRGladman,ECB,OFB
padding支持:ZeroPadding,NoPadding,AnsiX923,Iso10126,Iso97971,Pkcs7

CryptoJS.require.Padding().Mode();
CryptoJS.require.DES().Format.Hex();
var key = CryptoJS.enc.Utf8.parse("f2c20576f726c641");
var iv  = CryptoJS.enc.Utf8.parse("6f726c64f2c20572");
var cfg={
    iv: iv,
    mode:CryptoJS.mode.CFB,
    padding:CryptoJS.pad.Pkcs7,
    format:CryptoJS.format.Hex
};
//加密
var srcs = CryptoJS.enc.Utf8.parse("anlige测试");
F.echo(CryptoJS.DES.encrypt(srcs, key, cfg).toString(), true);
//解密
var srcs = CryptoJS.enc.Hex.parse("9e418a0562e550d0ff42dccfeb3ece64");
var decryptdata = CryptoJS.DES.decrypt(
    CryptoJS.lib.CipherParams.create({ ciphertext:srcs}),
    key,
    cfg
);
F.echo(decryptdata.toString(CryptoJS.enc.Utf8));

发表评论评论(0)