一、下載crypto.js文件庫javascript
https://github.com/brix/crypto-js/releasesjava
二、引入文件jquery
<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>
三、加解密git
//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); }