Convert RSA pem key String to der byte[]

試著忘記壹切 提交于 2019-12-11 04:27:29

问题


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:

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

I've tried this:

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());

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?


回答1:


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);


来源:https://stackoverflow.com/questions/21173929/convert-rsa-pem-key-string-to-der-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!