zoukankan      html  css  js  c++  java
  • JFrame实现批量获取Android安装包安全证书MD5

    今天遇到一个需求。获取全部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);
    			}
    		});
    		// 開始运行button
    		window.getBeginButton().addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				inputPath = window.getJTextFiled();
    				if (inputPath != null && !"".equals(inputPath.trim())) {
    					notDirectoryExcute(new File(inputPath));
    				}
    			}
    		});
    		// 清空结果button
    		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() + "
    ");
    			}
    		}
    	}
    
    	/**
    	 * 读取压缩文件内容
    	 * 
    	 * @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();
    	}
    }

  • 相关阅读:
    Python 的并发编程
    django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
    python 文件目录遍历
    Ubuntu 18.04 登陆界面进去,几秒之后自动退出到登陆界面
    terminal 快捷操作
    Boost 源代码交叉编译
    tar 常见操作
    vim 快捷设置和操作
    Visual Studio Linker选项设置
    5. glutInitDisplayMode 函数理解
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6894206.html
Copyright © 2011-2022 走看看