Fah*_*him 6 cryptography public-key-encryption x509
我有一个 X509Certificate,我将它写入/打印到一个文件中,如下所示。(我不是在写编码字节,因为我想阅读证书的内容)
X509Certificate cer = generateCertificate(); // cer is DER encoded
writeToFile( cer.toString() ); // cer.toString() converts DER to UTF/ASCII???
Run Code Online (Sandbox Code Playgroud)
后来我想将此文件(以上)作为字符串读取并创建一个新的 X509Certificate。
String cerStr = readCerFromFile(); // Read what is written above. In ASCII/ UTF format
ByteArrayInputStream bais = null;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
bais = new ByteArrayInputStream(cerStr.getBytes());
return (X509Certificate) cf.generateCertificate(bais);
} ...
Run Code Online (Sandbox Code Playgroud)
这将引发以下异常。
Java.security.cert.CertificateParsingException: Invalid DER-encoded certificate data
Run Code Online (Sandbox Code Playgroud)
很明显,我没有将 cerStr 转换为 DER 格式(我不知道是否可以转换为 DER )。任何人都可以解释如何从未编码的字符串创建 X509Certicate 。
提前致谢。!
简短的回答:你不能。DER 编码了太多的细节,无法轻松地与String. 您最好简单地使用cer.getEncoded()GregS 在评论中解释的方式保存 DER 编码证书。
如果您想查看证书的内容,只需使用操作系统识别的文件扩展名保存并双击它即可。如果您想使用命令行方法打印纯文本信息,请使用例如 openssl:
openssl x509 -text -noout -inform DER -in mycertificate.crt
Run Code Online (Sandbox Code Playgroud)
它在许多 Unix 版本(Linux、Apple)中是标准包含或可选的,也可以在 Windows 上运行。
| 归档时间: |
|
| 查看次数: |
6127 次 |
| 最近记录: |