1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @see PublicKeyInterop.sh
  26  */
  27 
  28 import java.security.*;
  29 import java.util.*;
  30 import javax.crypto.*;
  31 
  32 import sun.misc.HexDumpEncoder;
  33 
  34 /*
  35  * Confirm interoperability of RSA public keys between SunMSCAPI and SunJCE
  36  * security providers.
  37  */
  38 public class PublicKeyInterop {
  39 
  40     public static void main(String[] arg) throws Exception {
  41         PrivateKey privKey = null;
  42         Certificate cert = null;
  43         KeyStore ks = KeyStore.getInstance("Windows-MY");
  44         ks.load(null, null);
  45         System.out.println("Loaded keystore: Windows-MY");
  46 
  47         PublicKey myPuKey =
  48             (PublicKey) ks.getCertificate("6888925").getPublicKey();
  49         System.out.println("Public key is a " + myPuKey.getClass().getName());
  50         PrivateKey myPrKey = (PrivateKey) ks.getKey("6888925", null);
  51         System.out.println("Private key is a " + myPrKey.getClass().getName());
  52         System.out.println();
  53 
  54         byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
  55         HexDumpEncoder hde = new HexDumpEncoder();
  56         System.out.println("Plaintext:\n" + hde.encode(plain) + "\n");
  57 
  58         Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  59         rsa.init(Cipher.ENCRYPT_MODE, myPuKey);
  60         byte[] encrypted = rsa.doFinal(plain);
  61         System.out.println("Encrypted plaintext using RSA Cipher from " +
  62             rsa.getProvider().getName() + " JCE provider\n");
  63         System.out.println(hde.encode(encrypted) + "\n");
  64 
  65         Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
  66         rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);
  67         byte[] encrypted2 = rsa2.doFinal(plain);
  68         System.out.println("Encrypted plaintext using RSA Cipher from " +
  69             rsa2.getProvider().getName() + " JCE provider\n");
  70         System.out.println(hde.encode(encrypted2) + "\n");
  71 
  72         Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
  73         rsa3.init(Cipher.DECRYPT_MODE, myPrKey);
  74         byte[] decrypted = rsa3.doFinal(encrypted);
  75         System.out.println("Decrypted first ciphertext using RSA Cipher from " +
  76             rsa3.getProvider().getName() + " JCE provider\n");
  77         System.out.println(hde.encode(decrypted) + "\n");
  78         if (! Arrays.equals(plain, decrypted)) {
  79             throw new Exception("First decrypted ciphertext does not match " +
  80                 "original plaintext");
  81         }
  82 
  83         decrypted = rsa3.doFinal(encrypted2);
  84         System.out.println("Decrypted second ciphertext using RSA Cipher from "
  85             + rsa3.getProvider().getName() + " JCE provider\n");
  86         System.out.println(hde.encode(decrypted) + "\n");
  87         if (! Arrays.equals(plain, decrypted)) {
  88             throw new Exception("Second decrypted ciphertext does not match " +
  89                 "original plaintext");
  90         }
  91     }
  92 }