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

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

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

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

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

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

openssl s_client -connect mydomain.com:8443

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

有帮助吗?

解决方案

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

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 密钥库中删除它并移动它的安全影响。

其他提示

使用密钥存储资源管理器GUI - http://keystore-explorer.sourceforge.net/ - 允许你提取从各种格式的一个.jks私钥。

http://anandsekar.github.io /导出最私钥-从-A-JKS-密钥库/

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();
        }
}

公共静态无效的主要(字串[] 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);

    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top