40

keytool 有没有办法打印证书的公钥?我试过:

keytool -printcert -file client.crt

但它只提供以下信息:

Owner: CN=client, OU=as, O=as, L=as, ST=as, C=as
Issuer: EMAILADDRESS=as, CN=ca, OU=as, O=as, L=as, ST=as, C=as
Serial number: 3
Valid from: Tue Apr 10 12:18:47 GMT+05:30 2012 until: Wed Apr 10 12:18:47 GMT+05
:30 2013
Certificate fingerprints:
         MD5:  26:C0:29:E9:8C:AB:C3:9E:95:38:74:8A:87:D3:86:8D
         SHA1: 5C:5A:BA:47:44:83:7E:CB:48:BE:DD:E5:39:51:24:42:C6:C5:60:8B
         SHA256: DA:26:B8:C8:F4:04:3E:62:F3:7F:3B:EC:1D:9F:85:66:28:00:45:55:66:
15:FF:BB:37:77:97:59:F0:EC:0B:B6
         Signature algorithm name: SHA1withRSA
         Version: 1

这里面没有公钥。

4

5 回答 5

71

你可以这样做:

keytool -list -rfc -keystore mykeystore.jks -alias certificate_alias -storepass password

示例运行:

PS c:\sample> keytool -list -rfc -keystore mykeystore.jks -alias cert_alias -storepass password
Alias name: cert_alias
Creation date: Apr 25, 2014
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
-----BEGIN CERTIFICATE-----
MIIB4zCCAUygAwIBAgIIRzI14w7rL20wDQYJKoZIhvcNAQENBQAwMzELMAkGA1UEBhMCVVMxDTAL
BgNVBAoTBE5vbmUxFTATBgNVBAMTDE5vbmUgb3U9Tm9uZTAgFw0xNDA0MjQxNzQ0NDJaGA8yMTE0
MDQyNTE3NDQ0MlowMzELMAkGA1UEBhMCVVMxDTALBgNVBAoTBE5vbmUxFTATBgNVBAMTDE5vbmUg
b3U9Tm9uZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAivXBBtFnJTm1NbHysv3Mnpn/lCg6
1onJDxr/jkvI8+1Bljs1jktyYOeKDWxJwpDU7QyIqttgtDvRT4Yaew5WiQyADIyY0cBTvp3S7uKx
M5C3zxZdG6WTflU7xcYnGk3/d0VhwA6BL9YPsRaS/K+ww1yvxWKIOPW68wDe0ccvGWcCAwEAATAN
BgkqhkiG9w0BAQ0FAAOBgQB/5qDMA9fmlCWlOD9aHjBD6I8zAOSshMCFK8XcZJHowag8WtZyL3DR
insx2HoDlBewIJAEtAplo2NpeFyNtK93PS7zV+vwEYHCu46Db3klMksp3MmSXD39QPlmwfsGZlja
K8Ww0TsR5GtccFMH41KKa+PlvVZNEdZumdrca59olQ==
-----END CERTIFICATE-----
于 2014-04-25T20:57:26.393 回答
35

你可以用openssl.

如果此证书是 DER 编码(二进制),请使用:

openssl x509 -inform der -in client.crt -pubkey -noout

用于 PEM 编码的使用-inform pem选项(或根本没有-inform)。

要查看公钥的详细信息,请使用:

openssl x509 -inform der -in client.crt -pubkey -noout | openssl rsa -pubin -text -noout
于 2012-04-11T10:00:38.480 回答
13

您可以将证书打印为 pem 格式,然后使用 openssl 从 pem 格式打印公钥。

  1. -rfc向 -printcert添加选项

    keytool -printcert -rfc -file client.crt

  2. 将如下所示的输出保存到文件 client.pem

    -----开始证书----- MIIB4zCCAUygAwIBAgIIRzI14w7rL20wDQYJKoZIhvcNAQENBQAwMzELMAkGA1UEBhMCVVMxDTAL

    ……

    -----结束证书-----

  3. 然后使用 openssl

    openssl x509 -inform pem -text -in client.pem

所以你得到了公钥

于 2015-08-16T13:12:00.373 回答
8

Keytool list rfc只打印整个证书的base64 编码版本,而不是public key。Keytool 不支持打印证书的公钥。为此,我们可以使用openssl 。

于 2015-01-11T18:32:19.867 回答
0
keytool -export -alias myalias -keystore mystore.jks  | openssl x509 -inform der -pubkey -noout
于 2021-07-28T12:37:56.710 回答