传送门:
# crypto-js githubhttps://github.com/brix/crypto-js
demo1:
// 加载核心加密库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)