WebSphere Application Server & Liberty

WebSphere Application Server & Liberty

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

IBM Java 8 still gets IllegalBlockSizeException even though trying to decrypt valid data if IllegalBlockSizeException is once thrown.

  • 1.  IBM Java 8 still gets IllegalBlockSizeException even though trying to decrypt valid data if IllegalBlockSizeException is once thrown.

    Posted 08/17/21 07:20 AM

    Encrypted data is decrypted with javax.crypto.Cipher class in the application code.

    If trying to decrypt truncated data in the application, IllegalBlockSizeException is thrown as expected. After IllegalBlockSizeException is thrown, valid data can be decryted with the same cipher object when IBM Java 7 is used. However, IllegalBlockSizeException is still thrown in the same situation when IBM Java 8 is used. Why IBM Java 8 still gets IllegalBlockSizeException after IllegalBlockSizeException is thrown even though trying to decrypt valid data?



    #Support
    #SupportMigration
    #WebSphereApplicationServer(WAS)


  • 2.  RE: IBM Java 8 still gets IllegalBlockSizeException even though trying to decrypt valid data if IllegalBlockSizeException is once thrown.
    Best Answer

    Posted 08/17/21 07:21 AM

    The application code needs to call Cipher.init again to reset cipher object to re-use its cipher object when doFinal gets exception.

    The specification for doFinal in Cipher of Java 7 and Java 8 both indicate: "if any exception is thrown, this cipher object may need to be reset before it can be used again.".

    https://www.ibm.com/docs/en/sdk-java-technology/8?topic=jce-cipher

    https://www.ibm.com/docs/en/sdk-java-technology/7?topic=jce-cipher

    > Note: if any exception is thrown, this cipher object may need to be reset before it can be used again.

    There are different implementations for IBM Java 7 and IBM Java 8. But, both IBM Java 7 and IBM Java 8 are following the same specification for doFinal in Cipher. Therefore, the application code should be calling Cipher.init to reset cipher object like below when doFinal gets any exception for any Java or JCE version.

    ```

    try{

    cipher.doFinal(...);

    } catch (Exception e) {

    cipher.init(Cipher.DECRYPT_MODE, ...);

    }

    ```



    #Support
    #SupportMigration
    #WebSphereApplicationServer(WAS)