Open a pkcs8 file key without use OPENSSL

917 views Asked by At

I'm trying to open a pkcs8 KEY file and I have found some procedures that allow me to generate a PEM file using the OPENSSL library and then generate the PrivateKey from it. Unfortunately I want to open the KEY without manipulate the file previously. Is this possible?

1

There are 1 answers

0
hestellezg On

In this Web page I found the solution. It uses one library that Konstantin mentioned; It worked with the not-yet-commons-ssl-0.3.11.jar.

byte[] clavePrivada = getBytes(archivoClavePrivada); 
PKCS8Key pkcs8 = new PKCS8Key(clavePrivada, password.toCharArray()); 
PrivateKey pk = pkcs8.getPrivateKey();     

It uses the method getBytes()

private byte[] getBytes(InputStream is) { 
  int totalBytes = 714; 
  byte[] buffer = null; 
  try { 
    buffer = new byte[totalBytes]; 
    is.read(buffer, 0, totalBytes); 
    is.close(); 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } 
  return buffer; 
}