首页 > 技术文章 > 关于异或值怎么计算

liyanyan665 2019-09-12 17:28 原文

首先使用十六进制器打开微信dat文件,显示如下

jpg图片文件头一般为FF D8 开头的,所以此处使用科学计算器,计算异或值


计算后的值

所以此处异或值就是0x9D

代码
以下是java代码,创建一个weChatImgRevert .class后复制进去就好啦。
此处的jdk版本需要1.8以上…,另外三个参数需要改成自己的哦~

package main.java.com.example.demo;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class weChatImgRevert {

public static void main(String[] args) {
String path = "C:\\Users\\Administrator\\Documents\\WeChat Files\\xxx\\FileStorage\\Image\\2019-07";
String targetPath = "D:\\weChat\\2019-07\\";
int xor = 0xCB;
convert(path, targetPath, xor);
}

/**
* @param path 图片地址
* @param targetPath 转换后目录
*/
private static void convert(String path, String targetPath, int xor) {
File[] file = new File(path).listFiles();
if (file == null) {
return;
}
int size = file.length;
System.out.println("总共" + size + "个文件");
AtomicReference<Integer> integer = new AtomicReference<>(0);
Arrays.stream(file).parallel().forEach(file1 -> {
try (InputStream reader = new FileInputStream(file1);
OutputStream writer =
new FileOutputStream(targetPath + file1.getName().split("\\.")[0] + ".jpg")) {
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] ^ xor);
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),进度:" + integer.get() +
"/" + size);
} catch (Exception e) {
e.printStackTrace();
}
});
System.out.println("解析完毕!");
}

/**
* 获取异或值,不一定准确,当解析不出来的时候,换一张图片的异或值来解析
*
* @param PhotoPath
* @return
*/
private static int getXor(String PhotoPath) {
File file = new File(PhotoPath);
try (InputStream reader = new FileInputStream(file)) {
int[] xors = new int[4];
xors[0] = reader.read() & 0xFF ^ 0xFF;
xors[1] = reader.read() & 0xFF ^ 0xD8;
reader.skip(file.length() - 1);
xors[2] = reader.read() & 0xFF ^ 0xFF;
xors[3] = reader.read() & 0xFF ^ 0xD9;
Map<Integer, Integer> map = new HashMap<>();
for (int xor : xors) {
if (map.containsKey(xor)) {
map.put(xor, map.get(xor) + 1);
} else {
map.put(xor, 1);
}
}
return map.values().stream().max(Integer::compareTo).get();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
执行main方法后就可以在目标文件夹中去看转换后的图片了
以下是转换后的效果图片:

推荐阅读