Java 将 Asn.1 字节数组转换为浏览器方便的字符串
我尝试使用 Java 将 ASN.1 字节数组转换为字符串。到目前为止我的结果:
byte[] asn = ocspResponse.getEncoded();
String liccert = new String(asn, "Cp850");
这个字符串包含一些非常令人不安的字符串工件:
像这样:
4¦20110416173611Z0üÏ0üi0:0
有没有办法像这样转换字节数组:
RFMRIwEAYDVQQIEwlsb2NhbGhvc3QxFDASBgNV?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对不应该使用 String 构造函数将任意二进制数据转换为字符串。该构造函数用于获取实际上是编码文本的二进制数据并从中构建字符串。您的数据并不是真正的编码文本 - 它只是任意二进制数据。假装它最初是文本会导致数据丢失。
最好的方法通常是Base64 编码。有很多第三方库可以执行此操作,包括 Apache Commons Codec 或 此公共域编码器。
例如,使用后者:
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: