【问题标题】:How to create a certificate into a PKCS12 keystore with keytool?如何使用 keytool 在 PKCS12 密钥库中创建证书?
【发布时间】:2012-12-31 18:17:31
【问题描述】:

我想使用 keytool 程序将证书创建为 PKCS12 密钥库格式。

密钥库的扩展名为.pfx

我如何做到这一点?

【问题讨论】:

    标签: certificate ssl-certificate keytool pkcs#12


    【解决方案1】:

    如果密钥库是 PKCS12 类型 (.pfx),您必须使用 -storetype PKCS12 指定它(为便于阅读添加了换行符):

    keytool -genkey -alias <desired certificate alias> 
        -keystore <path to keystore.pfx>
        -storetype PKCS12 
        -keyalg RSA 
        -storepass <password> 
        -validity 730 
        -keysize 2048 
    

    【讨论】:

    • 问题的关键是如果keystore有扩展名.pfx你必须添加选项-storetype PKCS12没有这个选项keytool会抛出错误。
    • 使用keytool -list时,一定要附加-storetype PKCS12以在结果中看到Keystore type: PKCS12,而不是Keystore type: JKS。见this related answer
    【解决方案2】:

    问题关键的附加答案

    使用 JDK 8 (1.8.0_121-b13),如果您删除 -storetype pkcs12,则不会出现异常,但 keytool 会创建一个 JKS 密钥库,而 @987654324 @ 扩展名被忽略。

    它还要求-keypass mykeypasswordkeytool 不支持 PKCS12。

    %JAVA_HOME%/bin/keytool -genkeypair -alias mykey -keyalg EC -dname "cn=CN, ou=OU, o=O, c=C" -validity 365 -keystore keystore.pfx -keypass mykeypassword -storepass mystorepassword -v
    
    (translated)
    Generating keypair (Type EC, 256 Bit) and self-signed certificate (SHA256withECDSA) with a validity of 365 days
        for: CN=CN, OU=OU, O=O, C=C
    [keystore.pfx saved]
    

    列出内容:

    %JAVA_HOME%/bin/keytool -list -keystore keystore.pfx -storepass mystorepassword 
    
    (translated)
    Keystore-Type: JKS
    Keystore-Provider: SUN
    
    Keystore contains 1 entry.
    
    mykey, 25.04.2017, PrivateKeyEntry,
    Certificate-Fingerprint (SHA1): A1:6C:5F:8F:43:37:1A:B6:43:69:08:DE:6B:B9:4D:DB:05:C9:D5:84
    

    你看它是一个 Java 密钥库。

    下一个问题是,即使您在 -list 密钥库时指定 -storetype pkcs12,keytool 仍会将存储显示为 JKS 密钥库!

    让我们试试吧:

    %JAVA_HOME%/bin/keytool -genkeypair -alias mykey -keyalg EC -dname "cn=CN, ou=OU, o=O, c=C" -validity 365 -storetype pkcs12 -keystore keystore.pkx -keypass mykeypassword -storepass mystorepassword -v
    
    (translated)
    Warning: No support for different keystore and key password for PKCS12 keystores. The value of -keypass will be ignored.
    Generating keypair (Type EC, 256 Bit) and self signed certificate (SHA256withECDSA) with a validity of 365 Days
            für: CN=CN, OU=OU, O=O, C=C
    [keystore.pkx saved]
    

    现在列出内容:

    %JAVA_HOME%/bin/keytool -list -keystore keystore.pkx -storepass mystorepassword
    
    (translated)
    Keystore-Type: JKS // ??
    Keystore-Provider: SUN
    
    Keystore contains 1 entry
    
    mykey, 25.04.2017, PrivateKeyEntry,
    Certificate Fingerprint (SHA1): EA:C2:36:C6:55:69:CB:32:22:C7:14:83:67:47:D2:7E:06:8E:13:14
    

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 2015-09-22
      • 2017-07-23
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多