使用java的X509序列号

我需要从X509证书中获取一些数据。

如果我在Windows中打开证书文件,它会以此格式显示其序列号。

ex. 39 65 70 eb d8 9f 28 20 4e c2 a0 6b 98 48 31 0d 

我试图使用java获取相同的数据。 加载后,我使用

 x509.getSerialNumber(); and result is : 76292708057987193002565060032465481997 

那么这两者有什么区别? 我希望结果为上一个。

Windows显示序列号的hex表示,而Java从X509Certificate.getSerialNumber()返回BigInteger结果。

要将BigInteger显示为hex值,只需调用toString(16)

 BigInteger bi = new BigInteger("76292708057987193002565060032465481997"); System.out.println(bi.toString(16)); 

将输出:

 396570ebd89f28204ec2a06b9848310d 

第一个是证书的hex值。 另一个是小数。

现在它取决于您如何转换初始证书bytearray以将其打印出来。

让我们说这是你的证书:

 byte[] cert = { (byte) 0xFD, (byte) 0xB1, (byte) 0xDD, ..., (byte) 0x00 }; BigInteger certVal = new BigInteger(cert); System.out.println("And result is (hex): " + certVal.toString(16));