在我的应用程序中,我使用secretKey加密和解密数据。为此,我正在使用AES算法。但我在解密中得到了异常,三个已经使用密钥加密的值中的一个值。非法块大小异常在使用填充密码解密时,输入长度必须是16的倍数
的例外是:
Illegal Block Size Exception Input length must be multiple of 16 when decrypting with padded cipher.
下面是我的代码:
功能来encyrpt值
public static String symmetricEncrypt(String text, String secretKey) {
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw;
String encryptedString;
SecretKeySpec skeySpec;
BASE64Encoder bASE64Encoder = new BASE64Encoder();
byte[] encryptText = text.getBytes();
Cipher cipher;
try {
raw = decoder.decodeBuffer(secretKey);
skeySpec = new SecretKeySpec(raw, "AES");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
encryptedString = bASE64Encoder.encode(cipher.doFinal(encryptText));
}
catch (Exception e) {
e.printStackTrace();
return "Error";
}
return encryptedString;
}
功能解密值
public static String symmetricDecrypt(String text, String secretKey) {
BASE64Decoder decoder = new BASE64Decoder();
BASE64Decoder base64Decoder = new BASE64Decoder();
Cipher cipher;
String encryptedString;
byte[] encryptText = null;
byte[] raw;
SecretKeySpec skeySpec;
try {
raw = decoder.decodeBuffer(secretKey);
skeySpec = new SecretKeySpec(raw, "AES");
encryptText = base64Decoder.decodeBuffer(text);
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
encryptedString = new String(cipher.doFinal(encryptText));
} catch (Exception e) {
e.printStackTrace();
return "Error";
}
return encryptedString;
}
下面是我在加密和解密
String secretKey = "XMzDdG4D03CKm2IxIWQw7g==";
String value1= "ABCD";
String enctypedValue1= "3uweh4pzoVyH1uODQmVNJA==";
String enctypedValue2= "37PTC20w4DMZYjG3f+GWepSvAbEJUccMXwS/lXilLav1qM/PrCTdontw5/82OdC1zzyhDEsFVRGo rV6gXAQcm+Zai15hliiUQ8l8KRMtUl4=";
String value4= "20000";
/** Ecnryption and decryption of value1 **/
String encryptedValue1= symmetricEncrypt(value1, secretKey);
String decryptedValue1 = symmetricDecrypt(encryptedValue1, secretKey);
/** Decryption of enctypedValue1 **/
String decryptedValue2 = symmetricDecrypt(enctypedValue1, secretKey);
System.out.println(decryptedValue2);
/** Decryption of enctypedValue2 (Not decrypted)**/
String decryptedValue3 = symmetricDecrypt(enctypedValue2, secretKey);
System.out.println(decryptedValue3);
/** Ecnryption and decryption of value4 **/
String encryptedValue4= symmetricEncrypt(value4, secretKey);
String decryptedValue4 = symmetricDecrypt(encryptedValue4, secretKey);
在测试函数的值,我写了下面的三个测试用例。
- 使用密钥对新值(值1)进行加密和解密。
- 两个使用相同密钥解密的示例加密值(enctypedValue1,enctypedValue2)。 encryptedValue2在使用相同密钥解密时出现问题。
- 使用密钥加密和解密的新值(值4)。
在解密encryptedValue2我得到以下异常:
Illegal Block Size Exception Input length must be multiple of 16 when decrypting with padded cipher
以下是我所得到的至今。
有问题的值在解码时似乎有问题,它返回81长度数组,它无法解密?
如果这个问题发生了,它应该发生在所有的值上。
这是一个价值特定的问题,或者它是与填充有关的东西,或者它可以在不同的浏览器,不同的操作系统上有不同的行为?
,你使用'密码=密码。getInstance(“AES/ECB/PKCS5Padding”);'但是,在你的加密方法中,你使用了'cipher = Cipher.getInstance(“AES”);'。他们需要是一样的。使用加密方法将填充添加到密码中。 – Multithreader
此链接可能会有所帮助:http://stackoverflow.com/questions/7640463/encryptionexception-javax-crypto-illegalblocksizeexception-input-length-must-b –
@Multithreader感谢您的更正。其实它写错了。请参阅编辑的问题。 – RAS