1、下载crypto.js文件库
https://github.com/brix/crypto-js/releases
2、引入文件
<script type="text/javascript" src="js/jquery.min.js"></script> <script src="js/rollups/tripledes.js"></script> <script src="js/components/mode-ecb.js"></script>
3、加解密
//DES 加密
// key为加密的秘钥。message 为需要加密的信息
function encryptByDES(message, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); var encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.toString(); } //DES 解密 function decryptByDES(ciphertext, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); // direct decrypt ciphertext var decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(ciphertext) }, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8); }