如何在VUE-CLI手脚架建立的工程中使用3des加密:

npm install crypto-js --save-dev
import CryptoJS from 'crypto-js'
//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))

 

Logo

前往低代码交流专区

更多推荐