### Question:
KeyReader { public PKCS8EncodedKeySpec readPrivateKey(FileInputStream fis, Optional<String> keyPassword) throws IOException { PEMParser keyReader = new PEMParser(new InputStreamReader(fis)); PEMDecryptorProvider decryptorProvider = new JcePEMDecryptorProviderBuilder().build(keyPassword.get().toCharArray()); Object keyPair = keyReader.readObject(); keyReader.close(); PrivateKeyInfo keyInfo; if (keyPair instanceof PEMEncryptedKeyPair) { PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptorProvider); keyInfo = decryptedKeyPair.getPrivateKeyInfo(); } else { keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo(); } return new PKCS8EncodedKeySpec(keyInfo.getEncoded()); }  PKCS8EncodedKeySpec readPrivateKey(FileInputStream fis, Optional<String> keyPassword);  }### Answer:
@Test public void canReadPrivateKeyWithNoPassword() throws IOException { KeyReader keyReader = new KeyReader(); FileInputStream pemPKey = new FileInputStream(PEMCertInfoTest.TEST_KEY_PATH); PKCS8EncodedKeySpec keySpec = keyReader.readPrivateKey(pemPKey, Optional.ofNullable("")); Assert.assertNotNull(keySpec); }
@Test public void canReadPrivateKeyWithPassword() throws IOException { Security.addProvider(new BouncyCastleProvider()); KeyReader keyReader = new KeyReader(); FileInputStream pemPKey = new FileInputStream(PEMCertInfoTest.TEST_KEY_PATH_WITH_PASSPHRASE); PKCS8EncodedKeySpec keySpec = keyReader.readPrivateKey(pemPKey, Optional.ofNullable(PEMCertInfoTest.TEST_KEY_PASSPHRASE) ); Assert.assertNotNull(keySpec); }