检索 x509 证书的序列号时缺少前导零 [英] Missing leading zeroes while retrieving serial number of a x509 cert

查看:55
本文介绍了检索 x509 证书的序列号时缺少前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 X.509 证书中获取序列号.当我将代码生成的序列号与实际序列号(在 Windows 上)进行比较时,实际序列号(X509 的)的前导零证书)丢失.

I'm trying to get serial number from a X.509 Cert.. When i compare the Serial number generated by my code with the actual serial number(on windows), leading zeroes of the actual serial number(of the X509 cert) are missing.

任何建议或替代方法来获取 x.509 证书的十六进制序列号和前导零??

Any suggestions or alternative ways to get the serial number of the x.509 cert in hex with the leading zeroes??

以下是我目前使用的代码段:

Below is the code segment which I'm currently using:

InputStream in = new FileInputStream("cert");
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(in);
String serialNum = certificate.getSerialNumber().toString(16);
System.out.println(serialNum);

推荐答案

BigInteger 有一个 toByteArray() 方法可以重新添加前导 00:

BigInteger has a toByteArray() method that re-adds the leading 00s:

byte[] serialNumBA = certificate.getSerialNumber().toByteArray()

现在您有几种方法可以将字节数组转换为十六进制字符串:

Now you have several ways to convert the byte array to a hex string:

如何转换字节在 Java 中将数组转换为十六进制字符串?

例如使用 Apache 公共编解码器:

For example with Apache commons codec:

String serialNum = Hex.encodeHexString(serialNumBA);

这篇关于检索 x509 证书的序列号时缺少前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆