java Input length must be multiple of 16 when decrypting with padded cipher
时间: 2023-12-08 14:38:52 浏览: 372
根据引用[1]中的错误提示信息,当使用填充密码解密时,输入长度必须是8的倍数。这通常是因为加密时使用了块密码模式,例如AES,而块密码模式要求输入长度必须是块大小的倍数。因此,如果输入长度不是块大小的倍数,则需要使用填充来填充输入以满足块大小的要求。
解决此问题的方法是确保输入长度是块大小的倍数。如果您正在使用填充,请确保填充的长度是正确的。如果您正在使用不同的块密码模式,请确保输入长度符合所选模式的要求。
如果您无法更改输入长度,则可以尝试使用无填充模式,例如ECB模式。但是,请注意,ECB模式不是安全的块密码模式,因为它不会隐藏明文中的模式。
相关问题
Input length must be multiple of 16 when decrypting with padded cipher
As an AI language model, I can tell you that this error message appears when you are trying to decrypt data that has been encrypted using a padding scheme, such as PKCS#7 or ISO/IEC 7816-4. Padding is used to ensure that the data being encrypted is a multiple of the block size of the cipher being used, which is typically 16 bytes for many block ciphers.
When you decrypt the data, the padding needs to be removed in order to obtain the original plaintext. However, if the input length is not a multiple of 16 bytes, it means that the padding has not been applied correctly or has been corrupted in some way.
To resolve this error, you should check that the data being decrypted is the same length as the original plaintext, minus any padding that may have been added. You should also check that the padding scheme used for encryption is compatible with the decryption algorithm being used.
java AES解密异常 Input length must be multiple of 16 when decrypting with padded cipher
该异常通常是由于加密数据的长度不是16的倍数导致的。在使用AES解密时,加密数据的长度必须是16的倍数,否则就会出现该异常。为了解决这个问题,可以在加密数据之前将其填充到16的倍数长度,然后再进行加密。常用的填充方式有PKCS5Padding和PKCS7Padding两种。
以下是一个Java AES解密的例子,其中使用了PKCS5Padding填充方式:
```java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
public static String decrypt(String encryptedData, String key) throws Exception {
byte[] keyBytes = key.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}
```
阅读全文