一:引入需要的cryptojs包
<script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script>
2:DES加密解密方法如下
//DES加密
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.ciphertext.toString();
}
//DES加密
function decryptByDES(ciphertext, key){
var keyHex = CryptoJS.enc.Utf8.parse(key);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var result_value = decrypted.toString(CryptoJS.enc.Utf8);
return result_value;
}
3:使用
var message = '111111';//需要加密的数据
var key = '12345678';//加密key
//加密
desMessage = encryptByDES(message, key);
console.log(desMessage);
//解密
message = decryptByDES(desMessage,key)
console.log(message);
这里我们就实现了js的加密解密