怎樣操作vue.js使用3DES加密
阿新 • • 發佈:2018-08-24
con space lpad lsp dex cipher containe utf8 ons
如何在VUE-CLI手腳架建立的工程中使用3des加密:
1 |
npm install crypto-js --save-dev
|
1 |
import CryptoJS from ‘crypto-js‘
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//DES加密 Pkcs7填充方式
encryptByDES(message, key){
const keyHex = CryptoJS.enc.Utf8.parse(key);
const encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
//DES解密
decryptByDES(ciphertext, key){
const keyHex = CryptoJS.enc.Utf8.parse(key);
// direct decrypt ciphertext
const decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(ciphertext) }, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
const _key = ‘abcdefghijklmn‘
const _password = ‘123456‘
//加密
console.log(this.encryptByDES(_password,_key))
//解密
console.log(this.decryptByDES(_password,_key))
|
怎樣操作vue.js使用3DES加密