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

Wrong exception thrown for invalid key type in Cipher.unwrap

XMLWordPrintable

    • hopper
    • generic
    • generic
    • Verified

        The following test demonstrates that the wrong exception is
        being thrown for an invalid wrappedKeyType during Cipher.unwrap():

        Test a) and test b) differ only in the wrappedKeyType. a) is erroneously
        set to PUBLIC_KEY and b) is correctly set to SECRET_KEY. Test a throws a
        NoSuchAlgorithmException instead of an InvalidKeyException
        per the spec.

        The output of the test is:
        Test #1java.security.NoSuchAlgorithmException:
        No installed providers can create keys for the Blowfishalgorithm

        Test b completes without error.

        ------------------------------------------------------------------------




        import java.security.*;
        import javax.crypto.*;


        public class testWrap2 {


           
           public static void main (String[] args) {
          
        try {

             Security.addProvider(new com.sun.crypto.provider.SunJCE());
             Cipher cp = Cipher.getInstance("Blowfish","SunJCE");
            //
            KeyGenerator kg = KeyGenerator.getInstance("Blowfish");
            Key mykey = kg.generateKey();
            AlgorithmParameters params = cp.getParameters();
            

         
            cp.init(Cipher.WRAP_MODE,mykey,params);
            byte[] b_b = cp.wrap(mykey);
            cp.init(Cipher.UNWRAP_MODE,mykey,params);
            Key mykey2 = cp.unwrap(b_b,"Blowfish",Cipher.PUBLIC_KEY);
           
            } catch (Exception e) {
            System.out.println("Test a)" + e);
            }



        try {

             Security.addProvider(new com.sun.crypto.provider.SunJCE());
             Cipher cp = Cipher.getInstance("Blowfish","SunJCE");
            //
            KeyGenerator kg = KeyGenerator.getInstance("Blowfish");
            Key mykey = kg.generateKey();
            AlgorithmParameters params = cp.getParameters();
            

         
            cp.init(Cipher.WRAP_MODE,mykey,params);
            byte[] b_b = cp.wrap(mykey);
            cp.init(Cipher.UNWRAP_MODE,mykey,params);
            Key mykey2 = cp.unwrap(b_b,"Blowfish",Cipher.SECRET_KEY);
           
            } catch (Exception e) {
         
                 System.out.println("Test b)" + e);
           
            }

          System.out.println("Exit without problem");
          System.exit(0);
         
           }
            
            
           
           }
            
            
            
            

              valeriep Valerie Peng
              kpolomsksunw Krystyna Polomski (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: