|
6 | 6 | import org.bouncycastle.crypto.AsymmetricCipherKeyPair; |
7 | 7 | import org.bouncycastle.crypto.CipherParameters; |
8 | 8 | import org.bouncycastle.crypto.generators.DHKeyPairGenerator; |
| 9 | +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; |
9 | 10 | import org.bouncycastle.crypto.params.DHKeyGenerationParameters; |
10 | 11 | import org.bouncycastle.crypto.params.DHParameters; |
11 | | -import org.bouncycastle.crypto.params.DHPublicKeyParameters; |
12 | 12 | import org.bouncycastle.crypto.params.DHPrivateKeyParameters; |
13 | | -import org.bouncycastle.crypto.params.AsymmetricKeyParameter; |
| 13 | +import org.bouncycastle.crypto.params.DHPublicKeyParameters; |
14 | 14 | import org.bouncycastle.crypto.params.ParametersWithRandom; |
15 | 15 |
|
16 | 16 | /** |
|
26 | 26 | */ |
27 | 27 | public class DHAgreement |
28 | 28 | { |
| 29 | + private static final BigInteger ONE = BigInteger.valueOf(1); |
| 30 | + |
29 | 31 | private DHPrivateKeyParameters key; |
30 | 32 | private DHParameters dhParams; |
31 | 33 | private BigInteger privateValue; |
@@ -89,6 +91,12 @@ public BigInteger calculateAgreement( |
89 | 91 |
|
90 | 92 | BigInteger p = dhParams.getP(); |
91 | 93 |
|
92 | | - return message.modPow(key.getX(), p).multiply(pub.getY().modPow(privateValue, p)).mod(p); |
| 94 | + BigInteger result = pub.getY().modPow(privateValue, p); |
| 95 | + if (result.compareTo(ONE) == 0) |
| 96 | + { |
| 97 | + throw new IllegalStateException("Shared key can't be 1"); |
| 98 | + } |
| 99 | + |
| 100 | + return message.modPow(key.getX(), p).multiply(result).mod(p); |
93 | 101 | } |
94 | 102 | } |
0 commit comments