传送门:
# crypto-js github https://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)
时间: 2024-09-30 12:49:07