Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher?
    primarykey
    data
    text
    <p>Using tomcat, I have two web-applications i.e app1 and app2. I sent url from app1 in encrypted form (using below code) to app2 . Then at app2 I decrypted this encrypted url. But am getting below exception at line 50 of <code>decryp</code> method. </p> <pre><code>"Getting javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher" </code></pre> <p>Though for debugging when I try to decrypt(using same code) the encrypted url at app1, it works fine. But not able to figure out what causing this exception at app2?</p> <p><strong>Here is the code</strong></p> <pre><code> import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class AESEncryptionDecryptionTest { private static final String ALGORITHM = "AES"; private static final String myEncryptionKey = "ThisIsFoundation"; private static final String UNICODE_FORMAT = "UTF8"; public static String encrypt(String valueToEnc) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.ENCRYPT_MODE, key); byte[] encValue = c.doFinal(valueToEnc.getBytes()); String encryptedValue = new BASE64Encoder().encode(encValue); return encryptedValue; } public static String decrypt(String encryptedValue) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue); byte[] decValue = c.doFinal(decordedValue);//////////LINE 50 String decryptedValue = new String(decValue); return decryptedValue; } private static Key generateKey() throws Exception { byte[] keyAsBytes; keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); Key key = new SecretKeySpec(keyAsBytes, ALGORITHM); return key; } public static void main(String[] args) throws Exception { String value = "password1"; String valueEnc = AESEncryptionDecryptionTest.encrypt(value); String valueDec = AESEncryptionDecryptionTest.decrypt(valueEnc); System.out.println("Plain Text : " + value); System.out.println("Encrypted : " + valueEnc); System.out.println("Decrypted : " + valueDec); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload