From: https://www.cnblogs.com/CyLee/p/7216988.htmlhtml
傳送門:git
# crypto-js github https://github.com/brix/crypto-js
demo1:github
// 加載核心加密庫 var CryptoJS = require("crypto-js"); // 加載des算法 var tripledes = require("crypto-js/tripledes"); // 開始加密,而且返回密文 var ciphertext = tripledes.encrypt("fuckyou", '123').toString(); // 解密 var plaintext = tripledes.decrypt(ciphertext, '123').toString(CryptoJS.enc.Utf8) // 輸出密文和解密後的內容 console.log(ciphertext, plaintext)
demo2:算法
// 加載核心加密庫 var CryptoJS = require("crypto-js"); function encrypt (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 { key: keyHex, value: encrypted.toString() } } function decrypt (message, key) { var plaintext = CryptoJS.DES.decrypt(message, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) return plaintext.toString(CryptoJS.enc.Utf8) } var a = encrypt('mssage123', '123'); var b = decrypt(a.value, a.key); console.log(a.value.length)