4108N/A/*
4108N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4108N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4108N/A *
4108N/A * This code is free software; you can redistribute it and/or modify it
4108N/A * under the terms of the GNU General Public License version 2 only, as
4108N/A * published by the Free Software Foundation.
4108N/A *
4108N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4108N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4108N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4108N/A * version 2 for more details (a copy is included in the LICENSE file that
4108N/A * accompanied this code).
4108N/A *
4108N/A * You should have received a copy of the GNU General Public License version
4108N/A * 2 along with this work; if not, write to the Free Software Foundation,
4108N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4108N/A *
4108N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4108N/A * or visit www.oracle.com if you need additional information or have any
4108N/A * questions.
4108N/A */
4108N/A
4108N/A/**
4108N/A * @see PublicKeyInterop.sh
4108N/A */
4108N/A
4108N/Aimport java.security.*;
4108N/Aimport java.util.*;
4108N/Aimport javax.crypto.*;
4108N/A
4108N/Aimport sun.misc.HexDumpEncoder;
4108N/A
4108N/A/*
4108N/A * Confirm interoperability of RSA public keys between SunMSCAPI and SunJCE
4108N/A * security providers.
4108N/A */
4108N/Apublic class PublicKeyInterop {
4108N/A
4108N/A public static void main(String[] arg) throws Exception {
4108N/A PrivateKey privKey = null;
4108N/A Certificate cert = null;
4108N/A KeyStore ks = KeyStore.getInstance("Windows-MY");
4108N/A ks.load(null, null);
4108N/A System.out.println("Loaded keystore: Windows-MY");
4108N/A
4108N/A PublicKey myPuKey =
4108N/A (PublicKey) ks.getCertificate("6888925").getPublicKey();
4108N/A System.out.println("Public key is a " + myPuKey.getClass().getName());
4108N/A PrivateKey myPrKey = (PrivateKey) ks.getKey("6888925", null);
4108N/A System.out.println("Private key is a " + myPrKey.getClass().getName());
4108N/A System.out.println();
4108N/A
4108N/A byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
4108N/A HexDumpEncoder hde = new HexDumpEncoder();
4108N/A System.out.println("Plaintext:\n" + hde.encode(plain) + "\n");
4108N/A
4108N/A Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
4108N/A rsa.init(Cipher.ENCRYPT_MODE, myPuKey);
4108N/A byte[] encrypted = rsa.doFinal(plain);
4108N/A System.out.println("Encrypted plaintext using RSA Cipher from " +
4108N/A rsa.getProvider().getName() + " JCE provider\n");
4108N/A System.out.println(hde.encode(encrypted) + "\n");
4108N/A
4108N/A Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
4108N/A rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);
4108N/A byte[] encrypted2 = rsa2.doFinal(plain);
4108N/A System.out.println("Encrypted plaintext using RSA Cipher from " +
4108N/A rsa2.getProvider().getName() + " JCE provider\n");
4108N/A System.out.println(hde.encode(encrypted2) + "\n");
4108N/A
4108N/A Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
4108N/A rsa3.init(Cipher.DECRYPT_MODE, myPrKey);
4108N/A byte[] decrypted = rsa3.doFinal(encrypted);
4108N/A System.out.println("Decrypted first ciphertext using RSA Cipher from " +
4108N/A rsa3.getProvider().getName() + " JCE provider\n");
4108N/A System.out.println(hde.encode(decrypted) + "\n");
4108N/A if (! Arrays.equals(plain, decrypted)) {
4108N/A throw new Exception("First decrypted ciphertext does not match " +
4108N/A "original plaintext");
4108N/A }
4108N/A
4108N/A decrypted = rsa3.doFinal(encrypted2);
4108N/A System.out.println("Decrypted second ciphertext using RSA Cipher from "
4108N/A + rsa3.getProvider().getName() + " JCE provider\n");
4108N/A System.out.println(hde.encode(decrypted) + "\n");
4108N/A if (! Arrays.equals(plain, decrypted)) {
4108N/A throw new Exception("Second decrypted ciphertext does not match " +
4108N/A "original plaintext");
4108N/A }
4108N/A }
4108N/A}