码迷,mamicode.com
首页 > 移动开发 > 详细

JFrame实现批量获取Android安装包安全证书MD5

时间:2017-05-23 15:50:47      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:dir   文件内容   files   逻辑   jframe   clear   over   label   and   

今天遇到一个需求。获取全部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() + "\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();
	}
}

JFrame实现批量获取Android安装包安全证书MD5

标签:dir   文件内容   files   逻辑   jframe   clear   over   label   and   

原文地址:http://www.cnblogs.com/cynchanpin/p/6894206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!