Path: blob/master/test/jdk/java/security/Signature/NONEwithRSA.java
41152 views
/*1* Copyright (c) 2003, 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 495584426* @summary ensure that the NONEwithRSA adapter works correctly27* @author Andreas Sterbenz28* @key randomness29*/3031import java.util.*;3233import java.security.*;3435import javax.crypto.*;3637public class NONEwithRSA {3839public static void main(String[] args) throws Exception {40// showProvider(Security.getProvider("SUN"));41Random random = new Random();42byte[] b = new byte[16];43random.nextBytes(b);4445KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");46kpg.initialize(512);47KeyPair kp = kpg.generateKeyPair();4849Signature sig = Signature.getInstance("NONEwithRSA");50sig.initSign(kp.getPrivate());51System.out.println("Provider: " + sig.getProvider());52sig.update(b);53byte[] sb = sig.sign();5455sig.initVerify(kp.getPublic());56sig.update(b);57if (sig.verify(sb) == false) {58throw new Exception("verification failed");59}6061Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");62c.init(Cipher.DECRYPT_MODE, kp.getPublic());63byte[] dec = c.doFinal(sb);64if (Arrays.equals(dec, b) == false) {65throw new Exception("decryption failed");66}6768sig = Signature.getInstance("NONEwithRSA", "SunJCE");69sig.initSign(kp.getPrivate());70sig = Signature.getInstance("NONEwithRSA", Security.getProvider("SunJCE"));71sig.initSign(kp.getPrivate());7273try {74Signature.getInstance("NONEwithRSA", "SUN");75throw new Exception("call succeeded");76} catch (NoSuchAlgorithmException e) {77e.printStackTrace();78}7980System.out.println("OK");81}8283private static void showProvider(Provider p) {84System.out.println(p);85for (Iterator t = p.getServices().iterator(); t.hasNext(); ) {86System.out.println(t.next());87}88}8990}919293