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

no IllegalStateException for javax.crypto.Cipher.doFianal() and wrap()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 5.0
    • 5.0
    • security-libs
    • b36
    • sparc
    • solaris_2.6
    • Verified

      Name: iiR10263 Date: 11/13/2003



      The specification requires Cipher objects to throw
      IllegaltateException "if this cipher is in a wrong state (e.g., has
      not been initialized)" when doFianal() or wrap() are called. For
      example we can consider that a Cipher object that is initialized for
      encryption is "in a wrong state" for wrapping.

      So it is reasonable to throw IllegalStateException if a Cipher was
      initialized for encryption but wrap() is called (or a Cipher was
      initialized for wrapping but doFinal() or update() is called).

      The implementation does not throw any exceptions in such situations.

      Please find the code example that reproduces the problem below

      import java.io.PrintWriter;
      import java.security.InvalidKeyException;
      import java.security.Key;
      import java.security.AlgorithmParameters;
      import java.security.spec.KeySpec;
      import javax.crypto.Cipher;
      import javax.crypto.SecretKeyFactory;
      import javax.crypto.spec.PBEKeySpec;
      import javax.crypto.spec.SecretKeySpec;

      public class e7 {
       
          public static void main(String argv[]) {
              Key k;
              Cipher c;
              AlgorithmParameters params = null;
              
              String alg = "PBEWITHMD5ANDDES";
              byte[] salt = {
                  (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
                  (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
              };
              int count = 20;
              String s = "My wonderfull password that is long enough. Tra-la-la, let me sing a song";
              
              try {
                  int kl = Cipher.getMaxAllowedKeyLength(alg);
                  
                  SecretKeyFactory skf =
                      SecretKeyFactory.getInstance(alg);

                  KeySpec ks = new PBEKeySpec(s.toCharArray(), salt, count, kl);

                  // PBE algorithm is symmetric.
                  k = skf.generateSecret(ks);

                  c = Cipher.getInstance(alg);

                  c.init(Cipher.WRAP_MODE, k);
                  
                  try {
                  
                      c.doFinal();
                      
                      System.out.println("Where is my exception?");
                  } catch (IllegalStateException e) {
                  }

              } catch (Exception e) {
                  e.printStackTrace(System.out);
              }
          }
      }

      Test output is
      Where is my exception?

      java full version "1.5.0-beta-b26"

      ======================================================================
      ###@###.### 10/26/04 10:26 GMT

            valeriep Valerie Peng
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: