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

InvalidKeyException from javax.crypto.Cipher.init(int opmode, Certificate cert)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 5.0
    • security-libs



      Name: iiR10263 Date: 01/04/2004



      Current implementaion fails to reinitialize javax.crypto.Cipher object
      instance after an attempt of initialization with null Certificate

      Please find code example and out put below

      import java.io.*;
      import java.util.*;
      import java.security.cert.*;
      import javax.crypto.Cipher;
      import java.security.InvalidKeyException;

      class e11 {
          public static void main(String argv[]) {
              String algorithm = "RSA";

              try {
                  String certStr = "-----BEGIN CERTIFICATE-----\n" +
      "MIICYDCCAckCBDm+nq8wDQYJKoZIhvcNAQEEBQAwdzELMAkGA1UEBhMCVVMxCzAJ\n" +
      "BgNVBAgTAkNBMRIwEAYDVQQHEwlTdW5ueXZhbGUxHzAdBgNVBAoTFlN1biBNaWNy\n" +
      "b3N5c3RlbXMsIEluYy4xETAPBgNVBAsTCEphdmFzb2Z0MRMwEQYDVQQDEwpSb2dl\n" +
      "ciBQaGFtMB4XDTAwMDkxMjIxMjI1NVoXDTIwMDkwNzIxMjI1NVowdzELMAkGA1UE\n" +
      "BhMCVVMxCzAJBgNVBAgTAkNBMRIwEAYDVQQHEwlTdW5ueXZhbGUxHzAdBgNVBAoT\n" +
      "FlN1biBNaWNyb3N5c3RlbXMsIEluYy4xETAPBgNVBAsTCEphdmFzb2Z0MRMwEQYD\n" +
      "VQQDEwpSb2dlciBQaGFtMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuUdLg\n" +
      "t0BUE/MZ/wkcjDDK5VRAhuOphPizHV90S1goG7u0Ayf6w9V9WdJXswmbyf0SbRRj\n" +
      "2IaH3ClRM/S+RuOZPzJyYY2GnIxUaIlOkWdBIcZv1l/ceXyal+C2oAF/ypRbstfE\n" +
      "Lq5Y/AyQNEesi42Php+wTLT7GOBj1AMMBNLdYwIDAQABMA0GCSqGSIb3DQEBBAUA\n" +
      "A4GBAEL9yV2GdVEeK7VdN3LKFxZ1egsZqtpzoUb37zqOXii27kdmIFGPPBal2/Ij\n" +
      "us/Dphu+BMwxFerEUV7r/KfjDPk0Wofwdj7Ls2fcK4LzRvEI+OswvBaAAqJ3D+ja\n" +
      "VcYBnS35IJDv0ocMUsPhr4kKUn0MQik3eixmh/Vz2Cu1bq1f\n" +
      "-----END CERTIFICATE-----";
                  byte[] bb = certStr.getBytes();
                  
                  if (bb != null) {
                      ByteArrayInputStream bas = new ByteArrayInputStream(bb);
                      BufferedInputStream bis = new BufferedInputStream(bas);
                      CertificateFactory cf = CertificateFactory.getInstance("X.509");
                      
                      if (bis.available() > 0) {
                          Cipher c;
                          X509Certificate cert =
                              (X509Certificate)cf.generateCertificate(bis);
                              
                          System.out.println(cert.toString());
                         
                          c = Cipher.getInstance(algorithm);
                          
                          System.out.println("Obtained Cipher for algorithm " + c.getAlgorithm());

                          try {
                              c.init(Cipher.ENCRYPT_MODE, (Certificate) null);
                          } catch (InvalidKeyException e) {
                              System.out.println("IKE for null certificate");
                          } catch (NullPointerException e) {
                              System.out.println("NPE for null certificate");
                          }
                          
                          c.init(Cipher.ENCRYPT_MODE, cert);
                          System.out.println("Cipher is initialized");
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace(System.out);
              }
          }
      }


      <ibr@void(pts/13).268> ~/links/java/jdk1.5.0/solaris/bin/java e11
      [
      [
        Version: V1
        Subject: CN=Roger Pham, OU=Javasoft, O="Sun Microsystems, Inc.", L=Sunnyvale, ST=CA, C=US
        Signature Algorithm: MD5withRSA, OID = 1.2.840.113549.1.1.4

        Key: Sun RSA public key, 1024 bits
        modulus: 122411402858696027936294583683561371998730842810020052647245850742614006653199301284673185989714751795439771531332191471814703433490765874453202934900168751252134585967410940304591432510984651278537717849083922048964292170356479520381696599615917773314172374863139694341791273866042577279292898110642737831267
        public exponent: 65537
        Validity: [From: Wed Sep 13 01:22:55 MSD 2000,
                     To: Tue Sep 08 01:22:55 MSD 2020]
        Issuer: CN=Roger Pham, OU=Javasoft, O="Sun Microsystems, Inc.", L=Sunnyvale, ST=CA, C=US
        SerialNumber: [ 39be9eaf]

      ]
        Algorithm: [MD5withRSA]
        Signature:
      0000: 42 FD C9 5D 86 75 51 1E 2B B5 5D 37 72 CA 17 16 B..].uQ.+.]7r...
      0010: 75 7A 0B 19 AA DA 73 A1 46 F7 EF 3A 8E 5E 28 B6 uz....s.F..:.^(.
      0020: EE 47 66 20 51 8F 3C 16 A5 DB F2 23 BA CF C3 A6 .Gf Q.<....#....
      0030: 1B BE 04 CC 31 15 EA C4 51 5E EB FC A7 E3 0C F9 ....1...Q^......
      0040: 34 5A 87 F0 76 3E CB B3 67 DC 2B 82 F3 46 F1 08 4Z..v>..g.+..F..
      0050: F8 EB 30 BC 16 80 02 A2 77 0F E8 DA 55 C6 01 9D ..0.....w...U...
      0060: 2D F9 20 90 EF D2 87 0C 52 C3 E1 AF 89 0A 52 7D -. .....R.....R.
      0070: 0C 42 29 37 7A 2C 66 87 F5 73 D8 2B B5 6E AD 5F .B)7z,f..s.+.n._

      ]
      Obtained Cipher for algorithm RSA
      IKE for null certificate
      java.security.InvalidKeyException: No installed provider supports this key: sun.security.rsa.RSAPublicKeyImpl
              at javax.crypto.Cipher.a(DashoA6275)
              at javax.crypto.Cipher.init(DashoA6275)
              at javax.crypto.Cipher.init(DashoA6275)
              at e11.main(e11.java:53)


      java full version "1.5.0-beta-b32"

      ======================================================================

            andreas Andreas Sterbenz
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: