Key Dependent Encoding
Encryption, or Encoding, simply is the art of leaving data in
obfuscated form in order to keep it secure.
It could be as simple as replacing all characters in a text file with those
from a predetermined set in a manner that has the original text having
a direct correlation with the encrypted / encoded version.
An example of this is seen in the Base-64 Encoding System.
Encryption could also mean adding junk data to an original data
in such a way that the original data is completely defaced but can
still be comfortably extracted from the encrypted version.
Encryption processes must always be consistent: i.e. the same type of
encryption, or encoding, carried out on the same data or file must always
produce exactly the same obfuscated output.
But with key dependent encryption, every unique key produces a completely
different encoded set or obfuscated data.
This ensures better security of data since brute-forcing becomes very difficult
without knowledge of the secret key used.
Also, the longer the key used, the more useless brute-forcing becomes.
This means that different individuals or firms can employ the same encryption process,
but have their own unique secret key (Private Key) to encrypt data with.
Such encrypted data cannot be comfortably decrypted by a second firm using the
same encryption process if the first firm can keep its key secret enough.
Recurrent Sequences or Series
Remember Sequences and Series from Ordinary Level Mathematics;
Recurrent Series to be precise? They become as useful as they can be here!
Recurrent Series has the unique characteristic that all succeeding terms in
a progression are totally dependent on all preceding terms - i.e. for any
Recurrent Series, any n+1th term cannot be determined unless the
value of the nth term and its predecessors are known;
and even more true is the fact that every other term in the progression
is absolutely dependent on the 1st term of the series.
The afore property is what we will be exploiting in the C++ algorithm for encrypting
data with reference to single secret keys.
Create a new class file;
Call it SoleKeyEncryption
.
Type out the adjoining C++ code for encrypting a chunk of data
with a secret key.
Important: We obligatorily wrote a BigInteger class
for use with this section.
It is freely available on Github
or you can download it as a zip file here.
SoleKeyEncryption Header File
#pragma once
#include "BigInteger.h"
#include <string>
#include <vector>
using namespace std;
class SoleKeyEncryption
{
public:
SoleKeyEncryption();
virtual ~SoleKeyEncryption();
vector<string> encodeWord(vector<char>, vector<char>);
string decodeWord(vector<string>, vector<char>);
};
C++ code for SoleKeyEncryption Class File
#include "stdafx.h"
#include "SoleKeyEncryption.h"
SoleKeyEncryption::SoleKeyEncryption()
{
}
vector<string> SoleKeyEncryption::encodeWord(vector<char> msg, vector<char> key) {
vector<string> encryption = {};
int n, t1;
string h, Tn;
stringstream x;
for (size_t i = 0; i < msg.size(); i++) {
t1 = (int) msg[i];
h = "";
h += key[i % (key.size() - 1)];
x << hex << h;
x >> n;
x.clear();
Tn = BigInteger::division(BigInteger::subtraction(BigInteger::multiplication(
BigInteger::exponentialPower("3", n - 1), to_string(2 * t1 + 1)), "1"), "2");
encryption.push_back(BigInteger::decToHex(Tn));
}
return encryption;
}
string SoleKeyEncryption::decodeWord(vector<string> code, vector<char> key) {
string decryption = "", h, t1, Tn;
int n;
stringstream x;
for (size_t i = 0; i < code.size(); i++) {
Tn = BigInteger::hexToDec(code[i]);
h = "";
h += key[i % (key.size() - 1)];
x << hex << h;
x >> n;
x.clear();
t1 = BigInteger::division(BigInteger::subtraction(BigInteger::division(
to_string(2 * stoi(Tn) + 1), BigInteger::exponentialPower("3", n - 1)), "1"), "2");
decryption += (char)stoi(t1);
}
return decryption;
}
SoleKeyEncryption::~SoleKeyEncryption()
{
}
Main Class
#include "stdafx.h"
#include "SoleKeyEncryption.h"
#include <iostream>
using namespace std;
int main()
{
vector<char> message = { 'm', 'e', 'r', 'r', 'y', ' ', 'x', 'm', 'a', 's' };
vector<char> key = { 'A', '5', 'F', 'B', '1', '7', 'C', '4', 'D', '8' };
SoleKeyEncryption go_secure;
vector<string> encrypted = go_secure.encodeWord(message, key);
string disp = "";
for (string s : encrypted) {
disp += s + "; ";
}
string msg = "";
for (char c : message) {
msg += c;
}
cout << "Message is '" << msg << "';\nEncrypted version is " << disp << endl;
string decrypted = go_secure.decodeWord(encrypted, key);
cout << "\n\nDecrypted version is '" << decrypted << "'.";
return 0;
}