一个专用密钥鉴于这种.pem
文件(与OpenSSL的生成,并用密码加密):获得来自RSA的.pem文件
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,AC009672952033EB
2wegzxf3MtncXS1CY3c.....
....
....
-----END RSA PRIVATE KEY-----
我如何在Java中PrivateKey
对象?我写了下面的代码,但我无法找到合适的方式来获得KeySpec
:
PrivateKey readFromPem(File keyFile, String password){
PemReader r = new PemReader(new InputStreamReader(new FileInputStream(keyFile)));
PemObject pemObject = r.readPemObject();
byte[] encodedKey = pemObject.getContent();
KeySpec keySpec = ???? // how to get this?
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(keySpec);
return key;
}
我想我应该建立一个RSAPrivateKeySpec
,但我不知道怎么办。我尝试了this answer和this other answer中的方法,但它们在解析字节数组时都会导致错误。
嗨,借助上述方法从密码保护的pfx文件读取私钥时获取NPE。 – mahi