Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8181140

cipher.doFinal(data) throws BadPaddingException starting with Java 8u112

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      OS Name: Microsoft Windows 8.1 Enterprise
      OS Version: 6.3.9600 N/A Build 9600
      OS Manufacturer: Microsoft Corporation
      OS Configuration: Member Workstation
      OS Build Type: Multiprocessor Free

      A DESCRIPTION OF THE PROBLEM :
      Beginning with Java 8 Java 8u112, the cipher.doFinal(data) call throws the following exception :
      javax.crypto.BadPaddingException: Given final block not properly padded

      The same code works fine with Java 8 Java 8u111 ( or prior ).

      REGRESSION. Last worked in version 8u111

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.8.0_111"
      Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The source code below shows the problem. Just running the source code with Java 8u112 throws an exception described below. Running the same source code with Java 8u11q does not throw exception and prints Successs !!!

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Console output :
        Successs !!!
      ACTUAL -
      javax.crypto.BadPaddingException: Given final block not properly padded
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:989)
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
      at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
      at javax.crypto.Cipher.doFinal(Cipher.java:2165)
      at com.symitar.datastore.DoFinalBug.newDecodeTest(DoFinalBug.java:27)
      at com.symitar.datastore.DoFinalBug.main(DoFinalBug.java:67)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.nio.ByteBuffer;
      import java.nio.CharBuffer;
      import java.security.spec.AlgorithmParameterSpec;
      import javax.crypto.Cipher;
      import javax.crypto.SecretKey;
      import javax.crypto.spec.IvParameterSpec;
      import javax.crypto.spec.SecretKeySpec;

      public class DoFinalBug
      {

           public void newDecodeTest(final String expectedClearString, final byte[] keyData, final byte[] ivData,
                        final byte[] encryptedData)
           {
                 try
                 {
                        SecretKey aesKey = new SecretKeySpec(keyData, "AES");
                        AlgorithmParameterSpec iv = new IvParameterSpec(ivData);
                        
                        Cipher cipher = Cipher.getInstance("AES/OFB32/PKCS5PADDING");
                        cipher.init(Cipher.DECRYPT_MODE, aesKey, iv);
                        
                        byte[] clearData = cipher.doFinal(encryptedData);
                        
                        String clearText = new String(convertByteArrayToCharArray(clearData));
                        if(clearText.equals(expectedClearString))
                        {
                       System.out.println("Successs !!! ");
                        }
                        
                 }
                 catch (Exception e)
                 {
                        e.printStackTrace();
                 }
           }
           
           public static char[] convertByteArrayToCharArray(final byte[] source)
           {
                 if (source == null)
                 {
                        throw new IllegalArgumentException("source is null.");
                 }

                 CharBuffer sourceCharBuffer = ByteBuffer.wrap(source).asCharBuffer();

                 char[] result = new char[sourceCharBuffer.length()];
                 for (int i = 0; i < result.length; i++)
                 {
                        result[i] = sourceCharBuffer.get();
                 }
                 return result;
           }
           
           public static void main (String[] args)
           {
           DoFinalBug doFinalBug = new DoFinalBug();
           String expectedClearString = "Xyz";
           byte[] keyData= new byte[] {-20, -67, 94, -85, -125, -113, -15, 29, -100, 69, 124, -84, -64, -2, 21, -1};
           byte[] ivData=new byte[] {-52, 10, -84, 57, -100, 6, -60, 92, -127, -47, -60, 92, -32, -77, -24, 53};
           byte[] encryptedData = new byte[] {-100, -8, -97, 116, 81, -58, -61, -42, 3, 72, 62, -32, -49, 84, -2, 73 };
          
           doFinalBug.newDecodeTest(expectedClearString, keyData, ivData, encryptedData);
          
           }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround found

      Attachments

        Activity

          People

            coffeys Sean Coffey
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: