String privateKeyStr = privateKeyBuf.toString(Charset.forName("utf8"));
Reader privateKeyReader = new StringReader(privateKeyStr);
PEMParser parser = new PEMParser(privateKeyReader);
Object object = parser.readObject();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKey privateKey = null;
if (object instanceof PrivateKeyInfo) {
privateKey = converter.getPrivateKey((PrivateKeyInfo) object);
} else if (object instanceof PEMKeyPair) {
privateKey = converter.getKeyPair((PEMKeyPair) object).getPrivate();
} else if (object instanceof PEMEncryptedKeyPair) {
char[] passphraseChars = null;
if ( passphrase == null ) {
passphraseChars = new char[]{};
} else {
passphraseChars = passphrase.toCharArray();
}
PEMDecryptorProvider decryptor = new JcePEMDecryptorProviderBuilder().build(passphraseChars);
try {
privateKey = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decryptor)).getPrivate();
} catch (Exception e) {
throw new Exception( "Invalid passphrase" );
}
} else {