【问题标题】:Convert RSA pem key String to der byte[]将 RSA pem 密钥字符串转换为 der byte[]
【发布时间】:2014-02-06 01:50:46
【问题描述】:

我正在尝试将 RSA pem 密钥(包含在字符串中)转换为 byte[],就像给定 .pem 文件 FileInputStream 时使用此方法一样:

http://jets3t.s3.amazonaws.com/api/org/jets3t/service/security/EncryptionUtil.html#convertRsaPemToDer(java.io.InputStream)

我试过了:

String pemKey = "-----BEGIN RSA PRIVATE KEY-----\r\n"
         + "{base64 encoded key part omitted}\r\n"
         + "{base64 encoded key part omitted}\r\n"
         + "{base64 encoded key part omitted}\r\n"
         + "-----END RSA PRIVATE KEY-----";
String base64 = pemKey
        .replaceAll("\\s", "")
        .replace("-----BEGINRSAPRIVATEKEY-----", "")
        .replace("-----ENDRSAPRIVATEKEY-----", "");

return Base64.decode(base64.getBytes());

我希望结果与 org.jets3t.service.security.EncryptionUtil.convertRsaPemToDer() 返回的结果相同,但在生成 CloudFront 流式 URL 时它似乎不起作用。

知道我做错了什么吗?

【问题讨论】:

    标签: java encryption amazon-web-services pem der


    【解决方案1】:

    只需将字符串包装在ByteArrayInputStream 中即可使用链接的方法:

    InputStream pemStream = new ByteArrayInputStream(pemKey.getBytes());
    byte[] derKey = EncryptionUtil.convertRsaPemToDer(pemStream);
    

    【讨论】:

    • 谢谢!我的方法产生了相同的结果;看起来问题出在其他地方......
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2015-11-15
    • 2021-07-14
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多