In my application, I encrypt and decrypt data using secretKey. For this, I use the AES algorithm. But I get an exception in the decryption, one value out of the three already encrypted values ββusing the secret key.
An exception:
Illegal Block Size Exception Input length must be multiple of 16 when decrypting with padded cipher.
Below is my code:
Function for encyrpt value
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; }
Value decryption function
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; }
Below are the values ββthat I encrypt and decrypt
String secretKey = "XMzDdG4D03CKm2IxIWQw7g=="; String value1= "ABCD"; String enctypedValue1= "3uweh4pzoVyH1uODQmVNJA=="; String enctypedValue2= "37PTC20w4DMZYjG3f+GWepSvAbEJUccMXwS/lXilLav1qM/PrCTdontw5/82OdC1zzyhDEsFVRGo rV6gXAQcm+Zai15hliiUQ8l8KRMtUl4="; String value4= "20000"; String encryptedValue1= symmetricEncrypt(value1, secretKey); String decryptedValue1 = symmetricDecrypt(encryptedValue1, secretKey); String decryptedValue2 = symmetricDecrypt(enctypedValue1, secretKey); System.out.println(decryptedValue2); String decryptedValue3 = symmetricDecrypt(enctypedValue2, secretKey); System.out.println(decryptedValue3); String encryptedValue4= symmetricEncrypt(value4, secretKey); String decryptedValue4 = symmetricDecrypt(encryptedValue4, secretKey);
In the test function, I wrote the following three test cases.
- The new value (value1) is encrypted and decrypted using the private key.
- Two examples of encrypted values ββ(enctypedValue1, enctypedValue2) that are decrypted using the same secret key. encryptedValue2, which received a problem decrypting using the same secret key.
- The new value (value 4) is encrypted and decrypted using the secret key.
When decrypting encryptedValue2, I get the following exception:
Illegal Block Size Exception Input length must be multiple of 16 when decrypting with padded cipher
Here is what I got so far.
The problem value seems to have a problem when decoding, it returns an 81 array of length that cannot be decrypted?
If this problem should happen, it should have happened with all the meanings.
Is this a value-specific issue, or is it related to the add-on, or may it have different behavior in different browsers, different os?