File structure changes, trying to release decoder

Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2020-09-16 16:24:53 +07:00
parent 7ed712ed97
commit 8f9c05f2cc
7 changed files with 203 additions and 62 deletions

View File

@@ -5,55 +5,29 @@
#include <mutex>
#include <conio.h>
#include "base64/base64.h"
#include "md5/md5.h"
#include "tea/tea.h"
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
#include "util.h"
using namespace std;
mutex mu;
string getFileName(const string& s, string* pathwithoutname) {
char separator = '/';
#ifdef _WIN32
separator = '\\';
#endif
size_t i = s.rfind(separator, s.length());
if(i != string::npos) {
pathwithoutname->clear();
pathwithoutname->append(s.substr(0, i + 1));
return s.substr(i + 1, s.length() - i);
}
return s;
}
string encodeKey(const string key) {
return md5(key).substr(0, 16);
}
bool encodeFile(const string fpath, const string key) {
// Key conversion
unsigned int k[4];
unsigned int kbuffer[4];
memset(k, 0, sizeof(k));
memset(kbuffer, 0, sizeof(kbuffer));
memcpy(kbuffer, key.c_str(), MIN(key.length(), 16));
for(int i = 0; i < 4; i++)
k[i] = kbuffer[i];
mu.lock(); cout << "[OUTPUT] Key converted" << endl; mu.unlock();
keyConversion(key, k);
mLockedCout("[OUTPUT] Key converted\n");
// Reading file
ifstream file(fpath, ios::in | ios::binary);
struct stat results;
if(stat(fpath.c_str(), &results) != 0) {
mu.lock(); cout << "[ERROR] File '" << fpath << "' not found" << endl; mu.unlock();
mLockedCout("[ERROR] File '" + fpath + "' not found\n");
return false;
}
size_t file_size = results.st_size;
char* fbuffer = new char[file_size];
file.read(fbuffer, file_size);
file.close();
mu.lock(); cout << "[OUTPUT] File '" << fpath << "' has been read" << endl; mu.unlock();
mLockedCout("[OUTPUT] File '" + fpath + "' has been read\n");
// Creating buffer
size_t vbuffer_size = file_size;
@@ -63,7 +37,7 @@ bool encodeFile(const string fpath, const string key) {
memset(vbuffer, 0, vbuffer_size);
memcpy(vbuffer, fbuffer, file_size);
// Crypting file
// TEA encoding
size_t obuffer_size = vbuffer_size + 4;
char* obuffer = new char[obuffer_size];
unsigned int v[2];
@@ -75,13 +49,11 @@ bool encodeFile(const string fpath, const string key) {
memcpy(vbuffer, fbuffer, file_size);
}
memcpy(&obuffer[obuffer_size - 4], &v[1], 4);
delete[] fbuffer;
delete[] vbuffer;
mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-coded\n");
// Base64 encoding
string ob64 = base64encode((unsigned char*)obuffer, obuffer_size);
delete[] obuffer;
mu.lock(); cout << "[OUTPUT] File '" << fpath << "' has been crypted" << endl; mu.unlock();
mLockedCout("[OUTPUT] File '" + fpath + "' has been Base64-coded\n");
// Filename generating
string filefolder;
@@ -94,7 +66,11 @@ bool encodeFile(const string fpath, const string key) {
ofile.clear();
ofile << ob64;
ofile.close();
mu.lock(); cout << "[OUTPUT] File '" << fullpath << "' has been written" << endl; mu.unlock();
mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n");
delete[] fbuffer;
delete[] vbuffer;
delete[] obuffer;
return true;
}
@@ -105,11 +81,7 @@ int main(const int argc, const char* argv[]) {
if(argc > 1) {
fpath.assign(argv[1]);
} else {
mu.lock();
cout << "[OUTPUT] You can open file(s) with this program" << endl
<< "[OUTPUT] Or drag'n'drop on it" << endl
<< "[INPUT] Enter filename (without spaces): ";
mu.unlock();
mLockedCout("[OUTPUT] You can open file(s) with this program\n[OUTPUT] Or drag'n'drop on it\n[INPUT] Enter filename (without spaces): ");
cin >> fpath;
}
@@ -118,7 +90,6 @@ int main(const int argc, const char* argv[]) {
cout << "[INPUT] Enter key: ";
cin >> key;
cout << endl;
key = encodeKey(key);
thread** threads;
if(argc > 1) {
@@ -145,5 +116,6 @@ int main(const int argc, const char* argv[]) {
cout << "[OUTPUT] Selected files are encrypted, press any key to close program." << endl;
getch();
return 0;
}

110
decoder.cpp Normal file
View File

@@ -0,0 +1,110 @@
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <thread>
#include <conio.h>
#include "base64/base64.h"
#include "tea/tea.h"
#include "util.h"
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");
// 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;
}
string fbuffer64;
ifile >> fbuffer64;
ifile.close();
mLockedCout("[OUTPUT] File '" + fpath + "' has been read\n");
// Base64 decoding
vector<unsigned char> fbuffer = base64decode(fbuffer64);
size_t fbuffer_size = fbuffer.size();
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);
}
memcpy(&fbuffer[fbuffer_size - 4], &v[1], 4);
mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-decoded\n");
// Filename generating
string filefolder;
string old_filename = getFileName(fpath, &filefolder);
string fullpath = filefolder + old_filename + ".dec";
// Writing file
/*ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
if(ofile.good())
ofile.clear();
ofile.write((char*)&vbuffer, fbuffer_size);
ofile.close();*/
mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n");
//delete[] vbuffer;
return true;
}
int main(const int argc, const char* argv[]) {
// Checking argc and file path
string fpath;
if(argc > 1) {
fpath.assign(argv[1]);
} else {
mLockedCout("[OUTPUT] You can open file(s) with this program\n[OUTPUT] Or drag'n'drop on it\n[INPUT] Enter filename (without spaces): ");
cin >> fpath;
}
// Reading key
string key;
cout << "[INPUT] Enter key: ";
cin >> key;
cout << 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);
threads[i] = t;
}
for(int i = 0; i < argc - 1; i++) {
threads[i]->join();
}
} else {
threads = new thread*[1];
thread* t = new thread(decodeFile, fpath, key);
threads[0] = t;
t->join();
}
for(size_t 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();
return 0;
}

View File

@@ -6,22 +6,27 @@
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/model_coder" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/model_coder" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Target title="coder">
<Option output="bin/coder/model_coder" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/coder/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add option="-std=c++11" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
<Target title="decoder">
<Option output="bin/decoder/model_decoder" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/decoder/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add option="-std=c++11" />
</Compiler>
<Linker>
<Add option="-s" />
@@ -30,14 +35,22 @@
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-std=c++11" />
</Compiler>
<Unit filename="base64/base64.cpp" />
<Unit filename="base64/base64.h" />
<Unit filename="main.cpp" />
<Unit filename="coder.cpp">
<Option target="coder" />
</Unit>
<Unit filename="decoder.cpp">
<Option target="decoder" />
</Unit>
<Unit filename="md5/md5.cpp" />
<Unit filename="md5/md5.h" />
<Unit filename="tea/tea.cpp" />
<Unit filename="tea/tea.h" />
<Unit filename="util.cpp" />
<Unit filename="util.h" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View File

@@ -1,7 +1,7 @@
#include "tea.h"
const unsigned int delta = 0x9E3779B9;
const unsigned int sum_int = 0xC6EF3720;
const unsigned int c_delta = 0x9E3779B9;
const unsigned int c_sum = 0xC6EF3720;
void encode(unsigned int* v, const unsigned int* k) {
unsigned int v0 = v[0],
@@ -10,7 +10,7 @@ void encode(unsigned int* v, const unsigned int* k) {
sum = 0;
for(i = 0; i < 32; i++) {
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += delta;
sum += c_delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
}
v[0] = v0;
@@ -21,10 +21,10 @@ void decode(unsigned int* v, const unsigned int* k) {
unsigned int v0 = v[0],
v1 = v[1],
i,
sum = sum_int;
sum = c_sum;
for(i = 0; i < 32; i++) {
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
sum -= delta;
sum -= c_delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
}
v[0] = v0;

File diff suppressed because one or more lines are too long

34
util.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include "util.h"
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
std::mutex mu;
void mLockedCout(const std::string str) {
mu.lock();
std::cout << str;
mu.unlock();
}
std::string getFileName(const std::string& s, std::string* pathwithoutname) {
char separator = '/';
#ifdef _WIN32
separator = '\\';
#endif
size_t i = s.rfind(separator, s.length());
if(i != std::string::npos) {
pathwithoutname->clear();
pathwithoutname->append(s.substr(0, i + 1));
return s.substr(i + 1, s.length() - i);
}
return s;
}
void keyConversion(const std::string key, unsigned int* k) {
std::string encoded_key = md5(key).substr(0, 16); // MD5 encoding
unsigned int kbuffer[4];
memset(kbuffer, 0, sizeof(kbuffer));
memcpy(kbuffer, encoded_key.c_str(), MIN(encoded_key.length(), 16));
for(int i = 0; i < 4; i++)
k[i] = kbuffer[i];
}

12
util.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <mutex>
#include "md5/md5.h"
void mLockedCout(const std::string);
void keyConversion(const std::string, unsigned int*);
std::string getFileName(const std::string&, std::string*);
#endif // UTIL_H