AI智能
改变未来

AES 解密报错:Given final block not properly padded. Such issues can arise if a bad key is used dur


问题:系统登录账号密码密文传输,用AES加密之后;登录的时候抛出:Given final block not properly padded. Such issues can arise if a bad key is used dur。

经过一系列排查,问题孵出了水面;前端加密和后端解密没对应;很尴尬…

前端代码(错误正确比较):

[code]import CryptoJS from \'crypto-js\' //错误import encrypt from \'utils/aes\'  //正确// 用户名登录Username({ commit }, userInfo) {const account = userInfo.account.trim()// const accChange = CryptoJS.AES.encrypt(account, \'secret key 123\'); //错误const accChange = encrypt.encrypt(account); //正确return new Promise((resolve, reject) => {login(accChange, userInfo.pass).then(response => {const { data } = responseif (data.resp_code === 0) {const token = data.datas.authorizationsessionStorage.setItem(\'authorization\',token)commit(\'TOKEN\', token)setToken(token)}resolve(response)}).catch(error => {reject(error)})})},

后端代码(正确):

[code]private static String SALT=\"bjbcsddskdkdkkkkdksk\";//CBC加密偏移量private static String IVCODE=\"5e8y6w45ju8w9jq8\";/*** 解密* @param encryptStr 解密的字符串* @param decryptKey 解密的key值* @return* @throws Exception*/public static String decrypt(String encryptStr, String decryptKey) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance(\"AES\");kgen.init(128);Cipher cipher = Cipher.getInstance(\"AES/CBC/PKCS5Padding\");IvParameterSpec iv = new IvParameterSpec(IVCODE.getBytes());cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), \"AES\"), iv);// 采用base64算法进行转码,避免出现中文乱码byte[] encryptBytes = Base64.decodeBase64(encryptStr);byte[] decryptBytes = cipher.doFinal(encryptBytes);return new String(decryptBytes);}

aes.js文件:

[code]import Vue from \'vue\'import CryptoJS from \'crypto-js\'//数据解密export default {decrypt(content) {var sKey = CryptoJS.enc.Utf8.parse(\"bjbcsddskdkdkkkkdksk\");var iv = CryptoJS.enc.Utf8.parse(\"5e8y6w45ju8w9jq8\");// var decrypt = CryptoJS.AES.decrypt(content, sKey, { mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7 });var decrypt = CryptoJS.AES.decrypt(content, sKey, {iv: iv,mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});var resultStr = CryptoJS.enc.Utf8.stringify(decrypt).toString();return resultStr;},encrypt(content) {var sKey = CryptoJS.enc.Utf8.parse(\"bjbcsddskdkdkkkkdksk\");var iv = CryptoJS.enc.Utf8.parse(\"5e8y6w45ju8w9jq8\");let srcs = CryptoJS.enc.Utf8.parse(content);let encrypted = CryptoJS.AES.encrypt(srcs, sKey, {iv,mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});return encrypted.toString();},}

 

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » AES 解密报错:Given final block not properly padded. Such issues can arise if a bad key is used dur