vue AES 加密string和json
2020-01-13 本文已影响0人
Hello杨先生
npm install crypto-js --save-dev
第一个加密string,写在外部js文件里面,模块化
第二个加密JSON,写在main.js里面,写在原型上
方法1
111111111111111第一步11111111111111111
util.js
const CryptoJS = require('crypto-js'); //引用AES源码js
const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); //十六位十六进制数作为密钥偏移量
//加密方法
export function Encrypt(word){
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
}
//解密方法
export function Decrypt(word){
let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
22222222222222第二步2222222222222
import { Encrypt , Decrypt } from '../../../html/assets/js/utils'
333333333333333第三步333333333333333333
mounted
this.test()
44444444444444四步444444444444
method
test(){
//测试加密
console.log("*************************************")
var chepai = 'A88888'
console.log('加密前',chepai)
var Encryptchepai = Encrypt(chepai)
console.log('加密了',Encryptchepai)
var Decryptchepai = Decrypt(Encryptchepai)
console.log('解密后',Decryptchepai)
}
方法2
111111111111111第一步11111111111111111
main.js
const CryptoJS = require('crypto-js'); //引用AES源码js
const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); //十六位十六进制数作为密钥偏移量
//加密方法
Vue.prototype.$Encrypt = function Encrypt(word){
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
}
//解密方法
Vue.prototype.$Decrypt= function Decrypt(word){
let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
22222222222222第二步2222222222222
mounted
this.test()
333333333333333第三步333333333333333333
method
test(){
var client = { //业务查询
license_code:'A88888',//license_code 车牌号
clientId:"8d7adwsaFea254732",//clientId 客户编号
createChannel:"中国银行",//createChannel 办理渠道
clientCreateDate:"2019/09/09 09:38:52",//clientCreateDate 用户办理时间
}
//测试加密
console.log("*************************************")
var chepai = client
console.log('JSON加密前',chepai)
var Encryptchepai = this.$Encrypt(JSON.stringify(chepai))
console.log('JSON加密了',Encryptchepai)
var Decryptchepai = JSON.parse(this.$Decrypt(Encryptchepai))
console.log('JSON解密后',Decryptchepai)
}
运行结果
image.png