app备案时需要使用apk中的签名公钥,以及md5,但官方提供的方法是使用jadxGUI去打开apk获取:
实际生产中apk都没有可能就要走备案流程。
但其实公钥模数和md5不需要apk,只需要keystore就行了。
这里自撸了一个jar来输出keystore的公钥模数与md5。
下载链接:
jar包使用很简单:
java -jar ParseKeystore-1.0.jar keystorePath password alias
回车即可得到keystore的模数与md5值:
需要注意的是,keystore的生成环境与解析环境要一致,否则可能导致失败。比如kesytore使用的jdk17生成的,然后在1.8环境下执行这个jar包就可能报错。
anyway,报错就用下面的源码自撸吧。
源码:
- public class Main {
- private static final String CERTIFICATE_TYPE_NAME = "X.509";
-
- public static void main(String[] args) {
- String path, password, alias;
- if (args.length == 3) {
- path = args[0];
- password = args[1];
- alias = args[2];
- } else {
- System.out.println("error, must fill path, password, alias params");
- return;
- }
- System.out.println("path: " + path + ", password:" + password + ", alias:" + alias);
- try {
- FileInputStream is = new FileInputStream(path);
- KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
- char[] passwd = password.toCharArray();
- keystore.load(is, passwd);
- Certificate cert = keystore.getCertificate(alias);
- String type = cert.getType();
- RSAPublicKey pub = (RSAPublicKey) cert.getPublicKey();
- String modulus = pub.getModulus().toString(10);
- System.out.println("modulus: " + modulus);
- if (CERTIFICATE_TYPE_NAME.equals(type) && cert instanceof X509Certificate) {
- X509Certificate x509cert = (X509Certificate) cert;
- String md5 = getThumbPrint(x509cert, "MD5");
- System.out.println("md5: " + md5);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static String getThumbPrint(X509Certificate cert, String type)
- throws NoSuchAlgorithmException, CertificateEncodingException {
- MessageDigest md = MessageDigest.getInstance(type);
- byte[] der = cert.getEncoded();
- md.update(der);
- byte[] digest = md.digest();
- return hexify(digest);
- }
-
- public static String hexify(byte[] bytes) {
- char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
- StringBuilder buf = new StringBuilder(bytes.length * 3);
- for (byte aByte : bytes) {
- buf.append(hexDigits[(aByte & 0xf0) >> 4]);
- buf.append(hexDigits[aByte & 0x0f]);
- buf.append(' ');
- }
- return buf.toString();
- }
- }