I have the following piece of code:
PEMParser pemParser;
File telexuskeys = new File(locationKey);
if(telexuskeys.exists())
pemParser = new PEMParser(new FileReader(telexuskeys));
else{
usage(ops);
throw new FileNotFoundException("The key file (company's certificate) doesn't exist!");
}
System.out.println("Loading company's certificate");
Object object = pemParser.readObject();
Object object2 = pemParser.readObject();
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passwordPem.toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
byte[] keyBytes = PrivateKeyInfo.getInstance(object2).getEncoded();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
PrivateKey pk = kf.generatePrivate(spec);
My pem file just have the certificate and the private key. I used to be able to read the file and obtain the private key but now the file is protected (encrypted) with a password. What is the instruction that I'm still missing. I know I need to use the PEMDecryptorProvider and the JcaPEMKeyConverter objects in order to obtain it but I haven't found the correct combination.