转换PEM RSA密钥字符串为DER的byte [] [英] Convert RSA pem key String to der byte[]

查看:216
本文介绍了转换PEM RSA密钥字符串为DER的byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个RSA PEM密钥(包含在一个字符串)转换为一个byte [],这样的方法并给定一个.pem文件的FileInputStream:

I'm trying to convert an RSA pem key (contained in a String) to a byte[], like this method does when given a .pem file FileInputStream:

<一个href="http://jets3t.s3.amazonaws.com/api/org/jets3t/service/security/EncryptionUtil.html#convertRsaPemToDer(java.io.InputStream)" rel="nofollow">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时。

I expect the result to be equivalent to what would be returned by org.jets3t.service.security.EncryptionUtil.convertRsaPemToDer() but it does not seem to be working when generating a CloudFront streaming URL.

知不知道我做错了什么?

Any idea what I'm doing wrong?

推荐答案

只是包装的字符串中 ByteArrayInputStream的键,您可以使用您链接的方法:

Just wrap the string in a ByteArrayInputStream and you can use the method you linked:

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

这篇关于转换PEM RSA密钥字符串为DER的byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆