猿问
下载APP

如何检查密钥库文件中的证书名称和别名?

我有一堆.keystore文件,需要找到一个具有特定CN和别名的文件。有没有办法使用keytool,jarsigner或其他工具来做到这一点?我找到了一种检查特定密钥库是否用于签署特定apk的方法,但是我还需要在每个文件中获取别名和证书名称。



胡说叔叔
浏览 156回答 3
3回答

largeQ

您可以运行以下命令来列出密钥库文件的内容(和别名):keytool -v -list -keystore .keystore如果要查找特定的别名,也可以在命令中指定它:keytool -list -keystore .keystore -alias foo如果找不到别名,它将显示异常:keytool错误:java.lang.Exception:别名不存在

慕标琳琳

为了获得所有详细信息,我必须在romaintaz答案中添加-v选项:keytool -v -list -keystore <FileName>.keystore

慕哥9229398

您可以从Java代码运行。try {&nbsp; &nbsp; &nbsp; &nbsp; File file = new File(keystore location);&nbsp; &nbsp; &nbsp; &nbsp; InputStream is = new FileInputStream(file);&nbsp; &nbsp; &nbsp; &nbsp; KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());&nbsp; &nbsp; &nbsp; &nbsp; String password = "password";&nbsp; &nbsp; &nbsp; &nbsp; keystore.load(is, password.toCharArray());&nbsp; &nbsp; &nbsp; &nbsp; Enumeration<String> enumeration = keystore.aliases();&nbsp; &nbsp; &nbsp; &nbsp; while(enumeration.hasMoreElements()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String alias = enumeration.nextElement();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("alias name: " + alias);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Certificate certificate = keystore.getCertificate(alias);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(certificate.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (java.security.cert.CertificateException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } catch (NoSuchAlgorithmException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } catch (KeyStoreException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }finally {&nbsp; &nbsp; &nbsp; &nbsp; if(null != is)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }证书类包含有关密钥库的所有信息。更新-获取私钥Key key = keyStore.getKey(alias, password.toCharArray());String encodedKey = new Base64Encoder().encode(key.getEncoded());System.out.println("key ? " + encodedKey);
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答