Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.5.0 compression implementation #13

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Headers/Encryption/Compression/compression_includes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <iostream>
#include <string>
#include <random>
#include <fstream>
#include <memory>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
41 changes: 41 additions & 0 deletions Headers/Encryption/Compression/compressor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "compression_includes.h"

#include "compressor.h"
namespace COMPRESSION {
//Roughly ~20% compression ratio on the bee movie script, my accurate representation of a lot of data. Th original binary length was 437376 and was compressed to 328840 characters long.
string binary_compression::compress(string input) {
string str;

for (int i = 0, increment = 0; i < input.length(); i++) {
for (increment = 1;; increment++) {
if (input[i] != input[i+increment] || increment == 9) {break;};
};
if (increment > 1) {
str+=input[i] + to_string(increment); i += increment-1;
}
else {
str += input[i];
};
increment = 1;
}
return str + breakChar;
};

string binary_compression::decompress(string input) {
string output;

cout << input << endl;
for (int i = 0; i < input.length(); i++) {
if (input[i] == breakChar) {break;};
if (input[i+1] != '0' && input[i+1] != '1' && input[i+1] != breakChar) {
for (int x = 0; x < input[i+1]-'0'; x++) {
output += input[i];
};
i++;
} else {output+=input[i];};
};
cout << input << endl;
cout << output << endl;
return output;
};
};
11 changes: 11 additions & 0 deletions Headers/Encryption/Compression/compressor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "compression_includes.h"

namespace COMPRESSION {
class binary_compression {
public:
static string compress(string path);
static string decompress(string path);
private:
static constexpr char breakChar = ';';
};
};
34 changes: 25 additions & 9 deletions Headers/Encryption/encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,19 +588,31 @@ namespace encryption {

ext.clear(); //Delete because it's useless
//Encrypt it

data = encrypt(data);
data = binary_compression::compress(data);

//Add some random bs to it to make it not look binary anymore
int *rep = new int(data.length() * AES::getRandomNum(AES::FILES::FILE_GEN_PARAMS::minChar, AES::FILES::FILE_GEN_PARAMS::maxChar));
unsigned long long *rep = new unsigned long long(data.length() * AES::getRandomNum(AES::FILES::FILE_GEN_PARAMS::minGenMul, AES::FILES::FILE_GEN_PARAMS::maxGenMul));

//Make sure the bloat doesnt max out the string length
if (*rep >= data.max_size()) {
*rep-= (*rep - data.max_size())-2;
};

for (int i = 0; i < *rep;) {
char* c = new char(AES::getRandomNum(AES::FILES::FILE_GEN_PARAMS::minChar, AES::FILES::FILE_GEN_PARAMS::maxChar));
if (*c != AES::FILES::FILE_GEN_PARAMS::invalid[0] && *c != AES::FILES::FILE_GEN_PARAMS::invalid[1]) {
char* c;
for (unsigned long long i = 0; i < *rep;) {
c = new char(AES::getRandomNum(AES::FILES::FILE_GEN_PARAMS::minChar, AES::FILES::FILE_GEN_PARAMS::maxChar));
bool isInvalid = false;
for (int h = 0; h < AES::FILES::FILE_GEN_PARAMS::invalidLength; h++) {
if (AES::FILES::FILE_GEN_PARAMS::invalid[h] == *c) {isInvalid = true; break;};
};
if (isInvalid == false) {
data.insert(AES::getRandomNum(0, data.length()), c);
i++;
};
delete c;
};
delete c;
delete rep;

//Make new file & path using old path by removing the extension from the string
Expand Down Expand Up @@ -644,19 +656,23 @@ namespace encryption {
data += buffer;
//Don't need this anymore sooo...
delete[] buffer;

//remove bloat
string filtered;
int *dataL = new int(data.length());
for (int i = 0; i < *dataL; i++) {
if ((int)data[i] == AES::FILES::FILE_GEN_PARAMS::invalid[0] || data[i] == AES::FILES::FILE_GEN_PARAMS::invalid[1]) {
bool isBad = false;
for (int y = 0; y < AES::FILES::FILE_GEN_PARAMS::invalidLength; y++) {
if (data[i] == AES::FILES::FILE_GEN_PARAMS::invalid[y]) {isBad=true; break;};
};
if (isBad==true) {
filtered += data[i];
};
}
};
delete dataL;
data = filtered;
filtered.clear();

data = binary_compression::decompress(data);
//Decrypt
data = decrypt(data);

Expand Down
7 changes: 5 additions & 2 deletions Headers/Encryption/encryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ constexpr unsigned short GLOBAL_MTX_SIZE = 4*4;
namespace encryption {

namespace VERSION {
static string ver = "v1.4.3";
static string ver = "v1.5.0";
//Major, Minor, Patch
//for major or minor, change patch to 0
}
Expand Down Expand Up @@ -262,7 +262,10 @@ namespace encryption {
public:
static constexpr short minChar = 32;
static constexpr short maxChar = 126;
static constexpr short invalid[2] = {48, 49};
static constexpr short minGenMul = 30;
static constexpr short maxGenMul = 60;
static constexpr short invalidLength = 11;
static constexpr short invalid[invalidLength] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59};
};
class TXT {
public:
Expand Down
3 changes: 3 additions & 0 deletions Headers/Encryption/encryption_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
#include <fstream>
using namespace std;

#include "Compression/compressor.h"
using namespace COMPRESSION;

typedef bitset<8> AESbyte;
typedef bitset<32> AESword;
4 changes: 2 additions & 2 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Thank you for using my header!

### v1.4.3
### v1.5.0

### What's new?
Fixed bug with `encryptFile()` and `decryptFile()` that would add weird values trailing at the end of the decrypted files. Added bloat to data in the `.aesenc` files.
Added binary compression to `encryptFile()`. So when `encrypt()` outputs a binary string, it gets encrypted using lossless compression. It has a ratio of about 20% on the bee movie script which is what I use to test large files.

## Basic Encryption
The program has the key which is located through `encryption::KEY::key`, you will need to set this before using the `encryption::encdec::decrypt()` function, since that function grabs from the namespace to use it and the only thing you pass to that function is the string. You must set the key before you call that function. It's not a problem for the `encrypt()` function since it generates a key each time its called. The generated key is the same varible `encryption::KEY::key`. Anytime you need to grab the key or set it, it is there, it will be nowhere else.
Expand Down
Loading