使用 Java keytool 检查存储密码的可能性
Checking the possibility to store passwords with Java keytool
在 Java 8 中,选项 -importpassword 已添加到 keytool。它适用于 JKECS 存储类型:
$ keytool -importpassword -storetype JCEKS -alias 别名
输入要存储的密码:
重新输入密码:
$keytool -list -storetype JCEKS -keypass "" -keystore mystore.jceks
Keystore type: JCEKS
Keystore provider: SunJCE
Your keystore contains 1 entry
alias, Apr 7, 2016, SecretKeyEntry,
尝试提取它时,出现错误:
keytool error: java.lang.Exception: Alias <alias> has no certificate
我的问题是:如何提取密码?
看起来 keytool
无法 extract/export 使用 -importpass
命令导入的密码。但是你可以使用KeyStore
api查看密码,使用下面的代码:
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(new FileInputStream(new File("KEYSTORE_FILE")), "KEYSTORE_PASSWORD".toCharArray());
SecretKey passwordKey = (SecretKey) ks.getKey("ALIAS", "KEY_PASSWORD".toCharArray());
System.out.println(new String(passwordKey.getEncoded()));
在 Java 8 中,选项 -importpassword 已添加到 keytool。它适用于 JKECS 存储类型: $ keytool -importpassword -storetype JCEKS -alias 别名 输入要存储的密码: 重新输入密码:
$keytool -list -storetype JCEKS -keypass "" -keystore mystore.jceks
Keystore type: JCEKS
Keystore provider: SunJCE
Your keystore contains 1 entry
alias, Apr 7, 2016, SecretKeyEntry,
尝试提取它时,出现错误:
keytool error: java.lang.Exception: Alias <alias> has no certificate
我的问题是:如何提取密码?
看起来 keytool
无法 extract/export 使用 -importpass
命令导入的密码。但是你可以使用KeyStore
api查看密码,使用下面的代码:
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(new FileInputStream(new File("KEYSTORE_FILE")), "KEYSTORE_PASSWORD".toCharArray());
SecretKey passwordKey = (SecretKey) ks.getKey("ALIAS", "KEY_PASSWORD".toCharArray());
System.out.println(new String(passwordKey.getEncoded()));