Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/PublicKeyInterop.java
38840 views
/*1* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @see PublicKeyInterop.sh25*/2627import java.security.*;28import java.util.*;29import javax.crypto.*;3031import sun.misc.HexDumpEncoder;3233/*34* Confirm interoperability of RSA public keys between SunMSCAPI and SunJCE35* security providers.36*/37public class PublicKeyInterop {3839public static void main(String[] arg) throws Exception {40KeyStore ks = KeyStore.getInstance("Windows-MY");41ks.load(null, null);42System.out.println("Loaded keystore: Windows-MY");4344PublicKey myPuKey =45(PublicKey) ks.getCertificate("6888925").getPublicKey();46System.out.println("Public key is a " + myPuKey.getClass().getName());47PrivateKey myPrKey = (PrivateKey) ks.getKey("6888925", null);48System.out.println("Private key is a " + myPrKey.getClass().getName());49System.out.println();5051byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};52HexDumpEncoder hde = new HexDumpEncoder();53System.out.println("Plaintext:\n" + hde.encode(plain) + "\n");5455Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");56rsa.init(Cipher.ENCRYPT_MODE, myPuKey);57byte[] encrypted = rsa.doFinal(plain);58System.out.println("Encrypted plaintext using RSA Cipher from " +59rsa.getProvider().getName() + " JCE provider\n");60System.out.println(hde.encode(encrypted) + "\n");6162Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");63rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);64byte[] encrypted2 = rsa2.doFinal(plain);65System.out.println("Encrypted plaintext using RSA Cipher from " +66rsa2.getProvider().getName() + " JCE provider\n");67System.out.println(hde.encode(encrypted2) + "\n");6869Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");70rsa3.init(Cipher.DECRYPT_MODE, myPrKey);71byte[] decrypted = rsa3.doFinal(encrypted);72System.out.println("Decrypted first ciphertext using RSA Cipher from " +73rsa3.getProvider().getName() + " JCE provider\n");74System.out.println(hde.encode(decrypted) + "\n");75if (! Arrays.equals(plain, decrypted)) {76throw new Exception("First decrypted ciphertext does not match " +77"original plaintext");78}7980decrypted = rsa3.doFinal(encrypted2);81System.out.println("Decrypted second ciphertext using RSA Cipher from "82+ rsa3.getProvider().getName() + " JCE provider\n");83System.out.println(hde.encode(decrypted) + "\n");84if (! Arrays.equals(plain, decrypted)) {85throw new Exception("Second decrypted ciphertext does not match " +86"original plaintext");87}88}89}909192