### Question:
PubkeyUtils { 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(); 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(); 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"); } } private  PubkeyUtils(); static String formatKey(Key key); static byte[] getEncodedPrivate(PrivateKey pk, String secret); static PrivateKey decodePrivate(byte[] encoded, String keyType); static PrivateKey decodePrivate(byte[] encoded, String keyType, String secret); static int getBitStrength(byte[] encoded, String keyType); static PublicKey decodePublic(byte[] encoded, String keyType); static KeyPair convertToKeyPair(PubkeyBean keybean, String password); static KeyPair recoverKeyPair(byte[] encoded); static String convertToOpenSSHFormat(PublicKey pk, String origNickname); static byte[] extractOpenSSHPublic(KeyPair pair); static String exportPEM(PrivateKey key, String secret); static final String PKCS8_START; static final String PKCS8_END; }### Answer:
@Test public void recoverPublicKey_FakeKey_Failure() throws Exception { try { PubkeyUtils.recoverPublicKey(null, new MyPrivateKey()); fail("Should not accept unknown key types"); } catch (NoSuchAlgorithmException expected) { } }