-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (112 loc) · 3.94 KB
/
Copy pathmain.cpp
File metadata and controls
156 lines (112 loc) · 3.94 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <fstream>
#include <filesystem>
#include <format>
#include "include/initkey.h"
#include "include/decenc.h"
#include "include/strangefunc1.h"
#include "include/md5.h"
namespace fs = std::filesystem;
char* SomeKey = nullptr;
const char* MAGIC_KEY = "8FE9D249BD2689BB4B70F5AE88A9E645";
int main(int argc, char** argv) {
if (argc != 3) {
puts("Usage: ");
puts("./main.exe dec [path_to_epk] ");
puts("./main.exe enc [path_to_dec_epk] ");
exit(-1);
}
bool is_dec = true;
if (strcmp(argv[1], "enc") == 0) {
is_dec = false;
}
fs::path p(argv[2]);
auto base = fs::absolute(argv[0]).parent_path();
std::string filename = p.filename().string();
if (is_dec && p.extension() != ".epk") {
std::cout << "[dec] input file should be .epk";
exit(-1);
}
if (!is_dec && p.extension() != ".epk_dec") {
std::cout << "[enc] input file should be .epk_dec";
exit(-1);
}
std::string real_filename;
for (int i = filename.length() - 1; i >= 0; --i) {
if (filename[i] == '#') {
real_filename = filename.substr(i+1, std::string::npos);
break;
}
}
if (real_filename.empty()) {
real_filename = filename;
}
std::string key = fs::path(real_filename).filename().stem().string();
int index = key.length();
std::cout << std::format("filename: {}\nreal_filename: {}\nkey: {}, len: {}\n", filename, real_filename, key, index);
std::fstream key_file(base / "SomeKey.bin", std::ios::binary | std::ios::in);
key_file.seekg(0,std::ios_base::end);
auto key_size = key_file.tellg();
SomeKey = new char[key_size * 2];
memset(SomeKey, 0, key_size * sizeof(char));
key_file.seekg(0,std::ios_base::beg);
key_file.read(SomeKey, key_size);
std::fstream file(p, std::ios::binary | std::ios::in);
file.seekg(0,std::ios_base::end);
size_t size = file.tellg();
uint8_t* buf = new uint8_t[size*2];
memset(buf, 0, size * sizeof(char) * 2);
file.seekg(0,std::ios_base::beg);
file.read((char*)buf, size);
auto* a1 = new char[0x1000];
std::cout << "start decrypt" << std::endl;
sub_1404C80B0((_DWORD*)a1, (__int64)key.c_str(), (int)index);
// NOTE(kuriko):
/*
struct EPK, see in readme
*/
if (is_dec) {
// NOTE(kuriko): this is a trick to avoid boundary problem
// first we decode the whole buffer, then we shrink it to size specific in epk
printf("file size: %x\n", size);
auto real_size = (buf[size-0x20+0] << 24)
| (buf[size-0x20+1] << 16)
| (buf[size-0x20+2] << 8)
| buf[size-0x20+3];
DecEncEPK(a1, (char*)buf, size - 0x30, dec_func);
size = real_size;
printf("raw size: %x\n", size);
} else {
printf("raw size: %x\n", size);
// Round up
uint64_t raw_size = size;
if (size % 4 != 0) {
size = (size + 3) / 4 * 4;
}
uint64_t padding_size = size;
printf("padding size: %x\n", padding_size);
DecEncEPK(a1, (char*)buf, padding_size, enc_func);
size += 0x10;
buf[size + 0] = BYTE3(raw_size);
buf[size + 1] = BYTE2(raw_size);
buf[size + 2] = BYTE1(raw_size);
buf[size + 3] = BYTE0(raw_size);
size += 0x10;
Chocobo1::MD5 md5;
md5.addData(buf, padding_size);
md5.addData(MAGIC_KEY, strlen(MAGIC_KEY) * sizeof(char));
md5.finalize();
auto md5_data = md5.toArray();
memcpy(buf + size, md5_data.data(), 0x10);
size += 0x10;
}
auto out_p = p.replace_extension(is_dec ? ".epk_dec" : ".epk_enc");
std::cout << "output: " << out_p << std::endl;
std::fstream fout(out_p, std::ios::binary | std::ios::out);
fout.write((char*)buf, size);
std::cout << "done" << std::endl;
return 0;
}