Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestRawRSACipher.java
41152 views
/*1* Copyright (c) 2011, 2018, 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* @test25* @bug 699400826* @summary basic test for RSA/ECB/NoPadding cipher27* @author Valerie Peng28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestRawRSACipher32* @run main/othervm -Djava.security.manager=allow TestRawRSACipher sm33*/3435import java.security.GeneralSecurityException;36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.Provider;39import java.util.Arrays;40import java.util.Random;41import javax.crypto.Cipher;4243public class TestRawRSACipher extends PKCS11Test {4445@Override46public void main(Provider p) throws Exception {47try {48Cipher.getInstance("RSA/ECB/NoPadding", p);49} catch (GeneralSecurityException e) {50System.out.println("Not supported by provider, skipping");51return;52}5354final int KEY_LEN = 1024;55KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", p);56kpGen.initialize(KEY_LEN);57KeyPair kp = kpGen.generateKeyPair();58Random random = new Random();59byte[] plainText, cipherText, recoveredText;60plainText = new byte[KEY_LEN/8];61random.nextBytes(plainText);62plainText[0] = 0; // to ensure that it's less than modulus6364Cipher c1 = Cipher.getInstance("RSA/ECB/NoPadding", p);65Cipher c2 = Cipher.getInstance("RSA/ECB/NoPadding", "SunJCE");6667c1.init(Cipher.ENCRYPT_MODE, kp.getPublic());68c2.init(Cipher.DECRYPT_MODE, kp.getPrivate());6970cipherText = c1.doFinal(plainText);71recoveredText = c2.doFinal(cipherText);72if (!Arrays.equals(plainText, recoveredText)) {73throw new RuntimeException("E/D Test against SunJCE Failed!");74}7576c2.init(Cipher.ENCRYPT_MODE, kp.getPublic());77c1.init(Cipher.DECRYPT_MODE, kp.getPrivate());78cipherText = c2.doFinal(plainText);79recoveredText = c1.doFinal(cipherText);80if (!Arrays.equals(plainText, recoveredText)) {81throw new RuntimeException("D/E Test against SunJCE Failed!");82}8384System.out.println("Test Passed");85}8687public static void main(String[] args) throws Exception {88main(new TestRawRSACipher(), args);89}90}919293