1 package edu.jiangxin.apktoolbox.utils;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.security.Key;
6 import java.security.KeyStore;
7 import java.security.KeyStoreException;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.UnrecoverableKeyException;
10 import java.security.cert.CertificateException;
11
12
13
14
15
16
17
18 public class KeyVerify {
19
20 public static String verify(String keyStorePath, char[] password, String alias, char[] aliasPassword) {
21 KeyStore keyStore = null;
22 FileInputStream fis = null;
23 Key key = null;
24 String message;
25 try {
26 keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
27 fis = new FileInputStream(keyStorePath);
28 keyStore.load(fis, password);
29 key = keyStore.getKey(alias, aliasPassword);
30 if (key == null) {
31 message = "alias does not exist";
32 }
33 message = "OK";
34 } catch (NoSuchAlgorithmException | CertificateException | IOException | KeyStoreException
35 | UnrecoverableKeyException e) {
36 message = e.getMessage();
37 } finally {
38 if (fis != null) {
39 try {
40 fis.close();
41 } catch (IOException e) {
42 message = "closing fis fails";
43 }
44 }
45 }
46 return message;
47 }
48 }