Abrufen von javax.crypto.IllegalBlockSizeException: Die Eingabelänge muss ein Vielfaches von 16 sein, wenn mit gepolsterter Verschlüsselung entschlüsselt wird.

Mit Tomcat habe ich zwei Webanwendungen, nämlich App1 und App2. Ich habe die URL von App1 in verschlüsselter Form (unter Verwendung des folgenden Codes) an App2 gesendet. Dann habe ich bei App2 diese verschlüsselte URL entschlüsselt. Aber bin immer unter Ausnahme in Zeile 50 vondecryp Methode.

"Getting javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher"

Obwohl für das Debuggen, wenn ich versuche, die verschlüsselte URL bei App1 zu entschlüsseln (mit demselben Code), funktioniert es gut. Aber nicht in der Lage, herauszufinden, was diese Ausnahme bei App2 verursacht?

Hier ist der 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);
}

}

Antworten auf die Frage(4)

Ihre Antwort auf die Frage