Release 'Full file coder and decoder'

Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2020-09-17 22:03:21 +07:00
parent 8f9c05f2cc
commit 489e783380
10 changed files with 79 additions and 78 deletions

View File

@@ -14,8 +14,7 @@ static inline bool is_base64(BYTE c) {
std::string base64encode(BYTE const* buf, unsigned int bufLen) { std::string base64encode(BYTE const* buf, unsigned int bufLen) {
std::string ret; std::string ret;
int i = 0, j = 0; int i = 0, j = 0;
BYTE char_array_3[3]; BYTE char_array_3[3], char_array_4[4];
BYTE char_array_4[4];
while(bufLen--) { while(bufLen--) {
char_array_3[i++] = *(buf++); 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[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f; 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]]; ret += base64_chars[char_array_4[j]];
while(i++ < 3) 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[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]; 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]); ret.push_back(char_array_3[i]);
i = 0; 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[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]; 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]); ret.push_back(char_array_3[j]);
} }

View File

@@ -5,7 +5,7 @@
#include <string> #include <string>
typedef unsigned char BYTE; 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&); std::vector<BYTE> base64decode(std::string const&);
#endif #endif

View File

@@ -10,23 +10,18 @@
using namespace std; using namespace std;
bool encodeFile(const string fpath, const string key) { bool encodeFile(const string fpath, const unsigned int* key) {
// Key conversion
unsigned int k[4];
keyConversion(key, k);
mLockedCout("[OUTPUT] Key converted\n");
// Reading file // Reading file
ifstream file(fpath, ios::in | ios::binary);
struct stat results; struct stat results;
if(stat(fpath.c_str(), &results) != 0) { if(stat(fpath.c_str(), &results) != 0) {
mLockedCout("[ERROR] File '" + fpath + "' not found\n"); mLockedCout("[ERROR] File '" + fpath + "' not found\n");
return false; return false;
} }
ifstream ifile(fpath, ios::in | ios::binary);
size_t file_size = results.st_size; size_t file_size = results.st_size;
char* fbuffer = new char[file_size]; char* fbuffer = new char[file_size];
file.read(fbuffer, file_size); ifile.read(fbuffer, file_size);
file.close(); ifile.close();
mLockedCout("[OUTPUT] File '" + fpath + "' has been read\n"); mLockedCout("[OUTPUT] File '" + fpath + "' has been read\n");
// Creating buffer // Creating buffer
@@ -40,19 +35,19 @@ bool encodeFile(const string fpath, const string key) {
// TEA encoding // TEA encoding
size_t obuffer_size = vbuffer_size + 4; size_t obuffer_size = vbuffer_size + 4;
char* obuffer = new char[obuffer_size]; char* obuffer = new char[obuffer_size];
unsigned int v[2]; unsigned int value[2];
memset(v, 0, sizeof(v)); memset(value, 0, sizeof(value));
for(unsigned int i = 0; i < file_size; i += 4) { for(unsigned int i = 0; i < file_size; i += 4) {
v[0] = *(unsigned int*)&vbuffer[i]; value[0] = *(unsigned int*)&vbuffer[i];
encode(&v[0], &k[0]); encode(&value[0], &key[0]);
memcpy(&obuffer[i], &v[0], 4); memcpy(&obuffer[i], &value[0], 4);
memcpy(vbuffer, fbuffer, file_size); 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"); mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-coded\n");
// Base64 encoding // 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"); mLockedCout("[OUTPUT] File '" + fpath + "' has been Base64-coded\n");
// Filename generating // Filename generating
@@ -64,7 +59,7 @@ bool encodeFile(const string fpath, const string key) {
ofstream ofile(fullpath.c_str(), ios::out | ios::binary); ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
if(ofile.good()) if(ofile.good())
ofile.clear(); ofile.clear();
ofile << ob64; ofile << obuffer64;
ofile.close(); ofile.close();
mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n"); mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n");
@@ -86,17 +81,22 @@ int main(const int argc, const char* argv[]) {
} }
// Reading key // Reading key
string key; string skey;
cout << "[INPUT] Enter key: "; cout << "[INPUT] Enter key: ";
cin >> key; cin >> skey;
cout << endl; cout << endl;
// Key conversion
unsigned int bkey[4];
keyConversion(skey, bkey);
cout << "[OUTPUT] Key converted" << endl;
thread** threads; thread** threads;
if(argc > 1) { if(argc > 1) {
threads = new thread*[argc - 1]; threads = new thread*[argc - 1];
for(int i = 0; i < argc - 1; i++) { for(int i = 0; i < argc - 1; i++) {
string tfpath(argv[i + 1]); string tfpath(argv[i + 1]);
thread* t = new thread(encodeFile, tfpath, key); thread* t = new thread(encodeFile, tfpath, bkey);
threads[i] = t; threads[i] = t;
} }
for(int i = 0; i < argc - 1; i++) { for(int i = 0; i < argc - 1; i++) {
@@ -104,12 +104,12 @@ int main(const int argc, const char* argv[]) {
} }
} else { } else {
threads = new thread*[1]; threads = new thread*[1];
thread* t = new thread(encodeFile, fpath, key); thread* t = new thread(encodeFile, fpath, bkey);
threads[0] = t; threads[0] = t;
t->join(); t->join();
} }
for(size_t i = 0; i < argc - 1; i++) { for(int i = 0; i < argc - 1; i++) {
delete threads[i]; delete threads[i];
} }
delete[] threads; delete[] threads;

View File

@@ -9,19 +9,14 @@
using namespace std; using namespace std;
bool decodeFile(const string fpath, const string key) { bool decodeFile(const string fpath, const unsigned int* key) {
// Key conversion
unsigned int k[4];
keyConversion(key, k);
mLockedCout("[OUTPUT] Key converted\n");
// Reading file // Reading file
ifstream ifile(fpath, ios::in | ios::binary);
struct stat results; struct stat results;
if(stat(fpath.c_str(), &results) != 0) { if(stat(fpath.c_str(), &results) != 0) {
mLockedCout("[ERROR] File '" + fpath + "' not found\n"); mLockedCout("[ERROR] File '" + fpath + "' not found\n");
return false; return false;
} }
ifstream ifile(fpath, ios::in | ios::binary);
string fbuffer64; string fbuffer64;
ifile >> fbuffer64; ifile >> fbuffer64;
ifile.close(); ifile.close();
@@ -33,17 +28,16 @@ bool decodeFile(const string fpath, const string key) {
mLockedCout("[OUTPUT] File '" + fpath + "' has been Base64-decoded\n"); mLockedCout("[OUTPUT] File '" + fpath + "' has been Base64-decoded\n");
// TEA decoding // TEA decoding
//unsigned char* vbuffer = new unsigned char[fbuffer_size]; unsigned char* vbuffer = new unsigned char[fbuffer_size];
//memset(vbuffer, 0, fbuffer_size); memset(vbuffer, 0, fbuffer_size);
unsigned int v[2]; unsigned int value[2];
memset(v, 0, sizeof(v)); memset(value, 0, sizeof(value));
memcpy(&v[1], &fbuffer[fbuffer_size], 4); memcpy(&value[1], &fbuffer[fbuffer_size - 4], 4);
for(unsigned int i = fbuffer_size; i >= 0; i--) { for(int i = fbuffer_size - 8; i >= 0; i -= 4) {
v[0] = *(unsigned int*)&fbuffer[i]; value[0] = *(unsigned int*)&fbuffer[i];
decode(&v[0], &k[0]); decode(&value[0], &key[0]);
memcpy(&fbuffer[i], &v[0], 4); memcpy(&vbuffer[i], &value[0], 4);
} }
memcpy(&fbuffer[fbuffer_size - 4], &v[1], 4);
mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-decoded\n"); mLockedCout("[OUTPUT] File '" + fpath + "' has been TEA-decoded\n");
// Filename generating // Filename generating
@@ -52,14 +46,15 @@ bool decodeFile(const string fpath, const string key) {
string fullpath = filefolder + old_filename + ".dec"; string fullpath = filefolder + old_filename + ".dec";
// Writing file // Writing file
/*ofstream ofile(fullpath.c_str(), ios::out | ios::binary); ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
if(ofile.good()) if(ofile.good())
ofile.clear(); ofile.clear();
ofile.write((char*)&vbuffer, fbuffer_size); for(unsigned int i = 0; i < fbuffer_size; i++)
ofile.close();*/ ofile << vbuffer[i];
ofile.close();
mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n"); mLockedCout("[OUTPUT] File '" + fullpath + "' has been written\n");
//delete[] vbuffer; delete[] vbuffer;
return true; return true;
} }
@@ -75,17 +70,22 @@ int main(const int argc, const char* argv[]) {
} }
// Reading key // Reading key
string key; string skey;
cout << "[INPUT] Enter key: "; cout << "[INPUT] Enter key: ";
cin >> key; cin >> skey;
cout << endl; cout << endl;
// Key conversion
unsigned int bkey[4];
keyConversion(skey, bkey);
cout << "[OUTPUT] Key converted" << endl;
thread** threads; thread** threads;
if(argc > 1) { if(argc > 1) {
threads = new thread*[argc - 1]; threads = new thread*[argc - 1];
for(int i = 0; i < argc - 1; i++) { for(int i = 0; i < argc - 1; i++) {
string tfpath(argv[i + 1]); string tfpath(argv[i + 1]);
thread* t = new thread(decodeFile, tfpath, key); thread* t = new thread(decodeFile, tfpath, bkey);
threads[i] = t; threads[i] = t;
} }
for(int i = 0; i < argc - 1; i++) { for(int i = 0; i < argc - 1; i++) {
@@ -93,18 +93,18 @@ int main(const int argc, const char* argv[]) {
} }
} else { } else {
threads = new thread*[1]; threads = new thread*[1];
thread* t = new thread(decodeFile, fpath, key); thread* t = new thread(decodeFile, fpath, bkey);
threads[0] = t; threads[0] = t;
t->join(); t->join();
} }
for(size_t i = 0; i < argc - 1; i++) { for(int i = 0; i < argc - 1; i++) {
delete threads[i]; delete threads[i];
} }
delete[] threads; delete[] threads;
cout << "[OUTPUT] Selected files are decrypted, press any key to close program." << endl; cout << "[OUTPUT] Selected files are decrypted, press any key to close program." << endl;
//getch(); getch();
return 0; return 0;
} }

View File

@@ -2,7 +2,7 @@
<CodeBlocks_project_file> <CodeBlocks_project_file>
<FileVersion major="1" minor="6" /> <FileVersion major="1" minor="6" />
<Project> <Project>
<Option title="model_coder" /> <Option title="full_model_coder" />
<Option pch_mode="2" /> <Option pch_mode="2" />
<Option compiler="gcc" /> <Option compiler="gcc" />
<Build> <Build>

View File

@@ -1,7 +1,7 @@
#ifndef TEA_ENCODE #ifndef TEA_ENCODE
#define TEA_ENCODE #define TEA_ENCODE
void encode(unsigned int* v, const unsigned int* k); void encode(unsigned int*, const unsigned int*);
void decode(unsigned int* v, const unsigned int* k); void decode(unsigned int*, const unsigned int*);
#endif #endif

22
test/full_file_decode.lua Normal file
View 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

View File

@@ -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