-
Notifications
You must be signed in to change notification settings - Fork 1
/
WechatUtil.java
78 lines (74 loc) · 2.35 KB
/
WechatUtil.java
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
package cn.xanderye.util;
import java.io.*;
/**
* @author XanderYe
* @description: 微信工具类
* @date 2021/5/18 15:08
*/
public class WechatUtil {
/**
* JPG开头二进制 FFD8
*/
private static final byte[] JPG_BYTES = new byte[]{-1, -40};
/**
* PNG开头二进制 8950
*/
private static final byte[] PNG_BYTES = new byte[]{-119, 80};
/**
* 解密微信图片
* @param inputPath
* @param outputPath
* @return void
* @author XanderYe
* @date 2021/5/18
*/
public static void decodeImage(String inputPath, String outputPath) throws IOException {
File datFile = new File(inputPath);
if (!datFile.exists()) {
throw new FileNotFoundException("dat文件不存在");
}
File dir = new File(outputPath);
if (!dir.isDirectory()) {
throw new IOException("outputPath请传入文件夹");
}
String fileName = datFile.getName().substring(0, datFile.getName().lastIndexOf("."));
OutputStream os = null;
try (InputStream is = new FileInputStream(datFile)) {
byte[] data = IoUtil.readBytes(is);
byte xorData = 0;
String suffix = null;
if ((byte) (data[0] ^ JPG_BYTES[0]) == (byte) (data[1] ^ JPG_BYTES[1])) {
xorData = (byte) (data[0] ^ JPG_BYTES[0]);
suffix = "jpg";
} else if ((byte) (data[0] ^ PNG_BYTES[0]) == (byte) (data[1] ^ PNG_BYTES[1])) {
xorData = (byte) (data[0] ^ PNG_BYTES[0]);
suffix = "png";
}
if (suffix == null) {
throw new IOException("解密失败");
}
File outputFile = new File(outputPath + File.separator + fileName + "." + suffix);
os = new FileOutputStream(outputFile);
decodeByte(data, xorData);
os.write(data);
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(os);
}
}
/**
* 异或处理图片数据
* @param data
* @param xorData
* @return void
* @author XanderYe
* @date 2021/5/18
*/
private static void decodeByte(byte[] data, byte xorData) {
for (int i = 0; i < data.length; i++) {
data[i] ^= xorData;
}
}
}