1. 程式人生 > >JFrame實現批量獲取Android安裝包安全證書MD5

JFrame實現批量獲取Android安裝包安全證書MD5

null direct true edr 結果 安全 全部 src cert

今天遇到一個需求。獲取全部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