Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/DHGenSharedSecret.java
41155 views
/*1* Copyright (c) 1997, 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 DHGenSharedSecret27* @author Jan Luehe28*/29import java.security.*;30import java.security.spec.*;31import java.security.interfaces.*;32import javax.crypto.*;33import javax.crypto.spec.*;34import java.math.BigInteger;3536public class DHGenSharedSecret {3738static byte[] DHPrime = {39(byte)0x00, (byte)0x8D, (byte)0x8A, (byte)0x6C, (byte)0x7F, (byte)0xCC,40(byte)0xA5, (byte)0xBF, (byte)0x9C, (byte)0xE1, (byte)0xFA, (byte)0x3C,41(byte)0xCA, (byte)0x98, (byte)0xB7, (byte)0x99, (byte)0xD1, (byte)0xE5,42(byte)0x2C, (byte)0xC0, (byte)0x26, (byte)0x97, (byte)0x12, (byte)0x80,43(byte)0x12, (byte)0xEF, (byte)0x0B, (byte)0xDE, (byte)0x71, (byte)0x76,44(byte)0xAA, (byte)0x2D, (byte)0x86, (byte)0x41, (byte)0x0E, (byte)0x6A,45(byte)0xC2, (byte)0x12, (byte)0xAA, (byte)0xAA, (byte)0xE4, (byte)0x84,46(byte)0x80, (byte)0x13, (byte)0x95, (byte)0x06, (byte)0xC4, (byte)0x83,47(byte)0xB9, (byte)0xD3, (byte)0x72, (byte)0xC5, (byte)0xC8, (byte)0x85,48(byte)0x96, (byte)0x59, (byte)0x08, (byte)0xFA, (byte)0x9E, (byte)0x3C,49(byte)0xDC, (byte)0x92, (byte)0x28, (byte)0xC3, (byte)0x1D, (byte)0x6F,50(byte)0x44, (byte)0x36, (byte)0x70, (byte)0x40, (byte)0x80, (byte)0xF1,51(byte)0x3552};5354static byte[] DHBase = {55(byte)0x72, (byte)0x21, (byte)0xB3, (byte)0xA8, (byte)0x83, (byte)0xDD,56(byte)0x76, (byte)0xF5, (byte)0x0D, (byte)0x9B, (byte)0x81, (byte)0x11,57(byte)0x15, (byte)0x03, (byte)0x6D, (byte)0x4D, (byte)0x46, (byte)0x65,58(byte)0x30, (byte)0xB0, (byte)0xFA, (byte)0xFE, (byte)0xBE, (byte)0xA8,59(byte)0xD9, (byte)0x83, (byte)0x33, (byte)0x54, (byte)0xC7, (byte)0xF6,60(byte)0x81, (byte)0xAC, (byte)0xCC, (byte)0xA3, (byte)0xAE, (byte)0xAA,61(byte)0xC8, (byte)0x11, (byte)0x38, (byte)0xD4, (byte)0x4F, (byte)0xC4,62(byte)0x89, (byte)0xD3, (byte)0x72, (byte)0xEE, (byte)0x22, (byte)0x5A,63(byte)0x68, (byte)0xF7, (byte)0xAC, (byte)0x24, (byte)0x01, (byte)0x9B,64(byte)0xE9, (byte)0x08, (byte)0xFE, (byte)0x58, (byte)0x0A, (byte)0xCF,65(byte)0xB9, (byte)0x52, (byte)0xB4, (byte)0x02, (byte)0x73, (byte)0xA4,66(byte)0xA6, (byte)0xB9, (byte)0x0C, (byte)0x8D, (byte)0xA7, (byte)0xFB,67};6869public static void main(String[] args) throws Exception {70DHGenSharedSecret test = new DHGenSharedSecret();71test.run();72}7374public void run() throws Exception {75long start, end;7677BigInteger p = new BigInteger(1, DHPrime);78BigInteger g = new BigInteger(1, DHBase);79int l = 512;8081DHParameterSpec spec =82new DHParameterSpec(p, g, l);8384// generate keyPairs using parameters85KeyPairGenerator keyGen =86KeyPairGenerator.getInstance("DH", "SunJCE");87keyGen.initialize(spec);8889// Alice generates her key pairs90KeyPair keyA = keyGen.generateKeyPair();9192// Bob generates his key pairs93KeyPair keyB = keyGen.generateKeyPair();9495// Alice encodes her public key in x509 format96// , and sends it over to Bob.97byte[] alicePubKeyEnc = keyA.getPublic().getEncoded();9899// bob encodes his publicKey in x509 format and100// sends it over to Alice101byte[] bobPubKeyEnc = keyB.getPublic().getEncoded();102103// bob uses it to generate Secret104X509EncodedKeySpec x509Spec =105new X509EncodedKeySpec(alicePubKeyEnc);106KeyFactory bobKeyFac = KeyFactory.getInstance("DH", "SunJCE");107PublicKey alicePubKey = bobKeyFac.generatePublic(x509Spec);108109110KeyAgreement bobAlice = KeyAgreement.getInstance("DH", "SunJCE");111start = System.currentTimeMillis();112bobAlice.init(keyB.getPrivate());113bobAlice.doPhase(alicePubKey, true);114byte[] bobSecret = bobAlice.generateSecret();115end = System.currentTimeMillis();116117System.out.println("Time elapsed: " + (end - start));118System.out.println("Test Passed!");119}120}121122123