如何从命令行显示 Java 密钥库 SecretKeyEntry

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37487358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:32:45  来源:igfitidea点击:

How to display Java keystore SecretKeyEntry from command line

javacommand-linekeystore

提问by rlandster

I have a Java keystore file using the storetype JCEKS. It contains SecretKeyEntry entries. I would like to be able to dump, from the command line, the actual secret keys stored in this file. I tried this:

我有一个使用存储类型 JCEKS 的 Java 密钥库文件。它包含 SecretKeyEntry 条目。我希望能够从命令行转储存储在此文件中的实际密钥。我试过这个:

keytool -list -keystore secretkeys.jks -storetype JCEKS

which returned

返回

Keystore type: JCEKS
Keystore provider: SunJCE

Your keystore contains 1 entry

secret1, May 27, 2016, SecretKeyEntry

But that does not show me the key itself. How can I extract and look at, from the command line, the secret key?

但这并没有向我展示密钥本身。如何从命令行中提取和查看密钥?

回答by Omikron

This is not possible with keytool.

这对于 keytool 是不可能的。

Converting the keystore to PKCS#12 and then using OpenSSL to view the key doesn't work either, because this is a symmetric key (SecretKeyEntry).

将密钥库转换为 PKCS#12,然后使用 OpenSSL 查看密钥也不起作用,因为这是一个对称密钥 ( SecretKeyEntry)。

If you are stuck with the command line, you could write a small Java program that does it. Something like this:

如果您被命令行困住了,您可以编写一个小型 Java 程序来执行此操作。像这样的东西:

String fileName = "secretkey.ks";
char[] password = "mypassword".toCharArray();
String alias = "secret1";

KeyStore ks = KeyStore.getInstance("JCEKS");
try (FileInputStream fis = new FileInputStream(fileName)) {
    ks.load(fis, password);
    SecretKey secretKey = (SecretKey) ks.getKey(alias, password);
    System.out.println(new BigInteger(1, secretKey.getEncoded()).toString(16));
}

This prints out the secret key as a hex string (toString()with radix 16).

这会将密钥打印为十六进制字符串(toString()基数为 16)。

Or you could use the GUI program KeyStore Explorer.

或者您可以使用 GUI 程序KeyStore Explorer