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

时间:2010-04-14 20:13:03

标签: tomcat ssl certificate self-signed keystore

我刚刚在运行tomcat 6的linux盒子上创建了一个自签名证书。

我创建了这样的密钥,有效期为10年:

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

并将密钥库复制到tomcat中的文件夹中,并更新server.xml以指向密钥库。

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

我可以使用以下方式生成公钥:

openssl s_client -connect mydomain.com:8443

但是如何导出/检索私钥?

4 个答案:

答案 0 :(得分:50)

这有点棘手。首先,您可以使用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 :(得分:5)

使用Keystore Explorer gui - http://keystore-explorer.sourceforge.net/ - 允许您以各种格式从.jks中提取私钥。

答案 2 :(得分: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();
        }
}

答案 3 :(得分:-1)

public static void main(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);

    }
}
相关问题