static PublicKey recoverPublicKey(KeyFactory kf, PrivateKey priv)
			throws NoSuchAlgorithmException, InvalidKeySpecException {
		if (priv instanceof RSAPrivateCrtKey) {
			RSAPrivateCrtKey rsaPriv = (RSAPrivateCrtKey) priv;
			return kf.generatePublic(new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv
					.getPublicExponent()));
		} else if (priv instanceof RSAPrivateKey) {
			BigInteger publicExponent = getRSAPublicExponentFromPkcs8Encoded(priv.getEncoded());
			RSAPrivateKey rsaPriv = (RSAPrivateKey) priv;
			return kf.generatePublic(new RSAPublicKeySpec(rsaPriv.getModulus(), publicExponent));
		} else if (priv instanceof DSAPrivateKey) {
			DSAPrivateKey dsaPriv = (DSAPrivateKey) priv;
			DSAParams params = dsaPriv.getParams();

			// Calculate public key Y
			BigInteger y = params.getG().modPow(dsaPriv.getX(), params.getP());

			return kf.generatePublic(new DSAPublicKeySpec(y, params.getP(), params.getQ(), params
					.getG()));
		} else if (priv instanceof ECPrivateKey) {
			ECPrivateKey ecPriv = (ECPrivateKey) priv;
			ECParameterSpec params = ecPriv.getParams();

			// Calculate public key Y
			ECPoint generator = params.getGenerator();
			BigInteger[] wCoords = EcCore.multiplyPointA(new BigInteger[] { generator.getAffineX(),
					generator.getAffineY() }, ecPriv.getS(), params);
			ECPoint w = new ECPoint(wCoords[0], wCoords[1]);

			return kf.generatePublic(new ECPublicKeySpec(w, params));
		} else {
			throw new NoSuchAlgorithmException("Key type must be RSA, DSA, or EC");
		}
	}
@Test
	public void recoverPublicKey_FakeKey_Failure() throws Exception {
		try {
			PubkeyUtils.recoverPublicKey(null, new MyPrivateKey());
			fail("Should not accept unknown key types");
		} catch (NoSuchAlgorithmException expected) {
		}
	}