Android AES加密报错处理:javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_in...
时间: 2024-03-09 22:45:57 浏览: 457
这个错误通常是由于加密数据的长度与AES加密算法的块大小不匹配导致的。AES算法的块大小为128位,即16个字节。如果加密的数据长度不是16的倍数,那么就需要进行填充(padding)操作,使其长度满足要求。
在Android中,可以使用PKCS7Padding或者ZeroPadding两种填充方式。其中,PKCS7Padding是常用的填充方式,可以保证填充数据的长度和填充数据的值都是正确的。而ZeroPadding则是在数据末尾补0,不保证填充数据的长度和填充数据的值都是正确的。
所以,你可以尝试修改你的加密代码,添加相应的填充方式:
```
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
byte[] encryptedData = cipher.doFinal(data);
```
如果还是出现相同的错误,可以检查一下加密数据的长度是否是16的倍数,或者尝试使用ZeroPadding进行填充。
相关问题
javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH
这个异常通常表示在使用加密算法时出现了错误的最终块长度。可能的原因之一是,解密的数据块长度与加密时使用的密钥长度不匹配。
要解决这个问题,你可以检查以下几个方面:
1. 确保使用相同的密钥进行加密和解密操作。
2. 检查加密和解密的数据块长度是否一致。
3. 如果你使用的是对称加密算法(如AES),确保密钥长度符合算法要求。
4. 如果你使用的是非对称加密算法(如RSA),检查公钥和私钥是否匹配。
如果以上步骤都没有解决问题,可能还需要进一步检查代码实现和算法配置是否正确。如果问题仍然存在,提供更多的上下文和代码示例可能有助于更准确地定位问题。
报错javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher 的解决方法
引用中提到,当使用AES解密时,报错javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher的意思是解密字符串的数组长度必须是16的倍数。所以解决这个问题的方法就是确保传入解密方法的字符串长度是16的倍数。你可以在加密之前,将原始字符串进行填充,使其长度符合要求。在解密之前,也需要确保解密字符串的长度是16的倍数。具体的实现方法可以参考代码中的示例中的相关部分。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* [javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padd](https://blog.csdn.net/weixin_45915335/article/details/124152930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
- *2* [javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with pad](https://blog.csdn.net/qq_30624649/article/details/114937740)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
- *3* [java AES 加密,报javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when ...](https://blog.csdn.net/qq_36205206/article/details/119993785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)