try {
factory = KeyFactory.getInstance("DSA");
return factory.generatePublic(pubKeySpec);
} catch (GeneralSecurityException e) {
throw new KeyException("Unable to derive public key from DSA private key", e);
}
} else if (key instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
try {
factory = KeyFactory.getInstance("RSA");
return factory.generatePublic(pubKeySpec);
} catch (GeneralSecurityException e) {
throw new KeyException("Unable to derive public key from RSA private key", e);
}
} else {
throw new KeyException("Private key was not a DSA or RSA key");
}
}