* @return the corresponding public key
* @throws GeneralSecurityException if it didn't work
*/
public static PublicKey derivePublicKey(PrivateKey key) throws GeneralSecurityException {
if (key instanceof DSAPrivateKey) {
DSAPrivateKey dsaKey = (DSAPrivateKey) key;
DSAParams keyParams = dsaKey.getParams();
BigInteger g = keyParams.getG();
BigInteger p = keyParams.getP();
BigInteger q = keyParams.getQ();
BigInteger x = dsaKey.getX();
BigInteger y = q.modPow(x, p);
DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, p, q, g);
return KeyFactory.getInstance("DSA").generatePublic(keySpec);
} else if (key instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;