本文翻译自:How to check certificate name and alias in keystore files?
I have a bunch of .keystore files and need to find one with specific CN and alias. 我有一堆.keystore文件,需要找到一个具有特定CN和别名的文件。 Is there a way to do it with keytool, jarsigner or some other tool? 有没有办法使用keytool,jarsigner或其他工具来做到这一点? I found a way to check if specific keystore was used to sign a specific apk, but I also need to get the alias and certificate name in each of the files. 我找到了一种检查特定密钥库是否用于签署特定apk的方法,但是我还需要在每个文件中获取别名和证书名称。
参考:https://stackoom.com/question/s6Jf/如何检查密钥库文件中的证书名称和别名
You can run the following command to list the content of your keystore file (and alias name): 您可以运行以下命令来列出密钥库文件的内容(和别名):
keytool -v -list -keystore .keystore
If you are looking for a specific alias, you can also specify it in the command: 如果要查找特定的别名,也可以在命令中指定它:
keytool -list -keystore .keystore -alias foo
If the alias is not found, it will display an exception: 如果找不到别名,它将显示异常:
keytool error: java.lang.Exception: Alias does not exist keytool错误:java.lang.Exception:别名不存在
You can run from Java code. 您可以从Java代码运行。
try {
File file = new File(keystore location);
InputStream is = new FileInputStream(file);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "password";
keystore.load(is, password.toCharArray());
Enumeration enumeration = keystore.aliases();
while(enumeration.hasMoreElements()) {
String alias = enumeration.nextElement();
System.out.println("alias name: " + alias);
Certificate certificate = keystore.getCertificate(alias);
System.out.println(certificate.toString());
}
} catch (java.security.cert.CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null != is)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Certificate class holds all information about the keystore. 证书类包含有关密钥库的所有信息。
UPDATE- OBTAIN PRIVATE KEY 更新-获取私钥
Key key = keyStore.getKey(alias, password.toCharArray());
String encodedKey = new Base64Encoder().encode(key.getEncoded());
System.out.println("key ? " + encodedKey);
@prateek Hope this is what you looking for! @prateek希望这是您想要的!
KeyStore Explorer开源可视化工具来管理密钥库。
为了获得所有详细信息,我必须在romaintaz答案中添加-v选项:
keytool -v -list -keystore .keystore
In a bash-like environment you can use: 在类似bash的环境中,可以使用:
keytool -list -v -keystore cacerts.jks | grep 'Alias name:' | grep -i foo
This command consist of 3 parts. 该命令包括3个部分。 As stated above, the 1st part will list all trusted certificates with all the details and that's why the 2nd part comes to filter only the alias information among those details. 如上所述, 第一部分将列出具有所有详细信息的所有受信任证书,这就是为什么第二部分开始仅过滤那些详细信息中的别名信息的原因。 And finally in the 3rd part you can search for a specific alias (or part of it). 最后,在第3部分中,您可以搜索特定的别名(或其中的一部分)。 The -i turns the case insensitive mode on. -i打开不区分大小写的模式。 Thus the given command will yield all aliases containing the pattern 'foo', fe foo, 123_FOO, fooBar, etc. For more information man grep
. 因此,给定的命令将产生所有包含模式'foo',fe foo,123_FOO,fooBar等的别名。有关更多信息,请参见man grep
。