|
1 | 1 | import java.math.BigInteger;
|
2 |
| -import java.util.Scanner; |
3 | 2 |
|
4 | 3 | public final class DiffieHellman {
|
5 | 4 | private DiffieHellman() {
|
6 | 5 | throw new UnsupportedOperationException("Utility class");
|
7 | 6 | }
|
8 |
| - public static void main(String[] args) { |
9 |
| - Scanner read = new Scanner(System.in); |
10 |
| - System.out.println("Hello User! \nEnter your name:"); |
11 |
| - String name = read.nextLine(); |
12 |
| - read.nextLine(); |
13 |
| - System.out.println("Welcome " + name + "!"); |
14 |
| - |
15 |
| - BigInteger n; |
16 |
| - BigInteger g; |
17 |
| - BigInteger x; |
18 |
| - BigInteger y; |
19 |
| - BigInteger k1; |
20 |
| - BigInteger k2; |
21 |
| - BigInteger a; |
22 |
| - BigInteger b; |
23 |
| - |
24 |
| - System.out.println("Enter two prime numbers: "); |
25 |
| - n = new BigInteger(read.next()); |
26 |
| - g = new BigInteger(read.next()); |
27 |
| - |
28 |
| - System.out.println("Person A : Enter your secret number"); |
29 |
| - x = new BigInteger(read.next()); |
30 |
| - a = g.modPow(x, n); |
31 |
| - |
32 |
| - System.out.println("Person B : Enter your secret number"); |
33 |
| - y = new BigInteger(read.next()); |
34 |
| - b = g.modPow(y, n); |
35 |
| - |
36 |
| - k1 = b.modPow(x, n); |
37 |
| - k2 = a.modPow(y, n); |
38 |
| - |
39 |
| - System.out.println("A's secret key: " + k1); |
40 |
| - System.out.println("B's secret key: " + k2); |
| 7 | + |
| 8 | + public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { |
| 9 | + return base.modPow(secret, prime); |
| 10 | + } |
| 11 | + |
| 12 | + public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { |
| 13 | + return otherPublicValue.modPow(secret, prime); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// DiffieHellmanTest.java - JUnit Tests |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.CsvSource; |
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | + |
| 23 | +import java.math.BigInteger; |
| 24 | + |
| 25 | +public class DiffieHellmanTest { |
| 26 | + |
| 27 | + @ParameterizedTest |
| 28 | + @DisplayName("Diffie-Hellman Key Exchange Test") |
| 29 | + @CsvSource({ |
| 30 | + "23, 5, 6, 15", |
| 31 | + "97, 7, 12, 23", |
| 32 | + "61, 2, 9, 19" |
| 33 | + }) |
| 34 | + void testDiffieHellman(String nStr, String gStr, String xStr, String yStr) { |
| 35 | + BigInteger n = new BigInteger(nStr); |
| 36 | + BigInteger g = new BigInteger(gStr); |
| 37 | + BigInteger x = new BigInteger(xStr); |
| 38 | + BigInteger y = new BigInteger(yStr); |
| 39 | + |
| 40 | + BigInteger a = DiffieHellman.calculatePublicValue(g, x, n); |
| 41 | + BigInteger b = DiffieHellman.calculatePublicValue(g, y, n); |
| 42 | + |
| 43 | + BigInteger k1 = DiffieHellman.calculateSharedSecret(b, x, n); |
| 44 | + BigInteger k2 = DiffieHellman.calculateSharedSecret(a, y, n); |
| 45 | + |
| 46 | + assertEquals(k1, k2, "Shared secret keys do not match for inputs n=" + nStr + ", g=" + gStr); |
41 | 47 | }
|
42 | 48 | }
|
0 commit comments