Skip to content

Commit

Permalink
Merge pull request #10 from ArthurF23/v1.2.2-Example-project-patch-br…
Browse files Browse the repository at this point in the history
…anch

v1.3.0
  • Loading branch information
ArthurF23 authored Feb 1, 2022
2 parents b7962ac + 324a384 commit 682321b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 114 deletions.
104 changes: 7 additions & 97 deletions Headers/Encryption/encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,7 @@ namespace encryption {
">_>>",
"_.>>"
};
int encdec::start_example() {
cout << "Are you Encrypting or Decrypting?" << endl << "e/d" << endl;
char user_choice; cin >> user_choice; cout << "Please input the text" << endl << "Input here:" << endl; string user_inputted_string;
cin.ignore();
getline(cin >> noskipws, user_inputted_string);

if (user_choice == 'e') {
KEY::key = encdec::generate_key();
output_str = encdec::encrypt(user_inputted_string);
}
else if (user_choice == 'd') {
encdec::get_key();
output_str = encdec::decrypt(user_inputted_string);
cin.ignore();
}
else {
cout << "ERR please try again" << endl;
return encdec::start_example();
};
cout << "Key: " << KEY::key << endl << "Result: " << endl << output_str << endl;
cout << "Press any key to continue " << endl;
cin.ignore();
return 0;
}

bool encdec::isNumberString(string input) {
for (char &c : input) {
if (!isdigit(c)) {return false;};
Expand Down Expand Up @@ -80,9 +57,10 @@ namespace encryption {
return (char)encdec::get_random_num(encdec::constants::char_info::valid_char_min, encdec::constants::char_info::valid_char_max);
}

bool encdec::validate_key(string inp) {
bool encdec::assign_key(string inp) {
if (encdec::isNumberString(inp)) {
if (inp.length() == encdec::constants::key_info::key_length && stoi(inp) >= encdec::constants::key_info::key_min_value) {
KEY::key = stoi(inp);
return true;
};
}
Expand Down Expand Up @@ -153,20 +131,6 @@ namespace encryption {
output += char(stoi(chunk) - (KEY::key/scramble));
};
return output;
}

bool encdec::get_key() {
cout << "Please input " << encdec::constants::key_info::key_length << " digit numeric key: " << endl;
string user_input; cin >> user_input; cout << endl << "# " << user_input << " #" << endl;
if (encdec::validate_key(user_input)) {
KEY::key = stoi(user_input);
return true;
}
else {
cout << "Invalid key, please make sure your key is " << encdec::constants::key_info::key_length << " digits long and does not contain anything besides numbers." << endl;
return false;
}
return false;
};

/////////////////////////////////////////////////////////
Expand Down Expand Up @@ -384,7 +348,7 @@ namespace encryption {

AESword AES::global_expanded_key[expanded_key_size];

void AES::aes_init(OPTIONS genkey, string dummykey = "") {
void AES::aes_init(OPTIONS genkey, string dummykey) {
if (genkey == OPTIONS::doGenerateKey || dummykey.length() < mtx_size*bitsInByte) {
generate_key();
}
Expand Down Expand Up @@ -568,42 +532,6 @@ namespace encryption {
return true;
};

/////////////
///Example///
/////////////

int AES::start_example() {
string actual_string;
aes_init(OPTIONS::doGenerateKey); //Call before use

//Output key
cout << "Key: ";
for(int i=0; i<mtx_size; ++i) {
cout << AESKEY::key[i] << " ";
};
cout << endl;

cout << "Input what you would like encrypted:" << endl;
cin.ignore();
getline(cin >> noskipws, actual_string);


cout << endl << "Plaintext to be encrypted:"<< endl << actual_string << endl;

//Encryption, output ciphertext
actual_string = encrypt(actual_string);
cout << "Encrypted ciphertext:" << endl << actual_string << endl;
//Decrypt, output plaintext
actual_string = decrypt(actual_string);
cout << "Decrypted plaintext:" << endl << actual_string << endl;
cout << "Press any key to continue" << endl;
cin.ignore();
return 0;
};




///////////////////////////////////
///Making my own encrypted files///
///////////////////////////////////
Expand All @@ -616,6 +544,7 @@ namespace encryption {

//Extension: .aesenc
const string AES::FILE_EXTENSION = ".aesenc";

bool AES::encryptFile(string path) {
//checks if path is valid
ifstream infile(path, std::ifstream::binary);
Expand Down Expand Up @@ -733,30 +662,11 @@ namespace encryption {
//3rd param this is the string of 16 bytes that is the AES key youll need to input this if you dont want to generate a key
void DUO::init(string encdecKey, encryption::AES::OPTIONS genKey, string aesKey) {
//encdec init key
if (encryption::encdec::validate_key(encdecKey)) {
encryption::KEY::key = stoi(encdecKey);
if (!encryption::encdec::assign_key(encdecKey)) {
return;
};

//AES init
encryption::AES::aes_init(genKey, aesKey);
};

int DUO::example() {
string input;
cout << "Please input a string" << endl;
cin.ignore();
cin >> input;

init("82468224");

input = encrypt(input/*, encryption::encdec::FLAGS::no_bloat, encryption::encdec::FLAGS::no_rand_pattern*/);

cout << "Encrypted Result: " << endl << input << endl << "Decrypted Result: " << endl;

input = decrypt(input);

cout << input << endl;
return 0;
};

};
10 changes: 3 additions & 7 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.2.1";
static string ver = "v1.3.0";
//Major, Minor, Patch
//for major or minor, change patch to 0
}
Expand Down Expand Up @@ -153,17 +153,13 @@ namespace encryption {
do_rand_pattern = 0b11
};
//Checks to see if the key is valid
static bool validate_key(string inp);
static bool assign_key(string inp);
//Key generator
static unsigned int generate_key();
//Example Function
static int start_example();
//Encrypt input string
static string encrypt(string input, FLAGS bloat = FLAGS::do_bloat, FLAGS pattern = FLAGS::do_rand_pattern);
//Decrypt input string
static string decrypt(string input);
//Part of example, prompts user for key
static bool get_key(); //True = key valid | False = key invalid
};

class AES {
Expand Down Expand Up @@ -273,7 +269,7 @@ namespace encryption {
static bool decryptFF(string path);

//Call before use
static void aes_init(OPTIONS genkey, string dummykey);
static void aes_init(OPTIONS genkey, string dummykey = "");

static bool encryptFile(string path);
static bool decryptFile(string path);
Expand Down
7 changes: 3 additions & 4 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
## Thank you for using my header!

### v1.2.1
### v1.3.0

### What's new?
New "DUO" encrypting that utilizes both the `encdec::encrypt` and `AES::encrypt` (and their decryption varients) to make something that will be particularly hard to crack.
Changed all examples to make the header smaller, I moved all the examples to the main.cpp file so that code won't be in the header files.

#### Bug Fixes

###### Not really a bug although what was changed is the definition of Nr and Nk, they are more descriptive of what the actually are.
Added an example for each feature that the header offers.

## 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
Binary file modified main
Binary file not shown.
156 changes: 150 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,165 @@
#include "Headers/includes.h"

//1
void encdecExample() {
cout << "Are you Encrypting or Decrypting?" << endl << "e/d" << endl;
char user_choice; cin >> user_choice; cout << "Please input the text" << endl << "Input here:" << endl; string user_inputted_string;
cin.ignore();
getline(cin >> noskipws, user_inputted_string);

if (user_choice == 'e') {
KEY::key = encdec::generate_key();
output_str = encdec::encrypt(user_inputted_string);
}
else if (user_choice == 'd') {
cout << "Please enter the numeric key" << endl;
string _key;
cin >> _key;
encdec::assign_key(_key);
output_str = encdec::decrypt(user_inputted_string);
cin.ignore();
}
else {
cout << "ERR please try again" << endl;
return;
};

cout << "Key: " << KEY::key << endl << "Result: " << endl << output_str << endl;
cout << "Press any key to continue " << endl;
cin.ignore();
};

//2
void aesExample() {
string actual_string;
AES::aes_init(AES::OPTIONS::doGenerateKey); //Call before use

//Output key
cout << "Key: ";
for(int i=0; i<sizeof(AESKEY::key)/sizeof(AESKEY::key[0]); ++i) {
cout << AESKEY::key[i] << " ";
};
cout << endl;

cout << "Input what you would like encrypted:" << endl;
cin.ignore();
getline(cin >> noskipws, actual_string);


cout << endl << "Plaintext to be encrypted:"<< endl << actual_string << endl;

//Encryption, output ciphertext
actual_string = AES::encrypt(actual_string);
cout << "Encrypted ciphertext:" << endl << actual_string << endl;
//Decrypt, output plaintext
actual_string = AES::decrypt(actual_string);
cout << "Decrypted plaintext:" << endl << actual_string << endl;
cout << "Press any key to continue" << endl;
cin.ignore();
return;
};

//3
void duoExample() {
string input;
cout << "Please input a string" << endl;
cin.ignore();
cin >> input;

DUO::init("82468224");

input = DUO::encrypt(input/*, encryption::encdec::FLAGS::no_bloat, encryption::encdec::FLAGS::no_rand_pattern*/);

cout << "Encrypted Result: " << endl << input << endl << "Decrypted Result: " << endl;

input = DUO::decrypt(input);

cout << input << endl;
};

//4
void textFromFileExample() {
string input;
cout << "Please input the path to the file: " << endl;
cin.ignore();
cin >> input;
if (!AES::encryptFF(input)) {return;}; //returns false if path is bad
ifstream readfile(input);
string line;
cout << "Encrypted: " << endl;
while ( getline (readfile,line) ) {
cout << line << endl;
};
readfile.close();
if (!AES::decryptFF(input)) {return;};
readfile.open(input);
cout << "Decrypted: " << endl;
while ( getline (readfile,line) ) {
cout << line << endl;
};
readfile.close();
};

//5
void wholeFileEncryptionExample() {
string input;
cout << "Please input the path to the file: " << endl;
cin.ignore();
cin >> input;
if (!AES::encryptFile(input)) {return;}; //returns false if path is bad

string line;
cout << "Please input new path to .aesenc file" << endl;
cin.ignore();
string newInput;
cin >> newInput;
cout << "Encrypted: " << endl;

ifstream readfile(newInput);
while ( getline (readfile,line) ) {
cout << line << endl;
};
readfile.close();


if (!AES::decryptFile(newInput)) {return;};
readfile.open(newInput);
cout << "Decrypted: " << endl;
while ( getline (readfile,line) ) {
cout << line << endl;
};
readfile.close();
};

string text[7] = {"Thank you for using my Encryption/Decryption header.\nPlease visit my Github at ArthurF23\nVersion " + VERSION::ver + "\n\n##########################\n" + "\nWhich example would you like to use?", "encdec = 1", "AES = 2", "DUO = 3", "AES text from file = 4", "AES whole file encryption = 5", "Please input the cooresponding number to your desired example"};

int main() {
cout << "Thank you for using my Encryption/Decryption header. \n Please visit my Github at ArthurF23" << endl << "Version: " << VERSION::ver;
cout << " Which example would you like to use?" << endl << "1: simple example" << endl << "2: AES example" << endl << "1, 2, or 3" << endl;
cout << text[0] << endl;
cout << text[1] << endl;
cout << text[2] << endl;
cout << text[3] << endl;
cout << text[4] << endl;
cout << text[5] << endl;
cout << text[6] << endl;
char input;
cin >> input;
switch (input) {
case '1':
encdec::start_example();
encdecExample();
break;
case '2':
AES::start_example();
aesExample();
break;
case '3':
DUO::example();
duoExample();
break;

case '4':
textFromFileExample();
break;
case '5':
wholeFileEncryptionExample();
break;

default:
cout << "Err please input 1, 2, or 3" << endl;
return main();
Expand Down

0 comments on commit 682321b

Please sign in to comment.