如何从自签名证书的密钥库中导出私钥

25

我刚在运行Tomcat 6的Linux服务器上创建了一个自签名证书。

我按照以下方式创建了10年有效期的密钥:

keytool -genkey -alias tomcatorange -keyalg RSA -validity 3650

我将密钥库复制到Tomcat中的一个文件夹,并更新了server.xml以指向该密钥库。

现在我的网络管理员要求提供公钥和私钥(用于我们的负载均衡器)。

我可以使用以下命令生成公钥:

openssl s_client -connect mydomain.com:8443

但我该如何导出/检索私钥?


一个很好的参考资料 https://8gwifi.org/docs/jks.jsp - anish
4个回答

58

这有点棘手。首先,您可以使用 keytool 将私钥放入 PKCS12 格式中,该格式比 Java 的各种密钥库格式更便携/兼容。以下是一个示例,将Java密钥库中别名为“mykey”的私钥复制到名为 myp12file.p12 的PKCS12文件中。 [请注意,在大多数屏幕上,此命令会超出文本框的右侧:您需要向右滚动才能查看全部内容]

keytool -v -importkeystore -srckeystore .keystore -srcalias mykey -destkeystore myp12file.p12 -deststoretype PKCS12
Enter destination keystore password:  
Re-enter new password: 
Enter source keystore password:  
[Storing myp12file.p12]

现在文件myp12file.p12以PKCS12格式包含了私钥,可以直接被许多软件包使用或者使用openssl pkcs12命令进行进一步处理。例如,

openssl pkcs12 -in myp12file.p12 -nocerts -nodes
Enter Import Password:
MAC verified OK
Bag Attributes
    friendlyName: mykey
    localKeyID: 54 69 6D 65 20 31 32 37 31 32 37 38 35 37 36 32 35 37 
Key Attributes: <No Attributes>
-----BEGIN RSA PRIVATE KEY-----
MIIC...
.
.
.
-----END RSA PRIVATE KEY-----

打印出未加密的私钥。

请注意,这是一个 私钥有责任认识到从Java密钥库中删除它并将其移动的安全性含义。


1
上述openssl命令出现以下错误:6016:error:0D07207B:asn1编码程序:ASN1_get_object:头部过长:.\crypto\asn1\asn1_lib.c:150: - user265330
哇,谢谢你。我已经为这个问题苦恼了三天了。最后才发现我一直在使用错误的private.key。我从JKS中提取了正确的private.key,现在终于解决了。非常感谢你,加上一万分! - cbmeeks
7
针对所有遇到“输出密钥和证书错误”的用户:尝试在源和目标中使用相同的密码。 - James Watkins
3
如果其他人遇到了与@user265330相同的问题,请尝试在回答中的代码框中向右滚动,并包含“-deststoretype”参数。并不是说我做了这件事,或者什么的... - Dave Mulligan
@DaveMulligan:如果你认为有帮助的话,我可以移动命令行参数。我也可以将其分成两行或更多行,但我认为这可能看起来很丑。 - President James K. Polk
显示剩余2条评论

6

2

http://anandsekar.github.io/exporting-the-private-key-from-a-jks-keystore/

public class ExportPrivateKey {
        private File keystoreFile;
        private String keyStoreType;
        private char[] password;
        private String alias;
        private File exportedFile;

        public static KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
                try {
                        Key key=keystore.getKey(alias,password);
                        if(key instanceof PrivateKey) {
                                Certificate cert=keystore.getCertificate(alias);
                                PublicKey publicKey=cert.getPublicKey();
                                return new KeyPair(publicKey,(PrivateKey)key);
                        }
                } catch (UnrecoverableKeyException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (KeyStoreException e) {
        }
        return null;
        }

        public void export() throws Exception{
                KeyStore keystore=KeyStore.getInstance(keyStoreType);
                BASE64Encoder encoder=new BASE64Encoder();
                keystore.load(new FileInputStream(keystoreFile),password);
                KeyPair keyPair=getPrivateKey(keystore,alias,password);
                PrivateKey privateKey=keyPair.getPrivate();
                String encoded=encoder.encode(privateKey.getEncoded());
                FileWriter fw=new FileWriter(exportedFile);
                fw.write(“—–BEGIN PRIVATE KEY—–\n“);
                fw.write(encoded);
                fw.write(“\n“);
                fw.write(“—–END PRIVATE KEY—–”);
                fw.close();
        }


        public static void main(String args[]) throws Exception{
                ExportPrivateKey export=new ExportPrivateKey();
                export.keystoreFile=new File(args[0]);
                export.keyStoreType=args[1];
                export.password=args[2].toCharArray();
                export.alias=args[3];
                export.exportedFile=new File(args[4]);
                export.export();
        }
}

-1

公共静态无返回值主函数(String[] args) {

try {
        String keystorePass = "20174";
        String keyPass = "rav@789";
        String alias = "TyaGi!";
        InputStream keystoreStream = new FileInputStream("D:/keyFile.jks");
        KeyStore keystore = KeyStore.getInstance("JCEKS");
        keystore.load(keystoreStream, keystorePass.toCharArray());
        Key key = keystore.getKey(alias, keyPass.toCharArray());

        byte[] bt = key.getEncoded();
        String s = new String(bt);
        System.out.println("------>"+s);      
        String str12 = Base64.encodeBase64String(bt);

        System.out.println("Fetched Key From JKS : " + str12);

    } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException ex) {
        System.out.println(ex);

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接