Converting a PEM private key file to a JAVA PrivateKey Object

雨燕双飞 提交于 2019-12-11 02:33:46

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/21147566/converting-a-pem-private-key-file-to-a-java-privatekey-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!