標籤:md5 android apk 簽名
今天遇到一個需求,擷取所有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() {@Overridepublic 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() {@Overridepublic void actionPerformed(ActionEvent arg0) {inputPath = window.getJTextFiled();if (inputPath != null && !"".equals(inputPath.trim())) {notDirectoryExcute(new File(inputPath));}}});// 清空結果按鈕window.getClearButton().addActionListener(new ActionListener() {@Overridepublic 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命令擷取MD5Process 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