|
4 | 4 |
|
5 | 5 | public final class DiffieHellman {
|
6 | 6 |
|
7 |
| - // Private constructor to prevent instantiation of utility class |
8 |
| - private DiffieHellman() { |
9 |
| - throw new UnsupportedOperationException("Utility class"); |
| 7 | + private final BigInteger base; |
| 8 | + private final BigInteger secret; |
| 9 | + private final BigInteger prime; |
| 10 | + |
| 11 | + // Constructor to initialize base, secret, and prime |
| 12 | + public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) { |
| 13 | + this.base = base; |
| 14 | + this.secret = secret; |
| 15 | + this.prime = prime; |
10 | 16 | }
|
11 | 17 |
|
12 | 18 | // Method to calculate public value (g^x mod p)
|
13 |
| - public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { |
| 19 | + public BigInteger calculatePublicValue() { |
14 | 20 | // Returns g^x mod p
|
15 | 21 | return base.modPow(secret, prime);
|
16 | 22 | }
|
17 | 23 |
|
18 | 24 | // Method to calculate the shared secret key (otherPublic^secret mod p)
|
19 |
| - public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { |
| 25 | + public BigInteger calculateSharedSecret(BigInteger otherPublicValue) { |
20 | 26 | // Returns b^x mod p or a^y mod p
|
21 | 27 | return otherPublicValue.modPow(secret, prime);
|
22 | 28 | }
|
| 29 | + |
| 30 | + // Static utility methods for direct calculation (if needed) |
| 31 | + public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { |
| 32 | + return base.modPow(secret, prime); |
| 33 | + } |
| 34 | + |
| 35 | + public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { |
| 36 | + return otherPublicValue.modPow(secret, prime); |
| 37 | + } |
23 | 38 | }
|
0 commit comments