Skip to content

Commit

Permalink
Improvements to the ecryption algorithm for improved "randomness" fro…
Browse files Browse the repository at this point in the history
…m tiny key changes, macros to tweak settings.
  • Loading branch information
lewesche committed Jan 26, 2021
1 parent b6169a8 commit 7035543
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CPPFLAGS = -Wall -g -std=c++11
BIN_DIR = bin
SRC_DIR = src


all: $(BIN_DIR)/.dirstamp $(BIN_DIR)/secrets

$(BIN_DIR)/secret.o: $(SRC_DIR)/secret.cpp $(SRC_DIR)/secret.h
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ cp bin/secrets /usr/local/bin
```
Then run from anywhere with `secrets`

Before compiling with `make`, there are macros you can change within secret.h to modify the encryption/decryption algorithm in your binary. See the file for details.

## Usage
The first time it's run a file will be created in your home directory called `.secrets.txt`. This is where encrypted text and optionally unencrypted tags are stored. It might be a good idea to save a backup copy of this file.

Expand Down
46 changes: 34 additions & 12 deletions src/secret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <fstream>
#include "secret.h"
#include <math.h>

using namespace std;

Expand Down Expand Up @@ -32,28 +33,41 @@ const string& secret::get_key() const {


string secret::shift(const string& phrase, int multiplier) const {
size_t i=0;
size_t j=0;
bool seen_full_phrase = false;
bool seen_full_key = false;
string res = phrase;

// The multiplier input is pretty much arbitrary as long as
// the abs value of the multipliers used for encryption and
// decryption match, and they are opposite signs.
//
// But for some bonus security lets factor in the key size
multiplier *= key.size();
multiplier *= key.size()+1;

// And the sum of all key chars
multiplier *= strsum(key)+1;

// Some complicated math to figure out the num passes
size_t min_phrase_passes = (key.size() + static_cast<size_t>(pow(static_cast<double>(strsum(key)), static_cast<double>(key.size())))) % MAX_PASSES;
size_t min_key_passes = MAX_PASSES - min_phrase_passes; // If the above line returns a low num, this function will ensure plenty of passes.

size_t phrase_pass_count = 0, key_pass_count = 0;

size_t i=0;
size_t j=0;
bool done_phrase_passes = false;
bool done_key_passes = false;
string res = phrase;

// Make sure to iterate over the both strings entierly
while(!seen_full_phrase || !seen_full_key) {
while(!done_phrase_passes || !done_key_passes) {
if(j>=key.size()) {
j=0;
seen_full_key = true;
++key_pass_count;
if(key_pass_count >= min_key_passes)
done_key_passes = true;
}
if(i>=phrase.size()) {
i=0;
seen_full_phrase = true;
++phrase_pass_count;
if(phrase_pass_count >= min_phrase_passes)
done_phrase_passes = true;
}

res[i] = res[i] + key[j]*multiplier;
Expand All @@ -64,12 +78,20 @@ string secret::shift(const string& phrase, int multiplier) const {
}

void secret::encrypt() {
enc = shift(dec, 5);
enc = shift(dec, MULTIPLIER);
// enc_nums might be out of date if dec changed, but doesn't matter
}

void secret::decrypt() {
dec = shift(enc, -5);
dec = shift(enc, -1*MULTIPLIER);
}

char secret::strsum(string s) const {
char res = 0;
for(size_t i=0; i<s.size(); ++i) {
res += s[i];
}
return res;
}

string secret::numstr_2_charstr(const string& numstr) const {
Expand Down
14 changes: 14 additions & 0 deletions src/secret.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#ifndef SECRETS
#define SECRETS


/* ~~~ WARING ~~~
MULTIPLIER and MAX_PASSES are two constants used during encryption/decryption.
They can be changed for extra securty.
Changing either constant before compiling will result in a .secrets.txt file that cannot be read by other binaries compiled with different constants.
MAX_PASSES also effects the speed of the program, larger MAX_PASSES == slower decryption/encryption.
*/

#define MULTIPLIER 5
#define MAX_PASSES 3249


class secret {
private:
std::string key;
Expand Down Expand Up @@ -31,6 +44,7 @@ class secret {

private:
std::string shift(const std::string& phrase, int multiplier) const;
char strsum(std::string s) const;

// Converts a string of 3-char numbers (0-256) to a string of chars
std::string numstr_2_charstr(const std::string& numstr) const;
Expand Down

0 comments on commit 7035543

Please sign in to comment.