【问题标题】:How to get public key in hex format from a X.509 certificate如何从 X.509 证书中获取十六进制格式的公钥
【发布时间】:2017-08-01 18:53:11
【问题描述】:

是否可以通过openssl只获取十六进制格式的公钥?我已经使用了命令:

openssl x509 -in a.pem -text -noout

这只是打印证书,其中公钥以十六进制格式提供,但我无法解析它。例如这个命令:

openssl x509 -in a.pem -pubkey -noout

以下列格式返回公钥:

-----BEGIN PUBLIC KEY-----
#######
####===
-----END PUBLIC KEY----

有没有更好的方法来做到这一点?我期待以十六进制格式输出。

【问题讨论】:

标签: openssl cryptography x509


【解决方案1】:

由于(经过讨论)它是 Base64(装甲 ASCII)格式的自签名密钥,因此tomeko.net 之类的工具足以将其编码为十六进制。


原答案:

来自this article,获取可信证书:

从 X.509 证书中解析公钥并将其表示为十六进制数字变得简单易行。

openssl x509 -modulus -noout < pub.cer | sed s/Modulus=/0x/

只需将 pub.cer 替换为您要解析的证书文件

这使用modulus option

结果应该是这样的:

0xB1E057678343....

注意:以上内容适用于 X.509v3 文件,其中包含以“-- BEGIN ...”行为前缀的 ASCII (Base64) 保护数据(即实际 PEM 文件)。

如果您收到以下错误,则表示您正在尝试查看 DER 编码的证书:

unable to load certificate
PEM routines:PEM_read_bio:no start line:pem_lib.c:
Expecting: TRUSTED CERTIFICATE

对于der文件,还要注意the public key in DER format (which is a way of expressing X.509 objects as a sequence of bytes) includes more than just the modulus, but also the exponent (usually short) and the algorithm identifier

转换certificate from DER to PEM first

openssl x509 -inform der -in certificate.cer -out certificate.pem

然后再试一次

【讨论】:

  • 感谢您的回复。但出现错误:unable to load certificate 49367:error:0906D06C:PEM routines:PEM_read_bio:no start line:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.30.2/src/crypto/pem/pem_lib.c:648:Expecting: TRUSTED CERTIFICATE 尝试使用公钥,Modulus=Wrong Algorithm type 尝试使用证书。
  • @GowthamG 是您的私人 pem 证书,一个实际的 X.509v3 文件,其中包含前缀为“-- BEGIN”的 ASCII (Base64) 装甲数据?
  • 是的,它是一个X509v3 文件,它有一些像X509v3 extensions: X509v3 Basic Constraints 这样的组件,它是一个自签名证书。
  • 仅供参考,它以-----BEGIN CERTIFICATE----- 开头并以-----END CERTIFICATE----- 结尾
  • 没关系,原证书只有pem格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 2017-03-10
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多