Posted in Javascript onAugust 16, 2018
本文实例讲述了JS加密插件CryptoJS实现的DES加密。分享给大家供大家参考,具体如下:
前面一篇《JS加密插件CryptoJS实现AES加密操作》介绍了CryptoJS插件的简单配置与使用,这里再来看看CryptoJS实现DES加密的方法:
<!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>
tripledes.js 为 DES主文件 自带 CBC模块
mode-ecb.js 为 DES ECB模块
使用CBC模式引入 tripledes.js
使用 ECB模式引入 tripledes.js + mode-ecb.js
JS加密插件CryptoJS实现的DES加密示例
- Author -
酷炫神话声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@