Skip to content

Commit 5e8b536

Browse files
authored
Update DiffieHellman.java
1 parent 283b5f9 commit 5e8b536

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/main/java/com/thealgorithms/ciphers/DiffieHellman.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@
44

55
public final class DiffieHellman {
66

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;
1016
}
1117

1218
// Method to calculate public value (g^x mod p)
13-
public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) {
19+
public BigInteger calculatePublicValue() {
1420
// Returns g^x mod p
1521
return base.modPow(secret, prime);
1622
}
1723

1824
// 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) {
2026
// Returns b^x mod p or a^y mod p
2127
return otherPublicValue.modPow(secret, prime);
2228
}
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+
}
2338
}

0 commit comments

Comments
 (0)