Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/DHKeyFactory.java
41155 views
/*1* Copyright (c) 1998, 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* @test25* @bug 000000026* @summary DHKeyFactory27* @author Jan Luehe28*/2930import java.io.*;31import java.math.BigInteger;32import java.security.*;33import java.security.spec.*;34import java.security.interfaces.*;35import javax.crypto.*;36import javax.crypto.spec.*;37import javax.crypto.interfaces.*;3839/**40* This test creates a DH keypair, retrieves the encodings of the DH public and41* private key, and feeds those encodings to a DH key factory, which parses42* the encodings and creates a DH public and private key from them.43*/4445public class DHKeyFactory {4647private DHKeyFactory() {}4849public static void main(String argv[]) throws Exception {50DHKeyFactory test = new DHKeyFactory();51test.run();52System.out.println("Test Passed");53}5455private void run() throws Exception {5657DHParameterSpec dhSkipParamSpec;5859// use some pre-generated, default DH parameters60System.err.println("Using SKIP Diffie-Hellman parameters");61dhSkipParamSpec = new DHParameterSpec(skip1024Modulus,62skip1024Base);6364KeyPairGenerator kpgen = KeyPairGenerator.getInstance("DH", "SunJCE");65kpgen.initialize(dhSkipParamSpec);66KeyPair kp = kpgen.generateKeyPair();6768// get the public key encoding69byte[] pubKeyEnc = kp.getPublic().getEncoded();7071// get the private key encoding72byte[] privKeyEnc = kp.getPrivate().getEncoded();7374KeyFactory kfac = KeyFactory.getInstance("DH", "SunJCE");7576X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyEnc);77PublicKey pubKey = kfac.generatePublic(x509KeySpec);7879PKCS8EncodedKeySpec pkcsKeySpec = new PKCS8EncodedKeySpec(privKeyEnc);80PrivateKey privKey = kfac.generatePrivate(pkcsKeySpec);81}8283// The 1024 bit Diffie-Hellman modulus values used by SKIP84private static final byte skip1024ModulusBytes[] = {85(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,86(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,87(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,88(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,89(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,90(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,91(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,92(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,93(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,94(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,95(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,96(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,97(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,98(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,99(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,100(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,101(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,102(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,103(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,104(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,105(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,106(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,107(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,108(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,109(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,110(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,111(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,112(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,113(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,114(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,115(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,116(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7117};118119// The SKIP 1024 bit modulus120private static final BigInteger skip1024Modulus121= new BigInteger(1, skip1024ModulusBytes);122123// The base used with the SKIP 1024 bit modulus124private static final BigInteger skip1024Base = BigInteger.valueOf(2);125}126127128