Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a PEM private key file to a JAVA PrivateKey Object

I know that there are several other questions about that topic, but none of them have helped me out. I tried the BouncyCastle lib as well. Could someone please help me here? The PEM file looks like:

    -----BEGIN RSA PRIVATE KEY-----
    MIIEpAIBAAKCAQEAq2eYtnTsEc/qyqS ...


   ... zY3WG++SA+amcXiO721hJWNC+uTbZ1bzQ==
    -----END RSA PRIVATE KEY-----

I'm using this method

public static PrivateKey getPemPrivateKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException  {
    File f = new File(PEMFILES_FOLDER+filename);
    FileInputStream fis = new FileInputStream(f);
    DataInputStream dis = new DataInputStream(fis);
    byte[] keyBytes = new byte[(int) f.length()];
    dis.readFully(keyBytes);
    dis.close();

    String temp = new String(keyBytes);
    //TODO care about the linefeeds
    String privKeyPEM = temp.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
    privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");

    System.out.println("Private key: \n"+privKeyPEM);

    Base64 b64 = new Base64();
    byte [] decoded = b64.decode(privKeyPEM);


    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
    KeyFactory kf = KeyFactory.getInstance(RSA);
    return kf.generatePrivate(spec);
}

I am getting this error:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
like image 803
Hansi Zimmrer Avatar asked Apr 25 '26 19:04

Hansi Zimmrer


1 Answers

I hope this can help you. I have copied a working copy of getPemPrivatekey and the way I call it in the main function:

public PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
    File f = new File(filename);
    FileInputStream fis = new FileInputStream(f);
    DataInputStream dis = new DataInputStream(fis);
    byte[] keyBytes = new byte[(int) f.length()];
    dis.readFully(keyBytes);
    dis.close();

    String temp = new String(keyBytes);
    String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----", "");
    privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
    //System.out.println("Private key\n"+privKeyPEM);

    BASE64Decoder b64 = new BASE64Decoder();
    byte[] decoded = b64.decodeBuffer(privKeyPEM);

    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
    KeyFactory kf = KeyFactory.getInstance(algorithm);
    return kf.generatePrivate(spec);
}

The main program looks like this:

...

gcsr = new ...... // instantiate the class here

privateKey = gcsr.getPemPrivateKey("c:\\testdir\\java_private.pem", "RSA");
BASE64Encoder encoder1 = new BASE64Encoder();
String s1 = encoder1.encodeBuffer(gcsr.getPrivateKey().getEncoded());
System.out.println("Private Key in Base64:" + s1 + "\n");

This is currently working (Java 8 on my computer!). "gcsr" is the name of the object I instantiated from the class containing the function. Regards.

like image 147
Desphilboy Avatar answered Apr 27 '26 10:04

Desphilboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!