今天遇到一个需求,获取所有apk的签名的MD5,下面是我使用Java SE实现的一个工具,贴出核心源代码,希望给有需要的朋友有所帮助。
界面如下:
只需要制定.apk文件所在的目录即可,核心代码如下:
public class ReadCmdLine { private static MD5Window window; private static String inputPath; public static void main(String args[]) { window = new MD5Window(); window.setVisible(true); initWindow(); } private static void initWindow() { // 文件目录文本框 window.getFilePathButton().addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jfc.showDialog(new JLabel(), "选择"); File file = jfc.getSelectedFile(); notDirectoryExcute(file); } }); // 开始运行按钮 window.getBeginButton().addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { inputPath = window.getJTextFiled(); if (inputPath != null && !"".equals(inputPath.trim())) { notDirectoryExcute(new File(inputPath)); } } }); // 清空结果按钮 window.getClearButton().addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { window.setTextArea(""); } }); } /** * 判断是否是目录,如果不是则执行 * * @param file */ public static void notDirectoryExcute(File file) { if (file.isDirectory()) { inputPath = file.getAbsolutePath(); window.setJTextFiled(inputPath); File[] fiels = file.listFiles(); for (int i = 0; i < fiels.length; i++) { String absPath = fiels[i].getAbsolutePath(); if (absPath.contains(".apk")) { excute(absPath); } } } else { JOptionPane.showMessageDialog(window, "请选择目录"); } } /** * 核心逻辑 * * @param absPath */ public static void excute(String absPath) { // 1、从.apk中读取CERT.RSA文件 String appName = absPath.substring(absPath.lastIndexOf("_") + 1, absPath.lastIndexOf(".")); try { if (absPath != null) { readZipFile(absPath); } } catch (Exception e) { e.printStackTrace(); } // 2、执行 keytool命令获取MD5 Process process = null; List<String> processList = new ArrayList<String>(); try { process = Runtime.getRuntime().exec( "keytool -printcert -file D:/test/CERT.RSA"); BufferedReader input = new BufferedReader(new InputStreamReader( process.getInputStream())); String line = ""; while ((line = input.readLine()) != null) { processList.add(line); } input.close(); } catch (IOException e) { e.printStackTrace(); } // 过滤内容 for (String line : processList) { if (line.contains("MD5")) { window.setTextArea(window.getTextArea() + String.format("%-30s", appName) + line.trim() + "\n"); } } } /** * 读取压缩文件内容 * * @param file 压缩文件的路径 * @throws Exception */ public static void readZipFile(String file) throws Exception { ZipFile zf = new ZipFile(file); InputStream in = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zin = new ZipInputStream(in); File outFile = new File("D:\\test\\CERT.RSA"); OutputStream out = new FileOutputStream(outFile); InputStream rsaStream = zf.getInputStream(zf .getEntry("META-INF/CERT.RSA")); byte[] buf1 = new byte[1024]; int len; while ((len = rsaStream.read(buf1)) > 0) { out.write(buf1, 0, len); } rsaStream.close(); out.close(); in.close(); zin.closeEntry(); } }
时间: 2024-10-20 06:45:16