Exception:javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
偶發性異常:沒登陸時用AES/CBC/PKCS7Padding,登陸後用AES/CBC/PKCS5Padding;java
public static String decrypt(String keyStr, String ivStr, String encDataStr)throws Exception { try { logger.info("使用 AES/CBC/PKCS5Padding 解密手機號"); byte[] encData = Base64Util.decode(encDataStr); byte[] iv =Base64Util.decode(ivStr); byte[] key = Base64Util.decode(keyStr); AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); return new String(cipher.doFinal(encData),"UTF-8"); }catch (Exception e){ logger.info("使用 AES/CBC/PKCS7Padding 解密手機號"); byte[] encData = Base64Util.decode(encDataStr); byte[] iv =Base64Util.decode(ivStr); byte[] key = Base64Util.decode(keyStr); AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); return new String(cipher.doFinal(encData),"UTF-8"); } }