后端代码
//加密 data 对称AES Key byte[] key = getBytes("._^BV67nW6ck8fwg", 16);//秘钥长度最好是16位 SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key); String jsondata = aes.encryptHex("中国test"); System.out.println(jsondata); //解密,加密模式ecb,填充pkcs7,数据库128位,迁移量无,输出hex16进制,字符集utf-8 String decryptStr = aes.decryptStr(jsondata, CharsetUtil.CHARSET_UTF_8); System.out.println(decryptStr);
前端vue代码
utils文件夹下新建encryp.js
import CryptoJS from 'crypto-js' // 加密 export default { encrypt(word, keyStr, iv) { let keys = CryptoJS.enc.Utf8.parse(keyStr); let ivs = CryptoJS.enc.Utf8.parse(iv); let srcs = CryptoJS.enc.Utf8.parse(word); let encrypted = CryptoJS.AES.encrypt(srcs, keys, { iv: ivs, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); console.log("base64:" + encrypted); var encryptedStr = encrypted.ciphertext.toString(); console.log("16进制:" + encryptedStr); return encrypted.ciphertext.toString().toUpperCase(); //16进制 }, // } // 解密 decrypt(word, keyStr, iv) { // ********************解密16进制*********************** let key = CryptoJS.enc.Utf8.parse(keyStr); let ivs = CryptoJS.enc.Utf8.parse(iv); let encryptedHexStr = CryptoJS.enc.Hex.parse(word); let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: ivs, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); console.log(decryptedStr) return decryptedStr.toString(); } }
使用
import Crypto from "@/utils/encryp.js"; //很多页面要使用时也可以再main.js中定义:import Crypto from "@/utils/encryp.js";Vue.prototype.$crypto = Crypto; 页面中调用: var t = $crypto.encrypt('中国test',"._^BV67nW6ck8fwg","");
var t = Crypto.encrypt('中国test',"._^BV67nW6ck8fwg","");
console.log(t);
标签:aes,vue,java,iv,parse,enc,let,CryptoJS From: https://www.cnblogs.com/privateLogs/p/17526178.html