在将代码从1.11迁移到2.x版本时,无法在AWS Java 2.x版本中找到PEM的替代版本

时间:2019-11-20 07:06:51

标签: aws-java-sdk-2.x

请为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;         
}

1 个答案:

答案 0 :(得分: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);
}