mirror of
https://github.com/JDM170/model_coder
synced 2025-12-10 05:57:19 +07:00
Full file coding
Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
bin/
|
||||
obj/
|
||||
*.depend
|
||||
*.layout
|
||||
96
base64.cpp
Normal file
96
base64.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "base64.h"
|
||||
#include <iostream>
|
||||
|
||||
static const string base64_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
|
||||
static inline bool is_base64(BYTE c) {
|
||||
return (isalnum(c) || (c == '+') || (c == '/'));
|
||||
}
|
||||
|
||||
string base64encode(BYTE const* buf, unsigned int bufLen) {
|
||||
string ret;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
BYTE char_array_3[3];
|
||||
BYTE char_array_4[4];
|
||||
|
||||
while (bufLen--) {
|
||||
char_array_3[i++] = *(buf++);
|
||||
if (i == 3) {
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
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(i = 0; (i < 4) ; i++)
|
||||
ret += base64_chars[char_array_4[i]];
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i)
|
||||
{
|
||||
for(j = i; j < 3; j++)
|
||||
char_array_3[j] = '\0';
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
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++)
|
||||
ret += base64_chars[char_array_4[j]];
|
||||
|
||||
while(i++ < 3)
|
||||
ret += '=';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
vector<BYTE> base64decode(string const& encoded_string) {
|
||||
int in_len = encoded_string.size();
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int in_ = 0;
|
||||
BYTE char_array_4[4], char_array_3[3];
|
||||
vector<BYTE> ret;
|
||||
|
||||
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
|
||||
char_array_4[i++] = encoded_string[in_];
|
||||
in_++;
|
||||
if (i == 4) {
|
||||
for (i = 0; i < 4; i++)
|
||||
char_array_4[i] = base64_chars.find(char_array_4[i]);
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
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++)
|
||||
ret.push_back(char_array_3[i]);
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 4; j++)
|
||||
char_array_4[j] = 0;
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
char_array_4[j] = base64_chars.find(char_array_4[j]);
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
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++)
|
||||
ret.push_back(char_array_3[j]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
12
base64.h
Normal file
12
base64.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _BASE64_H_
|
||||
#define _BASE64_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
typedef unsigned char BYTE;
|
||||
|
||||
string base64encode(BYTE const* buf, unsigned int bufLen);
|
||||
vector<BYTE> base64decode(string const&);
|
||||
|
||||
#endif
|
||||
93
main.cpp
Normal file
93
main.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
#include "base64.h"
|
||||
#include "tea.h"
|
||||
|
||||
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
|
||||
|
||||
using namespace std;
|
||||
|
||||
string getFileName(const string& s, string* pathwithoutname) {
|
||||
char sep = '/';
|
||||
#ifdef _WIN32
|
||||
sep = '\\';
|
||||
#endif
|
||||
size_t i = s.rfind(sep, 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;
|
||||
}
|
||||
|
||||
int main(const int argc, const char* argv[])
|
||||
{
|
||||
const string fpath = "test.txd";
|
||||
const string key = "JdzFR2XLDaBtpGGD";
|
||||
|
||||
// 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];
|
||||
cout << "[OUTPUT] Key converted" << endl;
|
||||
|
||||
// Reading file
|
||||
ifstream file(fpath, ios::in | ios::binary);
|
||||
struct stat results;
|
||||
stat(fpath.c_str(), &results);
|
||||
size_t file_size = results.st_size;
|
||||
char* fbuffer = new char[file_size];
|
||||
file.read(fbuffer, file_size);
|
||||
file.close();
|
||||
cout << "[OUTPUT] File has been read" << endl;
|
||||
|
||||
// Creating buffer
|
||||
size_t vbuffer_size = file_size;
|
||||
if (vbuffer_size % 4 > 0)
|
||||
vbuffer_size += 4 - (vbuffer_size % 4);
|
||||
unsigned char* vbuffer = new unsigned char [vbuffer_size];
|
||||
memset(vbuffer, 0, vbuffer_size);
|
||||
memcpy(vbuffer, fbuffer, file_size);
|
||||
|
||||
// Crypting file
|
||||
size_t obuffer_size = vbuffer_size + 4;
|
||||
char* obuffer = new char[obuffer_size];
|
||||
unsigned int v[2];
|
||||
memset(v, 0, sizeof(v));
|
||||
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);
|
||||
memcpy(vbuffer, fbuffer, file_size);
|
||||
}
|
||||
memcpy(&obuffer[obuffer_size - 4], &v[1], 4);
|
||||
delete[] fbuffer;
|
||||
delete[] vbuffer;
|
||||
|
||||
// Base64 encoding
|
||||
string ob64 = base64encode((unsigned char*)obuffer, obuffer_size);
|
||||
delete[] obuffer;
|
||||
cout << "[OUTPUT] File has been crypted" << endl;
|
||||
|
||||
// File name generating
|
||||
string filefolder;
|
||||
string old_filename = getFileName(fpath, &filefolder);
|
||||
string fullpath = filefolder + old_filename + ".enc";
|
||||
|
||||
// Writing file
|
||||
ofstream ofile(fullpath.c_str(), ios::out | ios::binary);
|
||||
if (ofile.good()) {
|
||||
ofile.clear();
|
||||
}
|
||||
ofile << ob64;
|
||||
ofile.close();
|
||||
cout << "[OUTPUT] File has been written, exiting..." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
project.cbp
Normal file
41
project.cbp
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="test" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/test" 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/test" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Unit filename="base64.cpp" />
|
||||
<Unit filename="base64.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="tea.cpp" />
|
||||
<Unit filename="tea.h" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
32
tea.cpp
Normal file
32
tea.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "tea.h"
|
||||
|
||||
const unsigned int delta = 0x9E3779B9;
|
||||
const unsigned int sum_int = 0xC6EF3720;
|
||||
|
||||
void encode(unsigned int* v, const unsigned int* k) {
|
||||
unsigned int v0 = v[0],
|
||||
v1 = v[1],
|
||||
i,
|
||||
sum = 0;
|
||||
for(i = 0; i < 32; i++) {
|
||||
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
||||
sum += delta;
|
||||
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
|
||||
}
|
||||
v[0] = v0;
|
||||
v[1] = v1;
|
||||
}
|
||||
|
||||
void decode(unsigned int* v, const unsigned int* k) {
|
||||
unsigned int v0 = v[0],
|
||||
v1 = v[1],
|
||||
i,
|
||||
sum = sum_int;
|
||||
for(i = 0; i < 32; i++) {
|
||||
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
|
||||
sum -= delta;
|
||||
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
||||
}
|
||||
v[0] = v0;
|
||||
v[1] = v1;
|
||||
}
|
||||
7
tea.h
Normal file
7
tea.h
Normal file
@@ -0,0 +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);
|
||||
|
||||
#endif
|
||||
17
test/mta_decode.lua
Normal file
17
test/mta_decode.lua
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
-- Opening crypted file
|
||||
local file = fileOpen("test.txd.enc")
|
||||
local fileData = fileRead(file, fileGetSize(file))
|
||||
fileClose(file)
|
||||
|
||||
-- Decoding file data
|
||||
local decoded = teaDecode(fileData, "JdzFR2XLDaBtpGGD")
|
||||
fileData = nil
|
||||
|
||||
-- Writing uncrypted data to clean file
|
||||
file = fileCreate("test.txd.dec")
|
||||
fileWrite(file, decoded)
|
||||
fileClose(file)
|
||||
|
||||
decoded = nil
|
||||
file = nil
|
||||
BIN
test/test.txd
Normal file
BIN
test/test.txd
Normal file
Binary file not shown.
1
test/test.txd.enc
Normal file
1
test/test.txd.enc
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user