1

您能否建议 PEM 读取私钥的替代课程。代码如下:

import com.amazonaws.auth.PEM;

public PrivateKey getPrivateKey(String filename1) throws Exception {                
    InputStream res= new FileInputStream(filename1);              
    PrivateKey key = PEM.readPrivateKey(res);              
    return key;         
}
4

1 回答 1

0

通用 Java API 呢?

public PrivateKey read(String filename) throws Exception {
    final byte[] bytes = Files.readAllBytes(Paths.get(filename));
    final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
    final KeyFactory kf = KeyFactory.getInstance("RSA");

    return kf.generatePrivate(spec);
}
于 2019-11-20T14:04:38.263 回答