mirror of
https://github.com/JDM170/model_coder
synced 2025-12-10 05:57:19 +07:00
Release 'Full file coder and decoder'
Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
@@ -14,8 +14,7 @@ static inline bool is_base64(BYTE c) {
|
||||
std::string base64encode(BYTE const* buf, unsigned int bufLen) {
|
||||
std::string ret;
|
||||
int i = 0, j = 0;
|
||||
BYTE char_array_3[3];
|
||||
BYTE char_array_4[4];
|
||||
BYTE char_array_3[3], char_array_4[4];
|
||||
|
||||
while(bufLen--) {
|
||||
char_array_3[i++] = *(buf++);
|
||||
@@ -40,7 +39,7 @@ std::string base64encode(BYTE const* buf, unsigned int bufLen) {
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for(j = 0; j < i + 1; j++)
|
||||
for(j = 0; j < (i + 1); j++)
|
||||
ret += base64_chars[char_array_4[j]];
|
||||
|
||||
while(i++ < 3)
|
||||
@@ -69,7 +68,7 @@ std::vector<BYTE> base64decode(std::string const& encoded_string) {
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for(i = 0; (i < 3); i++)
|
||||
for(i = 0; i < 3; i++)
|
||||
ret.push_back(char_array_3[i]);
|
||||
i = 0;
|
||||
}
|
||||
@@ -86,7 +85,7 @@ std::vector<BYTE> base64decode(std::string const& encoded_string) {
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for(j = 0; j < i - 1; j++)
|
||||
for(j = 0; j < (i - 1); j++)
|
||||
ret.push_back(char_array_3[j]);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <string>
|
||||
typedef unsigned char BYTE;
|
||||
|
||||
std::string base64encode(BYTE const* buf, unsigned int bufLen);
|
||||
std::string base64encode(BYTE const*, unsigned int);
|
||||
std::vector<BYTE> base64decode(std::string const&);
|
||||
|
||||
#endif
|
||||
|
||||
44
coder.cpp
44
coder.cpp
@@ -10,23 +10,18 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool encodeFile(const string fpath, const string key) {
|
||||
// Key conversion
|
||||
unsigned int k[4];
|
||||
keyConversion(key, k);
|
||||
mLockedCout("[OUTPUT] Key converted\n");
|
||||
|
||||
bool encodeFile(const string fpath, const unsigned int* key) {
|
||||
// Reading file
|
||||
ifstream file(fpath, ios::in | ios::binary);
|
||||
struct stat results;
|
||||
if(stat(fpath.c_str(), &results) != 0) {
|
||||
mLockedCout("[ERROR] File '" + fpath + "' not found\n");
|
||||
return false;
|
||||
}
|
||||
ifstream ifile(fpath, ios::in | ios::binary);
|
||||
size_t file_size = results.st_size;
|
||||
char* fbuffer = new char[file_size];
|
||||
file.read(fbuffer, file_size);
|
||||
file.close();
|
||||
ifile.read(fbuffer, file_size);
|
||||
ifile.close();
|
||||
mLockedCout("[OUTPUT] File '" + fpath + "' has been read\n");
|
||||
|
||||
// Creating buffer
|
||||
@@ -40,19 +35,19 @@ bool encodeFile(const string fpath, const string key) {
|
||||
// TEA encoding
|
||||
size_t obuffer_size = vbuffer_size + 4;
|
||||
char* obuffer = new char[obuffer_size];
|
||||
unsigned int v[2];
|
||||
memset(v, 0, sizeof(v));
|
||||
unsigned int value[2];
|
||||
memset(value, 0, sizeof(value));
|
||||
for(unsigned int i = 0; i < file_size; i += 4) {
|
||||
v[0] = *(unsigned int*)&vbuffer[i];
|
||||
encode(&v[0], &k[0]);
|
||||
memcpy(&obuffer[i], &v[0], 4);
|
||||
value[0] = *(unsigned int*)&vbuffer[i];
|
||||
encode(&value[0], &key[0]);
|
||||
memcpy(&obuffer[i], &value[0], 4);
|
||||
memcpy(vbuffer, fbuffer, file_size);
|
||||
}
|
||||
memcpy(&obuffer[obuffer_size - 4], &v[1], 4);
|
||||
memcpy(&obuffer[vbuffer_size], &value[1], 4);
|
||||
mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-coded\n");
|
||||
|
||||
// Base64 encoding
|
||||
string ob64 = base64encode((unsigned char*)obuffer, obuffer_size);
|
||||
string obuffer64 = base64encode((unsigned char*)obuffer, obuffer_size);
|
||||
mLockedCout("[OUTPUT] File '" + fpath + "' has been Base64-coded\n");
|
||||
|
||||
// Filename generating
|
||||
@@ -64,7 +59,7 @@ bool encodeFile(const string fpath, const string key) {
|
||||
ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
|
||||
if(ofile.good())
|
||||
ofile.clear();
|
||||
ofile << ob64;
|
||||
ofile << obuffer64;
|
||||
ofile.close();
|
||||
mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n");
|
||||
|
||||
@@ -86,17 +81,22 @@ int main(const int argc, const char* argv[]) {
|
||||
}
|
||||
|
||||
// Reading key
|
||||
string key;
|
||||
string skey;
|
||||
cout << "[INPUT] Enter key: ";
|
||||
cin >> key;
|
||||
cin >> skey;
|
||||
cout << endl;
|
||||
|
||||
// Key conversion
|
||||
unsigned int bkey[4];
|
||||
keyConversion(skey, bkey);
|
||||
cout << "[OUTPUT] Key converted" << endl;
|
||||
|
||||
thread** threads;
|
||||
if(argc > 1) {
|
||||
threads = new thread*[argc - 1];
|
||||
for(int i = 0; i < argc - 1; i++) {
|
||||
string tfpath(argv[i + 1]);
|
||||
thread* t = new thread(encodeFile, tfpath, key);
|
||||
thread* t = new thread(encodeFile, tfpath, bkey);
|
||||
threads[i] = t;
|
||||
}
|
||||
for(int i = 0; i < argc - 1; i++) {
|
||||
@@ -104,12 +104,12 @@ int main(const int argc, const char* argv[]) {
|
||||
}
|
||||
} else {
|
||||
threads = new thread*[1];
|
||||
thread* t = new thread(encodeFile, fpath, key);
|
||||
thread* t = new thread(encodeFile, fpath, bkey);
|
||||
threads[0] = t;
|
||||
t->join();
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < argc - 1; i++) {
|
||||
for(int i = 0; i < argc - 1; i++) {
|
||||
delete threads[i];
|
||||
}
|
||||
delete[] threads;
|
||||
|
||||
54
decoder.cpp
54
decoder.cpp
@@ -9,19 +9,14 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool decodeFile(const string fpath, const string key) {
|
||||
// Key conversion
|
||||
unsigned int k[4];
|
||||
keyConversion(key, k);
|
||||
mLockedCout("[OUTPUT] Key converted\n");
|
||||
|
||||
bool decodeFile(const string fpath, const unsigned int* key) {
|
||||
// Reading file
|
||||
ifstream ifile(fpath, ios::in | ios::binary);
|
||||
struct stat results;
|
||||
if(stat(fpath.c_str(), &results) != 0) {
|
||||
mLockedCout("[ERROR] File '" + fpath + "' not found\n");
|
||||
return false;
|
||||
}
|
||||
ifstream ifile(fpath, ios::in | ios::binary);
|
||||
string fbuffer64;
|
||||
ifile >> fbuffer64;
|
||||
ifile.close();
|
||||
@@ -33,17 +28,16 @@ bool decodeFile(const string fpath, const string key) {
|
||||
mLockedCout("[OUTPUT] File '" + fpath + "' has been Base64-decoded\n");
|
||||
|
||||
// TEA decoding
|
||||
//unsigned char* vbuffer = new unsigned char[fbuffer_size];
|
||||
//memset(vbuffer, 0, fbuffer_size);
|
||||
unsigned int v[2];
|
||||
memset(v, 0, sizeof(v));
|
||||
memcpy(&v[1], &fbuffer[fbuffer_size], 4);
|
||||
for(unsigned int i = fbuffer_size; i >= 0; i--) {
|
||||
v[0] = *(unsigned int*)&fbuffer[i];
|
||||
decode(&v[0], &k[0]);
|
||||
memcpy(&fbuffer[i], &v[0], 4);
|
||||
unsigned char* vbuffer = new unsigned char[fbuffer_size];
|
||||
memset(vbuffer, 0, fbuffer_size);
|
||||
unsigned int value[2];
|
||||
memset(value, 0, sizeof(value));
|
||||
memcpy(&value[1], &fbuffer[fbuffer_size - 4], 4);
|
||||
for(int i = fbuffer_size - 8; i >= 0; i -= 4) {
|
||||
value[0] = *(unsigned int*)&fbuffer[i];
|
||||
decode(&value[0], &key[0]);
|
||||
memcpy(&vbuffer[i], &value[0], 4);
|
||||
}
|
||||
memcpy(&fbuffer[fbuffer_size - 4], &v[1], 4);
|
||||
mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-decoded\n");
|
||||
|
||||
// Filename generating
|
||||
@@ -52,14 +46,15 @@ bool decodeFile(const string fpath, const string key) {
|
||||
string fullpath = filefolder + old_filename + ".dec";
|
||||
|
||||
// Writing file
|
||||
/*ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
|
||||
ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
|
||||
if(ofile.good())
|
||||
ofile.clear();
|
||||
ofile.write((char*)&vbuffer, fbuffer_size);
|
||||
ofile.close();*/
|
||||
for(unsigned int i = 0; i < fbuffer_size; i++)
|
||||
ofile << vbuffer[i];
|
||||
ofile.close();
|
||||
mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n");
|
||||
|
||||
//delete[] vbuffer;
|
||||
delete[] vbuffer;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -75,17 +70,22 @@ int main(const int argc, const char* argv[]) {
|
||||
}
|
||||
|
||||
// Reading key
|
||||
string key;
|
||||
string skey;
|
||||
cout << "[INPUT] Enter key: ";
|
||||
cin >> key;
|
||||
cin >> skey;
|
||||
cout << endl;
|
||||
|
||||
// Key conversion
|
||||
unsigned int bkey[4];
|
||||
keyConversion(skey, bkey);
|
||||
cout << "[OUTPUT] Key converted" << endl;
|
||||
|
||||
thread** threads;
|
||||
if(argc > 1) {
|
||||
threads = new thread*[argc - 1];
|
||||
for(int i = 0; i < argc - 1; i++) {
|
||||
string tfpath(argv[i + 1]);
|
||||
thread* t = new thread(decodeFile, tfpath, key);
|
||||
thread* t = new thread(decodeFile, tfpath, bkey);
|
||||
threads[i] = t;
|
||||
}
|
||||
for(int i = 0; i < argc - 1; i++) {
|
||||
@@ -93,18 +93,18 @@ int main(const int argc, const char* argv[]) {
|
||||
}
|
||||
} else {
|
||||
threads = new thread*[1];
|
||||
thread* t = new thread(decodeFile, fpath, key);
|
||||
thread* t = new thread(decodeFile, fpath, bkey);
|
||||
threads[0] = t;
|
||||
t->join();
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < argc - 1; i++) {
|
||||
for(int i = 0; i < argc - 1; i++) {
|
||||
delete threads[i];
|
||||
}
|
||||
delete[] threads;
|
||||
|
||||
cout << "[OUTPUT] Selected files are decrypted, press any key to close program." << endl;
|
||||
//getch();
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="model_coder" />
|
||||
<Option title="full_model_coder" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef TEA_ENCODE
|
||||
#define TEA_ENCODE
|
||||
|
||||
void encode(unsigned int* v, const unsigned int* k);
|
||||
void decode(unsigned int* v, const unsigned int* k);
|
||||
void encode(unsigned int*, const unsigned int*);
|
||||
void decode(unsigned int*, const unsigned int*);
|
||||
|
||||
#endif
|
||||
|
||||
22
test/full_file_decode.lua
Normal file
22
test/full_file_decode.lua
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
local key = "1234" -- Model key
|
||||
local fileName = "full_file_encode" -- File name
|
||||
|
||||
-- Reading crypted file
|
||||
local file = fileOpen(fileName .. ".enc")
|
||||
local fileData = fileRead(file, fileGetSize(file))
|
||||
fileClose(file)
|
||||
|
||||
-- Decoding file data
|
||||
local encodedKey = string.sub(utf8.lower(md5(key)), 1, 16)
|
||||
local decoded = teaDecode(fileData, encodedKey)
|
||||
|
||||
-- Writing uncrypted data to clean file
|
||||
file = fileCreate(fileName .. ".dec")
|
||||
fileWrite(file, decoded)
|
||||
fileClose(file)
|
||||
|
||||
encodedKey = nil
|
||||
fileData = nil
|
||||
decoded = nil
|
||||
file = nil
|
||||
@@ -1,20 +0,0 @@
|
||||
|
||||
local key = "JdzFR2XLDaBtpGGD" -- Model key
|
||||
local fileName = "test.txd" -- File name
|
||||
|
||||
-- Opening crypted file
|
||||
local file = fileOpen(fileName..".enc")
|
||||
local fileData = fileRead(file, fileGetSize(file))
|
||||
fileClose(file)
|
||||
|
||||
-- Decoding file data
|
||||
local decoded = teaDecode(fileData, string.sub(utf8.lower(md5(key)), 1, 16))
|
||||
fileData = nil
|
||||
|
||||
-- Writing uncrypted data to clean file
|
||||
file = fileCreate(fileName..".dec")
|
||||
fileWrite(file, decoded)
|
||||
fileClose(file)
|
||||
|
||||
decoded = nil
|
||||
file = nil
|
||||
Reference in New Issue
Block a user