### Question:
SSHPublicKeyExtractor { public static String extract(final String pem, final String passPhrase) throws IOException { final Object priv = PEMDecoder.decode(pem.toCharArray(), passPhrase); if (priv instanceof RSAPrivateKey) { return "ssh-rsa " + DatatypeConverter.printBase64Binary(RSASHA1Verify.encodeSSHRSAPublicKey(((RSAPrivateKey)priv).getPublicKey())); } if (priv instanceof DSAPrivateKey) { return "ssh-dss " + DatatypeConverter.printBase64Binary(DSASHA1Verify.encodeSSHDSAPublicKey(((DSAPrivateKey)priv).getPublicKey())); } throw new IOException("should never happen"); }  static String extract(final String pem, final String passPhrase);  }### Answer:
@Test public void testExtractRSA() throws IOException { String pub = SSHPublicKeyExtractor.extract(RSA_PEM, null); assertEquals(RSA_EXPECTED, pub); }
@Test public void testExtractDSA() throws IOException { String pub = SSHPublicKeyExtractor.extract(DSA_PEM, null); assertEquals(DSA_EXPECTED, pub); }
@Test public void testExtractInvalidPEM1() throws IOException { boolean thrown = false; try { SSHPublicKeyExtractor.extract("", null); } catch (IOException e) { thrown = true; assertEquals("Invalid PEM structure, '-----BEGIN...' missing", e.getMessage()); } assertTrue(thrown); }
@Test public void testExtractInvalidPEM2() throws IOException { boolean thrown = false; try { SSHPublicKeyExtractor.extract("-----BEGIN RSA PRIVATE KEY-----", null); } catch (IOException e) { thrown = true; assertEquals("Invalid PEM structure, -----END RSA PRIVATE KEY----- missing", e.getMessage()); } assertTrue(thrown); }
@Test public void testExtractInvalidPEM3() throws IOException { boolean thrown = false; try { SSHPublicKeyExtractor.extract("-----BEGIN RSA PRIVATE KEY-----\nfoo\n-----END RSA PRIVATE KEY-----", null); } catch (IOException e) { thrown = true; assertEquals("Invalid PEM structure, no data available", e.getMessage()); } assertTrue(thrown); }
@Test public void testExtractInvalidPEM4() throws IOException { boolean thrown = false; try { SSHPublicKeyExtractor.extract("-----BEGIN RSA PRIVATE KEY-----\nVGhpcyBpcyBhIGpva2UhCg==\n-----END RSA PRIVATE KEY-----", null); } catch (IOException e) { thrown = true; assertEquals("Expected DER Sequence, but found type 84", e.getMessage()); } assertTrue(thrown); }