Java批量解析微信dat文件,微信圖片

參考地址:https://blog.csdn.net/a627428179/article/details/95485146

以下是java代碼,直接可用,不用手動計算異或值
此處的jdk版本需要1.8以上…,另外兩個參數(解析文件路徑和輸出路徑)需要改成自己的哦~
此處的原理是判斷圖片文件的十六進制特徵碼。

第一種:weChatImgRevert.java

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class weChatImgRevert {

    public static void main(String[] args) {
        String path = "D:\\WeChatSetup\\WeChat Files\\happy\\FileStorage\\Image\\2019-07";
        String targetPath = "F:\\wechat\\";
        convert(path, targetPath);
    }

    /**
     * @param path       圖片目錄地址
     * @param targetPath 轉換後目錄
     */
    private static void convert(String path, String targetPath) {
        File[] file = new File(path).listFiles();
        if (file == null) {
            return;
        }
        int size = file.length;
        System.out.println("總共" + size + "個文件");
        AtomicReference<Integer> integer = new AtomicReference<>(0);
        AtomicInteger x = new AtomicInteger();
        x.set((int) getXor(file[0])[1]);
        Arrays.stream(file).parallel().forEach(file1 -> {
            Object[] xor = getXor(file1);
            try (InputStream reader = new FileInputStream(file1);
                 OutputStream writer =
                         new FileOutputStream(targetPath + file1.getName().split("\\.")[0] + "." + xor[0])) {
                byte[] bytes = new byte[1024 * 10];
                int b;
                while ((b = reader.read(bytes)) != -1) {//這裏的in.read(bytes);就是把輸入流中的東西,寫入到內存中(bytes)。
                    for (int i = 0; i < bytes.length; i++) {
                        bytes[i] = (byte) (int)(bytes[i] ^ ((int) xor[1] == 0 ? x.get() : (int) xor[1]));
                        if (i == (b - 1)) {
                            break;
                        }
                    }
                    writer.write(bytes, 0, b);
                    writer.flush();
                }
                integer.set(integer.get() + 1);
                System.out.println(file1.getName() + "(大小:" + ((double) file1.length() / 1000) + "kb,異或值:" + xor[1] + ")," +
                        "進度:" + integer.get() +
                        "/" + size);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.out.println("解析完畢!");
    }

    /**
     * 判斷特徵碼所屬異或值
     *
     * @param file
     * @return
     */
    private static Object[] getXor(File file) {
        String index = "jpg";
        int xori = 0;
        if (file != null) {
            xori = getJPEGXor(file);
            if (xori == 0) {
                xori = getPNGXor(file);
                index = "png";
            }
            if (xori == 0) {
                xori = getBMPXor(file);
                index = "bmp";
            }
            if (xori == 0) {
                xori = getGIFXor(file);
                index = "gif";
            }
            if (xori == 0) {
                xori = getICOXor(file);
                index = "ico";
            }
            if (xori == 0) {
                index = "jpg";
            }
        }
        return new Object[]{index, xori};
    }

    /**
     * @param file
     * @return
     */
    private static Integer getJPEGXor(File file) {
        int[] xors = new int[4];
        try (InputStream reader = new FileInputStream(file)) {
            xors[0] = reader.read() ^ 0xFF;
            xors[1] = reader.read() ^ 0xD8;
           /* reader.skip(file.length() - 4);
            xors[2] = reader.read() ^ 0xDA;
            xors[3] = reader.read() ^ 0x03;*/
            System.out.println(reader.read());
            System.out.println(xors[0]);
            System.out.println(xors[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (xors[0] == xors[1]/** && xors[1] == xors[2] && xors[2] == xors[3]**/) {
            return xors[0];
        } else {
            return 0;
        }
    }

    /**
     * @param file
     * @return
     */
    private static Integer getPNGXor(File file) {
        int[] xors = new int[4];
        try (InputStream reader = new FileInputStream(file)) {
            xors[0] = reader.read() ^ 0x89;
            xors[1] = reader.read() ^ 0x50;
            xors[2] = reader.read() ^ 0x4E;
            xors[3] = reader.read() ^ 0x47;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (xors[0] == xors[1] && xors[1] == xors[2] && xors[2] == xors[3]) {
            return xors[0];
        } else {
            return 0;
        }
    }

    /**
     * @param file
     * @return
     */
    private static Integer getGIFXor(File file) {
        int[] xors = new int[4];
        try (InputStream reader = new FileInputStream(file)) {
            xors[0] = reader.read() ^ 0x47;
            xors[1] = reader.read() ^ 0x49;
            xors[2] = reader.read() ^ 0x46;
            xors[3] = reader.read() ^ 0x38;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (xors[0] == xors[1] && xors[1] == xors[2] && xors[2] == xors[3]) {
            return xors[0];
        } else {
            return 0;
        }
    }

    /**
     * @param file
     * @return
     */
    private static Integer getBMPXor(File file) {
        int[] xors = new int[2];
        try (InputStream reader = new FileInputStream(file)) {
            xors[0] = reader.read() ^ 0x42;
            xors[1] = reader.read() ^ 0x4D;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (xors[0] == xors[1]) {
            return xors[0];
        } else {
            return 0;
        }
    }

    /**
     * @param file
     * @return
     */
    private static Integer getICOXor(File file) {
        int[] xors = new int[4];
        try (InputStream reader = new FileInputStream(file)) {
            xors[0] = reader.read();
            xors[1] = reader.read();
            xors[2] = reader.read() ^ 0x01;
            xors[3] = reader.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (xors[0] == xors[1] && xors[1] == xors[2] && xors[2] == xors[3]) {
            return xors[0];
        } else {
            return 0;
        }
    }
}

第二種,細節稍有不同,文件類型較全:WxChatImgRevert2.java

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class WxChatImgRevert2 {
		
	public static void main(String[] args) {
		  String path = "D:\\WeChatSetup\\WeChat Files\\happy\\FileStorage\\Image\\2019-07";
	        String targetPath = "F:\\wechat\\2019-07";
        convert(path, targetPath);
    }

    /**
     * @param path       圖片目錄地址
     * @param targetPath 轉換後目錄
     */
    private static void convert(String path, String targetPath) {
        File[] file = new File(path).listFiles();
        if (file == null) {
            return;
        }
        int size = file.length;
        System.out.println("總共" + size + "個文件");
        AtomicReference<Integer> integer = new AtomicReference<>(0);
        AtomicInteger x = new AtomicInteger();
        for (File file1 : file) {
            if (file1.isFile()) {
                Object[] xori = getXor(file1);
                if (xori != null && xori[1] != null){
                    x.set((int)xori[1]);
                }
                break;
            }
        }
        Arrays.stream(file).parallel().forEach(file1 -> {
            if (file1.isDirectory()) {
                String[] newTargetPath = file1.getPath().split("/|\\\\");
                File targetFile = new File(targetPath+File.separator+newTargetPath[newTargetPath.length - 1]);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                convert(file1.getPath(),targetPath+File.separator+newTargetPath[newTargetPath.length - 1]);
                return;
            }
            Object[] xor = getXor(file1);
            if (x.get() == 0 && xor[1] != null && (int) xor[1] != 0) {
                x.set((int) xor[1]);
            }
            xor[1] = xor[1] == null ? x.get() : xor[1];
            try (InputStream reader = new FileInputStream(file1);
                 OutputStream writer =
                         new FileOutputStream(targetPath + File.separator + file1.getName().split("\\.")[0] + (xor[0] != null ?
                                 "." + xor[0] : ""))) {
                byte[] bytes = new byte[1024 * 10];
                int b;
                while ((b = reader.read(bytes)) != -1) {//這裏的in.read(bytes);就是把輸入流中的東西,寫入到內存中(bytes)。
                    for (int i = 0; i < bytes.length; i++) {
                        bytes[i] = (byte) (int) (bytes[i] ^ (int) xor[1]);
                        if (i == (b - 1)) {
                            break;
                        }
                    }
                    writer.write(bytes, 0, b);
                    writer.flush();
                }
                integer.set(integer.get() + 1);
                System.out.println(file1.getName() + "(大小:" + ((double) file1.length() / 1000) + "kb,異或值:" + xor[1] + ")," +
                        "進度:" + integer.get() +
                        "/" + size);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.out.println("解析完畢!");
    }

    /**
     * 判斷圖片異或值
     *
     * @param file
     * @return
     */
    private static Object[] getXor(File file) {
        Object[] xor = null;
        if (file != null) {
            byte[] bytes = new byte[4];
            try (InputStream reader = new FileInputStream(file)) {
                reader.read(bytes, 0, bytes.length);
            } catch (Exception e) {
                e.printStackTrace();
            }
            xor = getXor(bytes);
        }
        return xor;
    }

    /**
     * @param bytes
     * @return
     */
    private static Object[] getXor(byte[] bytes) {
        Object[] xorType = new Object[2];
        int[] xors = new int[3];
        for (Map.Entry<String, String> type : FILE_TYPE_MAP.entrySet()) {
            String[] hex = {
                    String.valueOf(type.getKey().charAt(0)) + type.getKey().charAt(1),
                    String.valueOf(type.getKey().charAt(2)) + type.getKey().charAt(3),
                    String.valueOf(type.getKey().charAt(4)) + type.getKey().charAt(5)
            };
            xors[0] = bytes[0] & 0xFF ^ Integer.parseInt(hex[0], 16);
            xors[1] = bytes[1] & 0xFF ^ Integer.parseInt(hex[1], 16);
            xors[2] = bytes[2] & 0xFF ^ Integer.parseInt(hex[2], 16);
            if (xors[0] == xors[1] && xors[1] == xors[2]) {
                xorType[0] = type.getValue();
                xorType[1] = xors[0];
                break;
            }
        }
        return xorType;
    }

    private final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();

    static {
        getAllFileType();
    }

    private static void getAllFileType() {
        FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg"); //JPEG (jpg)
        FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png"); //PNG (png)
        FILE_TYPE_MAP.put("47494638396126026f01", "gif"); //GIF (gif)
        FILE_TYPE_MAP.put("49492a00227105008037", "tif"); //TIFF (tif)
        FILE_TYPE_MAP.put("424d228c010000000000", "bmp"); //16色位圖(bmp)
        FILE_TYPE_MAP.put("424d8240090000000000", "bmp"); //24位位圖(bmp)
        FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp"); //256色位圖(bmp)
        FILE_TYPE_MAP.put("41433130313500000000", "dwg"); //CAD (dwg)
        FILE_TYPE_MAP.put("3c21444f435459504520", "html"); //HTML (html)
        FILE_TYPE_MAP.put("3c21646f637479706520", "htm"); //HTM (htm)
        FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css"); //css
        FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js"); //js
        FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf"); //Rich Text Format (rtf)
        FILE_TYPE_MAP.put("38425053000100000000", "psd"); //Photoshop (psd)
        FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml"); //Email [Outlook Express 6] (eml)
        FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc"); //MS Excel 注意:word、msi 和 excel的文件頭一樣
        FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd"); //Visio 繪圖
        FILE_TYPE_MAP.put("5374616E64617264204A", "mdb"); //MS Access (mdb)
        FILE_TYPE_MAP.put("252150532D41646F6265", "ps");
        FILE_TYPE_MAP.put("255044462d312e360d25", "pdf"); //Adobe Acrobat (pdf)
        FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb"); //rmvb/rm相同
        FILE_TYPE_MAP.put("464c5601050000000900", "flv"); //flv與f4v相同
        FILE_TYPE_MAP.put("00000020667479706973", "mp4");
        FILE_TYPE_MAP.put("49443303000000000f76", "mp3");
        FILE_TYPE_MAP.put("000001ba210001000180", "mpg"); //
        FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv"); //wmv與asf相同
        FILE_TYPE_MAP.put("524946464694c9015741", "wav"); //Wave (wav)
        FILE_TYPE_MAP.put("52494646d07d60074156", "avi");
        FILE_TYPE_MAP.put("4d546864000000060001", "mid"); //MIDI (mid)
        FILE_TYPE_MAP.put("504b0304140000000800", "zip");
        FILE_TYPE_MAP.put("526172211a0700cf9073", "rar");
        FILE_TYPE_MAP.put("235468697320636f6e66", "ini");
        FILE_TYPE_MAP.put("504b03040a0000000000", "jar");
        FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");//可執行文件
        FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");//jsp文件
        FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");//MF文件
        FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");//xml文件
        FILE_TYPE_MAP.put("efbbbf2f2a0d0a53514c", "sql");//xml文件
        FILE_TYPE_MAP.put("7061636b616765207765", "java");//java文件
        FILE_TYPE_MAP.put("406563686f206f66660d", "bat");//bat文件
        FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");//gz文件
        FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "properties");//bat文件
        FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");//bat文件
        FILE_TYPE_MAP.put("49545346030000006000", "chm");//bat文件
        FILE_TYPE_MAP.put("04000000010000001300", "mxp");//bat文件
        FILE_TYPE_MAP.put("504b0304140006000800", "docx");//docx文件
        FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");//WPS文字wps、表格et、演示dps都是一樣的
        FILE_TYPE_MAP.put("6431303a637265617465", "torrent");
        FILE_TYPE_MAP.put("494d4b48010100000200", "264");


        FILE_TYPE_MAP.put("6D6F6F76", "mov"); //Quicktime (mov)
        FILE_TYPE_MAP.put("FF575043", "wpd"); //WordPerfect (wpd)
        FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx"); //Outlook Express (dbx)
        FILE_TYPE_MAP.put("2142444E", "pst"); //Outlook (pst)
        FILE_TYPE_MAP.put("AC9EBD8F", "qdf"); //Quicken (qdf)
        FILE_TYPE_MAP.put("E3828596", "pwl"); //Windows Password (pwl)
        FILE_TYPE_MAP.put("2E7261FD", "ram"); //Real Audio (ram)
    }
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章