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

Cipher.doFinal need more size of recoveredText than necessary in decrypt mode

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      beagle
    • CPU:
      sparc
    • OS:
      solaris_8
    • Verification:
      Verified

      Description

      recovered text size should be equal to the size of cipher text. AES cipher can not distiguish the ENCRYPT mode and DECRYPT mode, doFinal method normally need more unnecessary recoveredText size in decryption case. The following program demonstrates this.

      import java.io.PrintStream;
      import java.security.*;
      import java.security.spec.*;
      import java.util.Random;

      import javax.crypto.*;
      import javax.crypto.spec.*;
      import java.security.Provider;
      import com.sun.advcrypto.provider.SunAES;

      public class test {

           public static void main( String argv[] ) {
          
           byte[] ip=new byte[0];
           String algorithm = "AES";
           String mode = "CBC";
          byte[] iv = null;
          String padding = "PKCS5Padding";
           int keyStrength=128;
           AlgorithmParameterSpec aps = null;
          
           try{
          
           // Initialization
      Random rdm = new Random();
      byte[] plainText=new byte[125];
      rdm.nextBytes(plainText);
      System.out.println("plain text length is "+plainText.length);
              
        Cipher ci = Cipher.getInstance( algorithm + "/" + mode + "/" + padding);
           KeyGenerator kg = KeyGenerator.getInstance( algorithm);
           kg.init(keyStrength);
           SecretKey key = kg.generateKey();

                      // encrypt
           ci.init( Cipher.ENCRYPT_MODE, key, aps);
           byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
      System.out.println("cipherText length is "+cipherText.length);
           int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
           ci.doFinal( cipherText, offset );
        
      if (!mode.equalsIgnoreCase("ECB")) {
      iv = ci.getIV();
      aps = new IvParameterSpec(iv);
      } else aps = null;


      //Decrypt
      ci.init( Cipher.DECRYPT_MODE, key, aps);
        byte[] recoveredText = new byte[125];
      System.out.println("Recovertext length is "+recoveredText.length);
           int len = ci.doFinal( cipherText, 0, cipherText.length, recoveredText );

           }catch(Exception ex){
           System.out.println("FAILED");
           ex.printStackTrace();
          
           }
           }
      }

      lwang:/home/lw129730/AES/testcode/test( 159 )%java test
      plain text length is 125
      cipherText length is 128
      Recovertext length is 125
      FAILED
      javax.crypto.ShortBufferException: The output buffer is too short
              at cryptix.jce.provider.cipher.SunAES_h.b(DashoA6275)
              at cryptix.jce.provider.cipher.BlockCipher.engineDoFinal(DashoA6275)
              at javax.crypto.Cipher.doFinal(DashoA6275)
              at test.main(test.java:90)

        Attachments

          Activity

            People

            Assignee:
            valeriep Valerie Peng
            Reporter:
            duke J. Duke (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: