Skip to content

Commit

Permalink
Merge pull request #14 from ArthurF23/Bitmap-encryption-addition
Browse files Browse the repository at this point in the history
Bitmap encryption addition
  • Loading branch information
ArthurF23 authored Feb 24, 2022
2 parents 49ee969 + 7d92a70 commit e670fa0
Show file tree
Hide file tree
Showing 36 changed files with 270 additions and 28 deletions.
12 changes: 12 additions & 0 deletions .breakpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"files": {
"Headers/Encryption/BMP/BMP_rw.cpp": [
{
"id": "adc090d2-84c7-4b02-b72c-881ecbdec91b",
"line": 7,
"version": 20,
"index": 134
}
]
}
}
81 changes: 81 additions & 0 deletions Headers/Encryption/BMP/BMP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "BMP_includes.h"

#include "BMP.h"
namespace BITMAP {
void BMP::ReadImage(const char *fileName,BMPbyte **pixels, int32 *width, int32 *height, int32 *bytesPerPixel) {
FILE *imageFile = fopen(fileName, "rb");
int32 dataOffset;
fseek(imageFile, DATA_OFFSET_OFFSET, SEEK_SET);
fread(&dataOffset, 4, 1, imageFile);
fseek(imageFile, WIDTH_OFFSET, SEEK_SET);
fread(width, 4, 1, imageFile);
fseek(imageFile, HEIGHT_OFFSET, SEEK_SET);
fread(height, 4, 1, imageFile);
int16 bitsPerPixel;
fseek(imageFile, BITS_PER_PIXEL_OFFSET, SEEK_SET);
fread(&bitsPerPixel, 2, 1, imageFile);
*bytesPerPixel = ((int32)bitsPerPixel) / 8;

int paddedRowSize = (int)(4 * ceil((float)(*width) / 4.0f))*(*bytesPerPixel);
int unpaddedRowSize = (*width)*(*bytesPerPixel);
int totalSize = unpaddedRowSize*(*height);
*pixels = (BMPbyte*)malloc(totalSize);
int i = 0;
BMPbyte *currentRowPointer = *pixels+((*height-1)*unpaddedRowSize);
for (i = 0; i < *height; i++)
{
fseek(imageFile, dataOffset+(i*paddedRowSize), SEEK_SET);
fread(currentRowPointer, 1, unpaddedRowSize, imageFile);
currentRowPointer -= unpaddedRowSize;
}

fclose(imageFile);
};

void BMP::WriteImage(const char *fileName, BMPbyte *pixels, int32 width, int32 height,int32 bytesPerPixel) {
FILE *outputFile = fopen(fileName, "wb");
//*****HEADER************//
const char *BM = "BM";
fwrite(&BM[0], 1, 1, outputFile);
fwrite(&BM[1], 1, 1, outputFile);
int paddedRowSize = (int)(4 * ceil((float)width/4.0f))*bytesPerPixel;
int32 fileSize = paddedRowSize*height + HEADER_SIZE + INFO_HEADER_SIZE;
fwrite(&fileSize, 4, 1, outputFile);
int32 reserved = 0x0000;
fwrite(&reserved, 4, 1, outputFile);
int32 dataOffset = HEADER_SIZE+INFO_HEADER_SIZE;
fwrite(&dataOffset, 4, 1, outputFile);

//*******INFO*HEADER******//
int32 infoHeaderSize = INFO_HEADER_SIZE;
fwrite(&infoHeaderSize, 4, 1, outputFile);
fwrite(&width, 4, 1, outputFile);
fwrite(&height, 4, 1, outputFile);
int16 planes = 1; //always 1
fwrite(&planes, 2, 1, outputFile);
int16 bitsPerPixel = bytesPerPixel * 8;
fwrite(&bitsPerPixel, 2, 1, outputFile);
//write compression
int32 compression = NO_COMPRESION;
fwrite(&compression, 4, 1, outputFile);
//write image size (in bytes);
int32 imageSize = width*height*bytesPerPixel;
fwrite(&imageSize, 4, 1, outputFile);
int32 resolutionX = 11811; //300 dpi
int32 resolutionY = 11811; //300 dpi
fwrite(&resolutionX, 4, 1, outputFile);
fwrite(&resolutionY, 4, 1, outputFile);
int32 colorsUsed = MAX_NUMBER_OF_COLORS;
fwrite(&colorsUsed, 4, 1, outputFile);
int32 importantColors = ALL_COLORS_REQUIRED;
fwrite(&importantColors, 4, 1, outputFile);
int i = 0;
int unpaddedRowSize = width*bytesPerPixel;
for ( i = 0; i < height; i++)
{
int pixelOffset = ((height - i) - 1)*unpaddedRowSize;
fwrite(&pixels[pixelOffset], 1, paddedRowSize, outputFile);
}
fclose(outputFile);
}
}
9 changes: 9 additions & 0 deletions Headers/Encryption/BMP/BMP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "BMP_includes.h"

namespace BITMAP {
class BMP {
public:
static void ReadImage(const char *fileName,BMPbyte **pixels, int32 *width, int32 *height, int32 *bytesPerPixel);
static void WriteImage(const char *fileName, BMPbyte *pixels, int32 width, int32 height,int32 bytesPerPixel);
};
}
23 changes: 23 additions & 0 deletions Headers/Encryption/BMP/BMP_includes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include <fstream>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

#define DATA_OFFSET_OFFSET 0x000A
#define WIDTH_OFFSET 0x0012
#define HEIGHT_OFFSET 0x0016
#define BITS_PER_PIXEL_OFFSET 0x001C
#define HEADER_SIZE 14
#define INFO_HEADER_SIZE 40
#define NO_COMPRESION 0
#define MAX_NUMBER_OF_COLORS 0
#define ALL_COLORS_REQUIRED 0

typedef unsigned int int32;
typedef short int16;
typedef unsigned char BMPbyte;
124 changes: 107 additions & 17 deletions Headers/Encryption/encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ namespace encryption {
/////////Pass through encrypt & decrypt functions//////
/////////that input and output strings/////////////////
///////////////////////////////////////////////////////
AESbyte AES::KEY::key[mtx_size];
AESbyte AES::KEY::key[mtx_size]; //Key definition

/////////////
///Encrypt///
Expand Down Expand Up @@ -557,6 +557,61 @@ namespace encryption {
data += buffer;
//Don't need this anymore sooo...
delete[] buffer;
infile.close();
};

void AES::FILES::TXT::out(string path, string data) {
//Original File
ofstream {path}; //Create... Doesn't matter if it's overwritten because it's about to be anyways
ofstream outfile(path, ios::out | ios::trunc);
outfile << data; //write to file
outfile.close(); //close file
}

void AES::FILES::BMP_::get(string path, string& data) {
char* filename = const_cast<char*>(path.c_str());
BMPbyte *pixels;
int32 width;
int32 height;
int32 bytesPerPixel;
//Get size of pixel arr with width*height*bytesPerPixel
BMP::ReadImage(filename, &pixels, &width, &height, &bytesPerPixel);
data+=to_string(width)+FILES::BMP_::DATA_SEPARATOR;
data+=to_string(height)+FILES::BMP_::DATA_SEPARATOR;
data+=to_string(bytesPerPixel)+FILES::BMP_::DATA_SEPARATOR;
for (unsigned int i=0; i<(width*height*bytesPerPixel); i++) {
data+=to_string(pixels[i]) + NUM_SEPARATOR;
};
};

void AES::FILES::BMP_::out(string path, string data) {
char* filename = const_cast<char*>(path.c_str());
data.erase(0, data.find_first_of(AES::FILES::EXTENSION_SEPERATOR)+1);
int32 width;
int32 height;
int32 bytesPerPixel;
string str;
str = data.substr(0, data.find_first_of(DATA_SEPARATOR));
width = stoi(str);
data.erase(0, data.find_first_of(DATA_SEPARATOR)+1);
str = data.substr(0, data.find_first_of(DATA_SEPARATOR));
height = stoi(str);
data.erase(0, data.find_first_of(DATA_SEPARATOR)+1);
str = data.substr(0, data.find_first_of(DATA_SEPARATOR));
bytesPerPixel = stoi(str);
data.erase(0, data.find_first_of(DATA_SEPARATOR)+1);
unsigned int length = width*height*bytesPerPixel;
BMPbyte pixels[length];
//BMP::getPix(&pixels, &width, &height, &bytesPerPixel);
for (int i=0; i<length; i++) {
string sm = data.substr(0, data.find_first_of(NUM_SEPARATOR));
unsigned int num = stoi(sm);
pixels[i] = (num);
if (data.find(NUM_SEPARATOR) != string::npos) {
data.erase(0, data.find_first_of(NUM_SEPARATOR)+1);
};
};
BMP::WriteImage(filename, pixels, width, height, bytesPerPixel);
};

//Design: file type will be in front of all the data.
Expand All @@ -569,7 +624,16 @@ namespace encryption {
const string AES::FILES::FILE_EXTENSION = ".aesenc";
const string AES::FILES::KEYFILE_NAME = "_KEYFILE";
const string AES::FILES::KEYFILE_EXT = ".aeskey";
const string AES::FILES::TXT::identifier[6] = {".txt", ".md", ".cpp", ".h", ".cs", ".c"};
const string AES::FILES::TXT::identifier[id_len] = {".txt", ".md", ".cpp", ".h", ".cs", ".c"};
const string AES::FILES::BMP_::identifier = ".bmp";

void AES::FILES::classify(string ext, AES::FILES::CLASSIFIER &type) {
for (int i=0; i < AES::FILES::TXT::id_len; i++) {
if (ext == FILES::TXT::identifier[i]) {type=FILES::CLASSIFIER::_TEXT;break;};
};

if (ext == FILES::BMP_::identifier) {type=FILES::CLASSIFIER::_BITMAP;};
}

//Key File
bool AES::FILES::gen_key_file(string path) {
Expand Down Expand Up @@ -619,14 +683,25 @@ namespace encryption {
//Add the extension + seperator to the data string
string data = ext + FILES::EXTENSION_SEPERATOR;

//Only can do text based
if (ext == FILES::TXT::identifier[0] || ext == FILES::TXT::identifier[1]) {
FILES::TXT::get(path, data);
} else {return false;};

FILES::CLASSIFIER type = FILES::CLASSIFIER::_RETURN;
FILES::classify(ext, type);

switch (type) {
case FILES::CLASSIFIER::_RETURN:
return false;
case FILES::CLASSIFIER::_TEXT:
FILES::TXT::get(path, data);
break;
case FILES::CLASSIFIER::_BITMAP:
FILES::BMP_::get(path,data);
break;
default:
return false;
};

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

//Encrypt it

data = encrypt(data);

//Make new file & path using old path by removing the extension from the string
Expand Down Expand Up @@ -654,7 +729,6 @@ namespace encryption {
string data;

//Just like in encryptFile

AES::FILES::TXT::get(path, data);

if (keyFilePath != "") {AES::FILES::in_key_file(keyFilePath);}
Expand All @@ -666,18 +740,34 @@ namespace encryption {

//Make new path
path.erase(path.find_last_of('.'), path.length());
path += data.substr(data.find_first_of('.'), data.find_first_of(FILES::EXTENSION_SEPERATOR));
string ext = data.substr(data.find_first_of('.'), data.find_first_of(FILES::EXTENSION_SEPERATOR));
path += ext; //add extension to path


//Erase that part of the decrypted data
data.erase(data.find_first_of('.'), data.find_first_of(FILES::EXTENSION_SEPERATOR)+1);
data.erase(data.length(), data.length());
//Original File
ofstream {path}; //Create... Doesn't matter if it's overwritten because it's about to be anyways
ofstream outfile(path, ios::out | ios::trunc);
//Write to file
outfile << data;
//Close the file
outfile.close();



FILES::CLASSIFIER type = FILES::CLASSIFIER::_RETURN;
FILES::classify(ext, type);

switch (type) {
case FILES::CLASSIFIER::_RETURN:
return false;

case FILES::CLASSIFIER::_TEXT:
FILES::TXT::out(path,data);
break;

case FILES::CLASSIFIER::_BITMAP:
FILES::BMP_::out(path,data);
break;

default:
return false;
};
return true;
};

Expand Down
22 changes: 19 additions & 3 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.7.1";
static string ver = "v1.8.0";
//Major, Minor, Patch
//for major or minor, change patch to 0
}
Expand Down Expand Up @@ -233,7 +233,7 @@ namespace encryption {
static char binary_to_char(AESbyte input);
static AESbyte binStr_to_byte(string input);
};

//Random number generation function
static inline unsigned int getRandomNum(unsigned int min, unsigned int max);
//Expand Key
Expand All @@ -249,17 +249,33 @@ namespace encryption {

class FILES {
public:
enum CLASSIFIER {
_RETURN = 0b00000000,
_TEXT = 0b00000001,
_BITMAP = 0b00000010
};
static const string FILE_EXTENSION;
static const string KEYFILE_NAME;
static const string KEYFILE_EXT;
static constexpr char EXTENSION_SEPERATOR = '~';
static bool gen_key_file(string path);
static bool in_key_file(string path);
static void classify(string ext, CLASSIFIER &clasif);

class TXT {
public:
static const string identifier[6];
static constexpr short id_len = 6;
static const string identifier[id_len];
static void get(string path, string& data);
static void out(string path, string data);
};
class BMP_ {
public:
static const string identifier;
static constexpr char DATA_SEPARATOR = ':';
static constexpr char NUM_SEPARATOR = ',';
static void get(string path, string& data);
static void out(string path, string data);
};
};

Expand Down
4 changes: 4 additions & 0 deletions Headers/Encryption/encryption_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
#include <random>
#include <bitset>
#include <fstream>
#include <cstdlib>
using namespace std;

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

#include "BMP/BMP.h"
using namespace BITMAP;

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

### v1.7.0
### v1.8.0

### What's new?
Patched the AES key as the generated key was not being set to the key. Location has changed to `AES::KEY::key`. If you were using the previous version, `AESKEY::key` simply add `::` between `AES` and `KEY`.

###### v1.7.1
Added a key file for `encryptFile()` & `decryptFile()`
Added bitmap encryption. You can now pass a .bmp file to `AES::encryptFile()` to encrypt a bitmap file.

## 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
1 change: 1 addition & 0 deletions bitmap/image.aesenc

Large diffs are not rendered by default.

Binary file added bitmap/image.bmp
Binary file not shown.
1 change: 1 addition & 0 deletions bitmap/image_KEYFILE.aeskey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
L,a,€< ,j`}jYjx+`}N,"TnAz,_UOz.l.1;
1 change: 1 addition & 0 deletions bitmap/img.aesenc

Large diffs are not rendered by default.

Binary file added bitmap/img.bmp
Binary file not shown.
1 change: 1 addition & 0 deletions bitmap/img_KEYFILE.aeskey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
)RAX€aVMIJl+ELzxBOCOxz,L"}yUN;
Binary file modified main
Binary file not shown.
Loading

0 comments on commit e670fa0

Please sign in to comment.