1 /*
   2  * Copyright (c) 2011, 2015, 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         KeyStore ks = KeyStore.getInstance("Windows-MY");
  42         ks.load(null, null);
  43         System.out.println("Loaded keystore: Windows-MY");
  44 
  45         PublicKey myPuKey =
  46             (PublicKey) ks.getCertificate("6888925").getPublicKey();
  47         System.out.println("Public key is a " + myPuKey.getClass().getName());
  48         PrivateKey myPrKey = (PrivateKey) ks.getKey("6888925", null);
  49         System.out.println("Private key is a " + myPrKey.getClass().getName());
  50         System.out.println();
  51 
  52         byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
  53         HexDumpEncoder hde = new HexDumpEncoder();
  54         System.out.println("Plaintext:\n" + hde.encode(plain) + "\n");
  55 
  56         Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  57         rsa.init(Cipher.ENCRYPT_MODE, myPuKey);
  58         byte[] encrypted = rsa.doFinal(plain);
  59         System.out.println("Encrypted plaintext using RSA Cipher from " +
  60             rsa.getProvider().getName() + " JCE provider\n");
  61         System.out.println(hde.encode(encrypted) + "\n");
  62 
  63         Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
  64         rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);
  65         byte[] encrypted2 = rsa2.doFinal(plain);
  66         System.out.println("Encrypted plaintext using RSA Cipher from " +
  67             rsa2.getProvider().getName() + " JCE provider\n");
  68         System.out.println(hde.encode(encrypted2) + "\n");
  69 
  70         Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
  71         rsa3.init(Cipher.DECRYPT_MODE, myPrKey);
  72         byte[] decrypted = rsa3.doFinal(encrypted);
  73         System.out.println("Decrypted first ciphertext using RSA Cipher from " +
  74             rsa3.getProvider().getName() + " JCE provider\n");
  75         System.out.println(hde.encode(decrypted) + "\n");
  76         if (! Arrays.equals(plain, decrypted)) {
  77             throw new Exception("First decrypted ciphertext does not match " +
  78                 "original plaintext");
  79         }
  80 
  81         decrypted = rsa3.doFinal(encrypted2);
  82         System.out.println("Decrypted second ciphertext using RSA Cipher from "
  83             + rsa3.getProvider().getName() + " JCE provider\n");
  84         System.out.println(hde.encode(decrypted) + "\n");
  85         if (! Arrays.equals(plain, decrypted)) {
  86             throw new Exception("Second decrypted ciphertext does not match " +
  87                 "original plaintext");
  88         }
  89     }
  90 }