java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding

Spring JIRA | Jasvinder Bahra | 3 years ago
  1. 0

    According to the official documentation (see section 3.2.2 at http://docs.spring.io/spring-android/docs/1.0.1.RELEASE/reference/html/auth.html), on an Android device, a string can be encrypted using the org.springframework.security.crypto.encrypt.AndroidEncryptors class from the spring-android-auth 1.0.1.RELEASE module. SO for example... String salt = "75f4c92894b2f3e7"; String password = "password"; org.springframework.security.crypto.encrypt.TextEncryptor encryptor = org.springframework.security.crypto.encrypt.AndroidEncryptors.text(password, salt); String encryptedString = encryptor.encrypt("hello"); During one run, the encryptedString variable is then set to something like "1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b2372a47276152e2bd". On a PC, using the Encryptors class from the spring-security-crypto module, that encrypted string could then be decrypted in the following manner... String salt = "75f4c92894b2f3e7"; String password = "password"; org.springframework.security.crypto.encrypt.TextEncryptor encryptor = org.springframework.security.crypto.encrypt.Encryptors.text(password, salt); String decryptedString = encryptor.decrypt("1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b2372a47276152e2bd"); However, when the JRE hits the decrypt method, the following exception is raised... java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:125) at org.springframework.security.crypto.encrypt.AesBytesEncryptor.decrypt(AesBytesEncryptor.java:75) at org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.decrypt(HexEncodingTextEncryptor.java:40) at local.encryption.DecryptionTest.main(DecryptionTest.java:18) Caused by: javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..) at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:121) ... 3 more According to the API documentation for the AndroidEncryptors class and the Encryptors class, the relevant methods both use "a 256 bit AES algorithm which derives the secret key using PKCS #5's PBKDF2 (Password-Based Key Derivation Function #2)". However, when you drill down into the classes, the algorithms each class uses seems to be different. While I suppose its possible that both do use the same algorithm, but are named differently in each class ("PBEWITHSHA256AND256BITAES-CBC-BC" and "PBKDF2WithHmacSHA1"), the fact that the decrypt method triggers an exception in the above situation suggests otherwise.

    Spring JIRA | 3 years ago | Jasvinder Bahra
    java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding
  2. 0

    According to the official documentation (see section 3.2.2 at http://docs.spring.io/spring-android/docs/1.0.1.RELEASE/reference/html/auth.html), on an Android device, a string can be encrypted using the org.springframework.security.crypto.encrypt.AndroidEncryptors class from the spring-android-auth 1.0.1.RELEASE module. SO for example... String salt = "75f4c92894b2f3e7"; String password = "password"; org.springframework.security.crypto.encrypt.TextEncryptor encryptor = org.springframework.security.crypto.encrypt.AndroidEncryptors.text(password, salt); String encryptedString = encryptor.encrypt("hello"); During one run, the encryptedString variable is then set to something like "1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b2372a47276152e2bd". On a PC, using the Encryptors class from the spring-security-crypto module, that encrypted string could then be decrypted in the following manner... String salt = "75f4c92894b2f3e7"; String password = "password"; org.springframework.security.crypto.encrypt.TextEncryptor encryptor = org.springframework.security.crypto.encrypt.Encryptors.text(password, salt); String decryptedString = encryptor.decrypt("1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b2372a47276152e2bd"); However, when the JRE hits the decrypt method, the following exception is raised... java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:125) at org.springframework.security.crypto.encrypt.AesBytesEncryptor.decrypt(AesBytesEncryptor.java:75) at org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.decrypt(HexEncodingTextEncryptor.java:40) at local.encryption.DecryptionTest.main(DecryptionTest.java:18) Caused by: javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..) at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:121) ... 3 more According to the API documentation for the AndroidEncryptors class and the Encryptors class, the relevant methods both use "a 256 bit AES algorithm which derives the secret key using PKCS #5's PBKDF2 (Password-Based Key Derivation Function #2)". However, when you drill down into the classes, the algorithms each class uses seems to be different. While I suppose its possible that both do use the same algorithm, but are named differently in each class ("PBEWITHSHA256AND256BITAES-CBC-BC" and "PBKDF2WithHmacSHA1"), the fact that the decrypt method triggers an exception in the above situation suggests otherwise.

    Spring JIRA | 3 years ago | Jasvinder Bahra
    java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding
  3. 0

    Decrypting a String Encrypted on an Android device

    Stack Overflow | 3 years ago | Jazz
    java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    AES Java Multithreading exceptions

    Stack Overflow | 5 years ago | Marcus
    javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..)
  6. 0

    OIM: AccessPolicies: tcCryptoException: decrypt failed

    Oracle Community | 2 years ago | vegaasen
    javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..)

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. javax.crypto.BadPaddingException

      Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..)

      at org.springframework.security.crypto.encrypt.CipherUtils.doFinal()
    2. spring-security-core
      HexEncodingTextEncryptor.decrypt
      1. org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:121)
      2. org.springframework.security.crypto.encrypt.AesBytesEncryptor.decrypt(AesBytesEncryptor.java:75)
      3. org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.decrypt(HexEncodingTextEncryptor.java:40)
      3 frames
    3. local.encryption
      DecryptionTest.main
      1. local.encryption.DecryptionTest.main(DecryptionTest.java:18)
      1 frame