Java KeyStore无需密码即可获取密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9758292/
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-08-16 07:17:00  来源:igfitidea点击:

KeyStore get key without password

javakeystorekeytool

提问by Nishant

I need to know if its possible to retrieve a key from a KeyStore without providing the 'storepass'. The documentation here says that "When retrieving information from the keystore, the password is optional; if no password is given, the integrity of the retrieved information cannot be checked and a warning is displayed"

我需要知道是否可以在不提供“storepass”的情况下从 KeyStore 检索密钥。这里的文档说“从密钥库中检索信息时,密码是可选的;如果没有给出密码,则无法检查检索到的信息的完整性并显示警告”

However when I try to get the Key without a password I get "java.lang.IllegalArgumentException: password can't be null"exception.

但是,当我尝试在没有密码的情况下获取密钥时,出现“java.lang.IllegalArgumentException:密码不能为空”异常。

Following is how I created the KeyStore

以下是我创建 KeyStore 的方式

keytool -genseckey -alias "myKey" -keystore KEYSTORE.jks -storepass "password" -storetype "JCEKS" -keyalg AES -keysize 128

keytool -genseckey -alias "myKey" -keystore KEYSTORE.jks -storepass "password" -storetype "JCEKS" -keyalg AES -keysize 128

And I tried to retrieve it as follows

我尝试按如下方式检索它

final KeyStore keyStore = KeyStore.getInstance("JCEKS");
keyStore.load(new FileInputStream(new File("C:\temp\keytool\KEYSTORE.jks")), 
  null);
final Key key = keyStore.getKey("myKey", null);

Which throws the following exception

抛出以下异常

java.lang.IllegalArgumentException: password can't be null
at com.sun.crypto.provider.SunJCE_z.<init>(DashoA13*..)
at com.sun.crypto.provider.JceKeyStore.engineGetKey(DashoA13*..)
at java.security.KeyStore.getKey(KeyStore.java:763)

Have I misunderstood the documentation completely? Is there any other way around this, as I don't see the point in storing the 'storepass' in clear in my code where everyone can see it, therefore making the password useless.

我是否完全误解了文档?有没有其他方法可以解决这个问题,因为我认为在我的代码中清楚地存储“storepass”没有意义,每个人都可以看到它,因此使密码无用。

回答by Cratylus

If storepass(which is used to access the private key) is not provided then it will try to use the keypassi.e. the keystore password, which you also don't provide.
That is why you get the exception.
You can not have boththe keypassand storepassnull
From your link:

如果storepass(用于访问私钥)未提供,则它将尝试使用keypass您也不提供的即密钥库密码。
这就是你得到异常的原因。
你不能keypassstorepass
从你的链接:

For a -keypass option, if you do not specify the option on the command line, keytool will first attempt to use the keystore password to recover the private/secret key, and if this fails, will then prompt you for the private/secret key password

对于 -keypass 选项,如果您没有在命令行指定该选项,keytool 将首先尝试使用密钥库密码来恢复私钥/秘密密钥,如果失败,则会提示您输入私钥/秘密密钥密码