In Java, I have a DSAPrivateKey
, which has an X
parameter, and also a DSAParams
with P
, Q
and G
parameters. I want to compute the corresponding DSAPublicKey
. I know I can construct a DSAPublicKeySpec
if I know Y
, P
, Q
, and G
, and then I can use the KeyFactory.generatePublic(KeySpec)
method to turn that into a DSAPublicKey
.
The thing I'm not sure about, is how to compute Y
given knowledge of X
, P
, Q
and G
. I guessed the answer was:
BigInteger y = g.multiply(x).mod(p);
But that produces exception:
Caused by: java.lang.IllegalArgumentException: Y value does not appear to be in correct group
at org.bouncycastle.crypto.asymmetric.KeyUtils.validated(Unknown Source)
at org.bouncycastle.crypto.asymmetric.AsymmetricDSAPublicKey.<init>(Unknown Source)
at org.bouncycastle.jcajce.provider.ProvDSAPublicKey.<init>(Unknown Source)
So obviously that guess isn't right. I also tried:
BigInteger y = g.modPow(x, p);
which gives the same exception.
I'm using BouncyCastle FIPS version 1.0.2, so I'd be happy with an answer that uses BouncyCastle classes, but I'd also be happy with one that doesn't use BouncyC