开发者

Java Convert Asn.1 byte Array in convienient String for Browser

开发者 https://www.devze.com 2023-02-25 02:44 出处:网络
I try to convert a ASN.1 Byte Array into a String with Java. My results up to now: byte[] asn = ocspResponse.getEncoded();

I try to convert a ASN.1 Byte Array into a String with Java. My results up to now:

byte[] asn = ocspResponse.getEncoded();
String liccert = new String(asn, "Cp850");
开发者_开发技巧

This String contains some String artifakts which are very disturbing:

Like this:

4¦20110416173611Z0üÏ0üi0:0  

Is there a way to convert the byte Array like this:

RFMRIwEAYDVQQIEwlsb2NhbGhvc3QxFDASBgNV?


You should absolutely not convert arbitrary binary data to a string using the String constructor. That constructor is for taking binary data which is actually encoded text and building a string from it. Your data isn't really encoded text - it's just arbitrary binary data. Pretending it was originally text is a recipe for data loss.

The best approach is usually to Base64 encode it. There are plenty of third party libraries to do this, including Apache Commons Codec or this public domain encoder.

For example, using the latter:

String liccert = Base64.encodeBytes(asn);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号