在java中将x509证书写入PEM格式的字符串?
是否有一些高级方法可以将 X509Certificate 写入 PEM 格式的字符串? 目前我正在执行 x509cert.encode() 将其写入 DER 格式的字符串,然后对其进行 Base 64 编码并附加页眉和页脚以创建 PEM 字符串,但这似乎很糟糕。特别是因为我也必须换行。
Is there some high level way to write an X509Certificate into a PEM formatted string?
Currently I'm doing x509cert.encode() to write it into a DER formatted string, then base 64 encoding it and appending the header and footer to create a PEM string, but it seems bad. Especially since I have to throw in line breaks too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这还不错。 Java不提供任何写入PEM文件的函数。你正在做的事情是正确的方法。即使 KeyTool 也做同样的事情,
如果你使用 BouncyCastle,你可以使用 PEMWriter 类在 PEM 中写出 X509 证书。
This is not bad. Java doesn't provide any functions to write PEM files. What you are doing is the correct way. Even KeyTool does the same thing,
If you use BouncyCastle, you can use PEMWriter class to write out X509 certificate in PEM.
还没有看到有人提出 Java 8 的
Base64.getMimeEncoder
方法 - 实际上允许您指定行长度 和 行分隔符,如下所示:我看看是否有这个 ^ 与标准编码器有什么区别,但我找不到任何东西。 javadoc 引用 RFC 2045 适用于 BASIC 和 MIME 编码器,并添加了 RFC 4648 适用于 BASIC。 AFAIK 这两个标准都使用相同的 Base64 字母表(表格看起来相同),因此如果您需要指定行长度,则应该使用 MIME。
这意味着使用 Java 8,可以通过以下方式完成
......
:
Haven't seen anyone bring up Java 8's
Base64.getMimeEncoder
method yet - actually allows you to specify both the line length and line separator like so:I looked to see if there was any difference with this ^ vs the standard encoder, and I couldn't find anything. The javadoc cites RFC 2045 for both BASIC and MIME encoders, with the addition of RFC 4648 for BASIC. AFAIK both of these standards use the same Base64 alphabet (tables look the same), so you should fine to use MIME if you need to specify a line length.
This means that with Java 8, this can be accomplished with:
...
...
之前的答案给出了与 3de party 软件(如 PHP)的兼容性问题,因为 PEM 证书未正确分块。
进口:
代码:
Previous answer gives compatibility problems with 3de party software (like PHP), because PEM cert is not correctly chunked.
Imports:
Code:
以下内容没有使用大型外部库或可能版本不一致的 sun.* 库。它建立在 judoman 的答案的基础上,但它也根据 OpenSSL、Java 和其他语言的要求,将行分成 64 个字符。
导入:
代码:(
我刚刚对柔道曼的答案发表了评论,但我没有足够的声誉点来允许发表评论,并且我的简单编辑被拒绝,因为它应该是评论或答案,所以这里是答案.)
如果您想直接写入文件,还需要导入 java.io.FileWriter 并:
The following uses no big external libraries or possibly version-inconsistent sun.* libraries. It builds on judoman's answer, but it also chunks lines at 64 characters, as required by OpenSSL, Java, and others.
Import:
Code:
(I would have just commented on judoman's answer, but I don't have enough reputation points to be allowed to comment, and my simple edit was rejected because it should have been a comment or an answer, so here's the answer.)
If you want to write straight to file, also
import java.io.FileWriter
and:如果您有来自充气城堡的 PEMWriter,那么您可以执行以下操作:
导入:
代码:
If you have PEMWriter from bouncy castle, then you can do the following :
Imports :
Code :
要基于 ZZ Coder 的想法构建,但不使用不能保证 JRE 版本之间一致的
sun.misc
类,请考虑以下使用类:
代码:
To build on ZZ Coder's idea, but without using the
sun.misc
classes that aren't guaranteed to be consistent between JRE versions, consider thisUse Class:
Code:
在 BouncyCastle 1.60 中,
PEMWriter
已被弃用,取而代之的是PemWriter
。PemWriter
是缓冲的,因此您需要在访问构建它的编写器之前刷新/关闭它。In BouncyCastle 1.60
PEMWriter
has been deprecated in favour ofPemWriter
.PemWriter
is buffered so you do need to flush/close it before accessing the writer that it was constructed with.几乎和@Andy Brown一样,少了一行代码。
正如他所说,PEMWriter 已被弃用。
Almost the same as @Andy Brown one less line of code.
As he said PEMWriter is deprecated.
使用 进行编码的另一种选择Guava 的 BaseEncoding:
然后:
Yet another alternative for encoding using Guava's BaseEncoding:
And then:
使用一些微小的 Base64,我制作了下面的东西,它不依赖于其他东西,如 Base64 或 bouncycastle。
Using some tiny Base64 I made below thing which does not depend on other things like base64 or bouncycastle.
Java 11 更新:
Java 11 update:
对于
BouncyCastle
来说,这是一种实现方法:With
BouncyCastle
this is one way to do it:使用 okhttp3-tls 可以在一行中
With okhttp3-tls it is possible in one line