微信小程序解密Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
时间: 2023-08-19 11:17:37 浏览: 64
在微信小程序解密过程中出现"Given final block not properly padded. Such issues can arise if a bad key is used during decryption"的错误,这是由于解密时使用的密钥与加密时使用的密钥不一致所导致的。[1][2][3]在AES加解密中,需要使用一个密钥来进行加解密操作。如果解密时使用的密钥与加密时使用的密钥不匹配,就会出现这个错误。请确保在解密过程中使用的密钥与加密时使用的密钥一致。
相关问题
Android开发 编译报错 Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
在Android开发中,如果出现"Given final block not properly padded. Such issues can arise if a bad key is used during decryption"报错,通常是因为解密的密钥和加密的密钥不一致。解决方法如下:
1.检查密钥是否正确:检查加密和解密时使用的密钥是否一致,如果不一致,需要将其修改为相同的密钥。
2.检查加密算法是否正确:检查加密和解密时使用的算法是否一致,如果不一致,需要将其修改为相同的算法。
3.检查数据是否正确:检查加密和解密的数据是否正确,如果不正确,需要重新生成正确的数据。
以下是一个示例代码,用于解决Android开发中出现"Given final block not properly padded. Such issues can arise if a bad key is used during decryption"报错的问题:
```java
try {
// 加密
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(encryToken.getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(encryToken.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
// 解密
Cipher cipher2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec2 = new SecretKeySpec(encryToken.getBytes(), "AES");
IvParameterSpec ivSpec2 = new IvParameterSpec(encryToken.getBytes());
cipher2.init(Cipher.DECRYPT_MODE, keySpec2, ivSpec2);
byte[] decrypted = cipher2.doFinal(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
```
Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
"Given final block not properly padded. Such issues can arise if a bad key is used during decryption"报错是因为解密时使用了错误的密钥。在AES解密过程中,需要使用与加密过程中相同的密钥来正确解密数据。
根据引用中的代码,每次生成的SecretKey可以不同。这是因为在JDK17中,KeyGenerator生成SecretKey的方式不同于之前的版本。JDK17中的KeyGenerator使用了更安全的随机数生成方法,每次生成的密钥都会不同。而在JDK8中,使用的是基于密码种子生成随机数序列,只要种子相同,生成的密钥就相同。
为了确保数据的安全性,每次使用KeyGenerator生成SecretKey是一个好的做法。这样可以避免密钥泄露或被猜测的风险。双方约定使用同样的密钥也是可以的,但需要保证密钥的安全性和保密性。如果密钥被泄露或被破解,加密的数据将会暴露在风险中。因此,使用KeyGenerator生成密钥可以增加数据的安全性。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [“Given final block not properly padded. Such issues can arise if a bad key is used during“错误解决](https://blog.csdn.net/u012660464/article/details/128341544)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [升级到JDK17,AES解密失败:Given final block not ... Such issues can arise if a bad key is used d](https://blog.csdn.net/libai245/article/details/130300658)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]