I have the following code snippet:
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 certificate) doesn't exist!"); } System.out.println("Loading company 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 has a certificate and a private key. Previously, I could read the file and get the private key, but now the file is password protected (encrypted). What is an instruction that I still donβt see. I know that I need to use the PEMDecryptorProvider and JcaPEMKeyConverter objects to get it, but I did not find the right combination.
source share