-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
114 lines (100 loc) · 3.69 KB
/
Copy pathapp.js
File metadata and controls
114 lines (100 loc) · 3.69 KB
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
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
_encryptionKey = null;
_directory = null;
// Get directory input from the user
async function getDirectoryFromUser() {
const answers = await inquirer.default.prompt([
{
type: 'input',
name: 'directory',
message: 'Please enter the directory path:',
validate: function (input) {
if (fs.existsSync(input) && fs.lstatSync(input).isDirectory()) {
return true;
}
return 'Invalid directory path or directory does not exist!';
}
}
]);
return answers.directory;
}
// Find encryptionKey from system.json
function findEncryptionKey(directory) {
_directory= directory;
const jsonFilePath = path.join(directory, 'data', 'system.json');
if (!fs.existsSync(jsonFilePath)) {
throw new Error('system.json file not found in the specified directory.');
}
try {
const jsonData = fs.readFileSync(jsonFilePath, 'utf-8');
const parsedData = JSON.parse(jsonData);
if (!parsedData.encryptionKey) {
throw new Error('encryptionKey not found in system.json.');
}
return parsedData.encryptionKey;
} catch (error) {
throw new Error('Error reading or parsing system.json: ' + error.message);
}
}
// Run the application
async function runApp() {
try {
const directory = await getDirectoryFromUser();
const encryptionKey = findEncryptionKey(directory);
console.log('Encryption key found:', encryptionKey);
_encryptionKey = encryptionKey;
extract();
} catch (error) {
console.error('Error:', error.message);
}
}
const Utils = {
decryptArrayBuffer: function (source) {
const header = new Uint8Array(source, 0, 16);
const headerHex = Array.from(header, x => x.toString(16)).join(",");
if (headerHex !== "52,50,47,4d,56,0,0,0,0,3,1,0,0,0,0,0") {
throw new Error("Decryption error");
}
const body = source.slice(16);
const view = new DataView(body);
const key = _encryptionKey.match(/.{2}/g);
for (let i = 0; i < 16; i++) {
view.setUint8(i, view.getUint8(i) ^ parseInt(key[i], 16));
}
return body;
}
};
async function processDirectory(inputDir, outputDir) {
const files = fs.readdirSync(inputDir);
for (const file of files) {
const fullPath = path.join(inputDir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const newOutputDir = path.join(outputDir, file);
fs.mkdirSync(newOutputDir, { recursive: true });
await processDirectory(fullPath, newOutputDir);
} else if (path.extname(file) === '.png_') {
try {
const encryptedData = fs.readFileSync(fullPath);
const decryptedData = Utils.decryptArrayBuffer(encryptedData.buffer);
const outputFile = path.join(outputDir, file.replace('.png_', '.png'));
fs.writeFileSync(outputFile, Buffer.from(decryptedData));
console.log(`Decrypted: ${outputFile}`);
} catch (error) {
console.error(`Failed to decrypt ${file}: ${error.message}`);
}
}else{
continue;
}
}
}
async function extract(){
const inputDir = path.resolve(path.join(_directory,'./img'));
const outputDir = path.resolve(path.join(_directory,'./output_img'));
fs.mkdirSync(outputDir, { recursive: true });
await processDirectory(inputDir, outputDir);
console.log('Decryption completed!');
}
runApp();