From:  https://www.cnblogs.com/CyLee/p/7216988.html

 

传送门:

# 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)

 

 
posted on 2018-09-05 17:06  今夜太冷  阅读(3582)  评论(0编辑  收藏  举报