本文实例讲述了JS使用插件cryptojs进行加密解密数据。分享给大家供大家参考,具体如下:&l
本文实例讲述了JS使用插件cryptojs进行加密解密数据。分享给大家供大家参考,具体如下:
<!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="jquery-1.10.1.js"></script>
<script src="tripledes.js"></script>
<script src="mode-ecb-min.js"></script>
<script type="text/javascript">
var key = 'BOTWAVEE';
//CBC模式加密
function encryptByDESModeCBC(message) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(key);
encrypted = CryptoJS.DES.encrypt(message, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
}
);
return encrypted.ciphertext.toString();
}
//CBC模式解密
function decryptByDESModeCBC(ciphertext2) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(key);
// 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 test(){
var source = $("#source").val();
var cc = encryptByDESModeEBC(CryptoJS.enc.Utf8.parse(source));
$("#target").val(cc);
}
function test1(){
var source = $("#sourceS").val();
var dd = decryptByDESModeEBC(source);
$("#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="加密" />
<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="解密"/>
</div>
</body>
</html>
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
BASE64编码解码工具: http://tools.jb51.net/transcoding/base64
密码安全性在线检测: http://tools.jb51.net/password/my_password_safe
高强度密码生成器: http://tools.jb51.net/password/CreateStrongPassword
MD5在线加密工具: http://tools.jb51.net/password/CreateMD5Password
在线散列/哈希算法加密工具: http://tools.jb51.net/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具: http://tools.jb51.net/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具: http://tools.jb51.net/password/sha_encode
JS 插件 cryptojs 加密 解密