-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
128 lines (112 loc) · 3.69 KB
/
app.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
console.time('完成,耗时');
var fs = require('fs');
var xor = 'a4a4a4a4a4'; //异或值(十六进制)
xor = hexToBin(xor);
var dataPath = './0c9a14c9d42da570d3ea13466be20608.dat'; //要解密的文件路径
var resPath = './res.'; //存放路径
var xorLen = 2;
//读取文件,获取到十六进制数据
fs.readFile(dataPath, { encoding: 'hex' }, function (err, data /** 加密后的十六进制数据*/) {
if (err) {
console.log(err);
} else {
var res = handleEncrypted(data, xor); //解密后的十六进制数据
var extension = getNameExtension(res.substring(0,4));
var hex = Buffer.from(res, 'hex'); //转为十六进制
fs.writeFile(resPath + extension, hex, function (err) {
if (err) {
console.log('出错:', err);
}
console.timeEnd('完成,耗时');
})
}
})
//解密加密数据
function handleEncrypted(strEncrypted) {
//先获取异或值(仅限于jpg文件)
// getXor(strEncrypted.substring(0, 4));
let strLength = strEncrypted.length;
var source = '';
var list = [];
for (var i = 0; i < strLength; i = i + xorLen) {
var str = strEncrypted.substring(0, xorLen);
strEncrypted = strEncrypted.substring(xorLen);
var res = getResult(str);
list.push(res);
}
source = list.join('');
return source;
}
//获取异或值
function getXor(str) {
xor = 'ffd8';
xor = getResult(str);
return;
}
//获取文件名后缀
function getNameExtension(hex) {
var str = hex.substring(0, 4);
var res = dataHead.find(function (item) {
return item.hex === hex;
}).name
return res;
}
//十六进制转二进制
function hexToBin(str) {
let hex_array = [{ hex: '0', bin: "0000" }, { hex: '1', bin: "0001" }, { hex: '2', bin: "0010" }, { hex: '3', bin: "0011" }, { hex: '4', bin: "0100" }, { hex: '5', bin: "0101" }, { hex: '6', bin: "0110" }, { hex: '7', bin: "0111" },
{ hex: '8', bin: "1000" }, { hex: '9', bin: "1001" }, { hex: 'a', bin: "1010" }, { hex: 'b', bin: "1011" }, { hex: 'c', bin: "1100" }, { hex: 'd', bin: "1101" }, { hex: 'e', bin: "1110" }, { hex: 'f', bin: "1111" }];
let value = "";
for (let i = 0; i < str.length; i++) {
value += hex_array.find(function (item) {
return item.hex == str[i];
}).bin;
}
return value;
}
//二进制转十六进制
function binToHex(str) {
let hex_array = [{ hex: '0', bin: "0000" }, { hex: '1', bin: "0001" }, { hex: '2', bin: "0010" }, { hex: '3', bin: "0011" }, { hex: '4', bin: "0100" }, { hex: '5', bin: "0101" }, { hex: '6', bin: "0110" }, { hex: '7', bin: "0111" },
{ hex: '8', bin: "1000" }, { hex: '9', bin: "1001" }, { hex: 'a', bin: "1010" }, { hex: 'b', bin: "1011" }, { hex: 'c', bin: "1100" }, { hex: 'd', bin: "1101" }, { hex: 'e', bin: "1110" }, { hex: 'f', bin: "1111" }];
let value = '';
let list = [];
while (str.length > 4) {
list.push(str.substring(0, 4));
str = str.substring(4);
}
list.push(str);
for (let i = 0; i < list.length; i++) {
value += hex_array.find(function (item) {
return item.bin == list[i];
}).hex;
}
return value;
}
//获取计算结果
function getResult(a) {
let A = hexToBin(a);
let B = xor;
let d = "";
for (let i = 0; i < A.length; i++) {
if (A[i] === B[i]) {
d = d.concat('0');
} else {
d = d.concat('1');
}
}
return binToHex(d);
}
//扩展名-十六进制表
var dataHead = [
{
name: 'jpg',
hex: 'ffd8'
},
{
name: 'png',
hex: '8950'
},
{
name: 'gif',
hex: '4749'
}
]