CryptoJs是google推出的一款前段解密类库.功能强大,包含很多的前段解密算法.
一.google下载地址:
二次开发版本
google原版地址
二.常用方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Testing websockets</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- <script src="crypto-js.js"></script> -->
<script src="tripledes.js"></script>
<script type="text/javascript">
var key = ‘BOTWAVEE‘;
//CBC模式加密 utf8 to base64
function encryptByDESModeCBCUtf8to64(message,key,iv) {
//var message=CryptoJS.enc.Utf8.parse(message);
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(iv);
encrypted = CryptoJS.DES.encrypt(message, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
// padding:CryptoJS.pad.Pkcs7
}
);
//加密 成Base64
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}
function encryptByDESModeCBCUtf8to(message,key,iv) {
var message=CryptoJS.enc.Utf8.parse(message);
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(iv);
encrypted = CryptoJS.DES.encrypt(message, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
}
);
//加密 成Base64
return encrypted.ciphertext.toString();
// CryptoJS.enc.Base64
}
//CBC模式解密
function decryptByDESModeCBC(ciphertext2,key,iv) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(iv);
// CryptoJS.enc.Base64.parse(
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(ciphertext2)
}, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
//CBC模式解密
function decryptByDESModeCBCHex(ciphertext2,key,iv) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(iv);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext2)
}, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
//DES ECB模式加密
function encryptByDESModeEBC(message){
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.ciphertext.toString();
}
//DES ECB模式解密
function decryptByDESModeEBC(ciphertext){
var keyHex = CryptoJS.enc.Utf8.parse(key);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var result_value = decrypted.toString(CryptoJS.enc.Utf8);
return result_value;
}
function test0(){
var source = $("#source").val();
// CryptoJS.enc.Utf8.parse(
var cc = encryptByDESModeCBCUtf8to(source,"rrettre","234234234");
$("#target").val(cc);
}
function test(){
var source = $("#source").val();
// CryptoJS.enc.Utf8.parse(
var cc = encryptByDESModeCBCUtf8to64(source,"rrettre","234234234");
$("#target").val(cc);
}
function test1(){
var source = $("#sourceS").val();
var dd = decryptByDESModeCBC(source,"rrettre","234234234");
$("#jiemi").val(dd);
}
function test2(){
var source = $("#sourceS").val();
var dd = decryptByDESModeCBCHex(source,"rrettre","234234234");
$("#jiemi").val(dd);
}
</script>
</head>
<body>
<div>
加密前:<textarea id="source" value="" style="width:500px;height:90px;" /></textarea>
<hr>
加密后:<textarea id="target" value="" style="width:500px;height:90px;" ></textarea>
<hr>
<input type="button" onclick="test();" name="" value="加密" />
<input type="button" onclick="test0();" name="" value="加密" />
<hr>
密文:<textarea id="sourceS" value="" width="400px" style="width:500px;height:90px;" ></textarea>
<hr>
解密后:<textarea id="jiemi" value="" style="width:500px;height:90px;" ></textarea>
<hr>
<input type="button" onclick="test1();" name="" value="解密"/>
<input type="button" onclick="test2();" name="" value="解密Hex"/>
</div>
</body>
</html>
三.心得体会
1.google版本的源码,可以选择一种解密方式的文件,非常适合对解密文件大小有限制的项目.
2.二次开发版本,有全量包,跟google的使用方式相同.
四.微信小程序使用CryptoJs.
微信小程序引用类库文件,需要将对象 以module.export的形式,或其他小程序支持的形式输出,才能正常使用.
例如一下方式: