11/*
2- * Copyright (c) 1997, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1997, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
5353
5454public class DHKeyAgreement2 {
5555
56- private static final String SUNJCE = "SunJCE" ;
56+ private static final String PROVIDER_NAME = System .getProperty (
57+ "test.provider.name" , "SunJCE" );
5758
5859 // Hex formatter to upper case with ":" delimiter
5960 private static final HexFormat HEX_FORMATTER = HexFormat .ofDelimiter (":" ).withUpperCase ();
@@ -90,7 +91,7 @@ private void run(String mode) throws Exception {
9091 // Some central authority creates new DH parameters
9192 System .err .println ("Creating Diffie-Hellman parameters ..." );
9293 AlgorithmParameterGenerator paramGen
93- = AlgorithmParameterGenerator .getInstance ("DH" , SUNJCE );
94+ = AlgorithmParameterGenerator .getInstance ("DH" , PROVIDER_NAME );
9495 paramGen .init (primeSize );
9596 AlgorithmParameters params = paramGen .generateParameters ();
9697 dhParameterSpec = (DHParameterSpec )params .getParameterSpec
@@ -108,7 +109,7 @@ private void run(String mode) throws Exception {
108109 * above
109110 */
110111 System .err .println ("ALICE: Generate DH keypair ..." );
111- KeyPairGenerator aliceKpairGen = KeyPairGenerator .getInstance ("DH" , SUNJCE );
112+ KeyPairGenerator aliceKpairGen = KeyPairGenerator .getInstance ("DH" , PROVIDER_NAME );
112113 aliceKpairGen .initialize (dhParameterSpec );
113114 KeyPair aliceKpair = aliceKpairGen .generateKeyPair ();
114115 System .out .println ("Alice DH public key:\n " +
@@ -117,14 +118,14 @@ private void run(String mode) throws Exception {
117118 aliceKpair .getPrivate ().toString ());
118119 DHParameterSpec dhParamSpec =
119120 ((DHPublicKey )aliceKpair .getPublic ()).getParams ();
120- AlgorithmParameters algParams = AlgorithmParameters .getInstance ("DH" , SUNJCE );
121+ AlgorithmParameters algParams = AlgorithmParameters .getInstance ("DH" , PROVIDER_NAME );
121122 algParams .init (dhParamSpec );
122123 System .out .println ("Alice DH parameters:\n "
123124 + algParams .toString ());
124125
125126 // Alice executes Phase1 of her version of the DH protocol
126127 System .err .println ("ALICE: Execute PHASE1 ..." );
127- KeyAgreement aliceKeyAgree = KeyAgreement .getInstance ("DH" , SUNJCE );
128+ KeyAgreement aliceKeyAgree = KeyAgreement .getInstance ("DH" , PROVIDER_NAME );
128129 aliceKeyAgree .init (aliceKpair .getPrivate ());
129130
130131 // Alice encodes her public key, and sends it over to Bob.
@@ -135,7 +136,7 @@ private void run(String mode) throws Exception {
135136 * in encoded format.
136137 * He instantiates a DH public key from the encoded key material.
137138 */
138- KeyFactory bobKeyFac = KeyFactory .getInstance ("DH" , SUNJCE );
139+ KeyFactory bobKeyFac = KeyFactory .getInstance ("DH" , PROVIDER_NAME );
139140 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec
140141 (alicePubKeyEnc );
141142 PublicKey alicePubKey = bobKeyFac .generatePublic (x509KeySpec );
@@ -149,7 +150,7 @@ private void run(String mode) throws Exception {
149150
150151 // Bob creates his own DH key pair
151152 System .err .println ("BOB: Generate DH keypair ..." );
152- KeyPairGenerator bobKpairGen = KeyPairGenerator .getInstance ("DH" , SUNJCE );
153+ KeyPairGenerator bobKpairGen = KeyPairGenerator .getInstance ("DH" , PROVIDER_NAME );
153154 bobKpairGen .initialize (dhParamSpec );
154155 KeyPair bobKpair = bobKpairGen .generateKeyPair ();
155156 System .out .println ("Bob DH public key:\n " +
@@ -159,7 +160,7 @@ private void run(String mode) throws Exception {
159160
160161 // Bob executes Phase1 of his version of the DH protocol
161162 System .err .println ("BOB: Execute PHASE1 ..." );
162- KeyAgreement bobKeyAgree = KeyAgreement .getInstance ("DH" , SUNJCE );
163+ KeyAgreement bobKeyAgree = KeyAgreement .getInstance ("DH" , PROVIDER_NAME );
163164 bobKeyAgree .init (bobKpair .getPrivate ());
164165
165166 // Bob encodes his public key, and sends it over to Alice.
@@ -171,7 +172,7 @@ private void run(String mode) throws Exception {
171172 * Before she can do so, she has to instanticate a DH public key
172173 * from Bob's encoded key material.
173174 */
174- KeyFactory aliceKeyFac = KeyFactory .getInstance ("DH" , SUNJCE );
175+ KeyFactory aliceKeyFac = KeyFactory .getInstance ("DH" , PROVIDER_NAME );
175176 x509KeySpec = new X509EncodedKeySpec (bobPubKeyEnc );
176177 PublicKey bobPubKey = aliceKeyFac .generatePublic (x509KeySpec );
177178 System .err .println ("ALICE: Execute PHASE2 ..." );
0 commit comments