将PEM格式的字符串转换为java

将PEM格式的字符串转换为java

本文介绍了将PEM格式的字符串转换为java.security.cert.X509Certificate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用PEM格式的字符串创建 java.security.cert.X509Certificate 实例?PEM格式的字符串是HTTP请求"SSL_CLIENT_CERT"标头值.

How does one create a java.security.cert.X509Certificate instance from a PEM-formatted String? The PEM-formatted String is a HTTP request "SSL_CLIENT_CERT" header value.

答案:基于mgaert的回答,这是我在Scala中写的内容:

val cert = factory.generateCertificate(
    new ByteArrayInputStream(
      Base64.decodeBase64(
        cert.stripPrefix("-----BEGIN CERTIFICATE-----").stripSuffix("-----END CERTIFICATE-----")
      )
    ).asInstanceOf[X509Certificate]

推荐答案

使用一些InputStream读取将Base64解码为二进制,然后尝试

Decode the Base64 to binary, with some InputStream reading it, then try

CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(is);

这篇关于将PEM格式的字符串转换为java.security.cert.X509Certificate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:54