1. 程式人生 > >AES解密異常Given final block not properly padded-在線助手博客

AES解密異常Given final block not properly padded-在線助手博客

str con cipher pro OS pad 相關 span properly

AES 128/192/256位CBC/CFB/ECB/OFB/PCBC 在線加密解密

解密內容:1243CFEBD819AA6B1C717DE870459F7B
秘鑰:http://www.it399.com
沒有使用填充向量iv
AES解密異常Given final block not properly padded

javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
    at com.sun
.crypto.provider.CipherCore.doFinal(CipherCore.java:824) at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436) at javax.crypto.Cipher.doFinal(Cipher.java:2165)

原因: 解密的時候直接對輸入的16進制值 1243CFEBD819AA6B1C717DE870459F7B 取getBytes了,這樣獲取到的字節數組長度是16,不能直接這樣操作

inputValue.getBytes(AESUtil.CHARSET
)

需要將16進制轉字節數組,這樣獲取到的內容是32位,而且和之前直接獲取到的字節數組不一樣。

TypeConvert.hexStringToBytes(inputValue)

16進制轉字節數組方法

/**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null
|| hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; }

這樣解密之後的字符串是 在線助手 正常!!

還有一種情況是輸入的字符串不正常不能解密!

相關文章

AES 128/192/256位CBC/CFB/ECB/OFB/PCBC加密解密

http://www.it399.com/blog/web/201805211243

AES 128/192/256位CBC/CFB/ECB/OFB/PCBC在線加密解密|在線工具|在線助手|在線生成|在線制作

http://www.it399.com/aes

AES解密異常Given final block not properly padded

http://www.it399.com/blog/web/201805211406

本文同步發布在 在線助手博客|在線助手|在線工具|在線生成|在線制作 ,轉載請註明來自 在線助手博客 頻道【AES解密異常Given final block not properly padded】,原文鏈接:**http://www.it399.com/blog/web/201805211406**

AES解密異常Given final block not properly padded-在線助手博客