Refractoring

This commit is contained in:
amazing-username
2017-06-23 18:01:42 -05:00
parent d82b286ec9
commit 6c1f92ea30
14 changed files with 254 additions and 103 deletions
+4 -1
View File
@@ -4,9 +4,12 @@ Cryption::Cryption(const std::string& message)
{ this->message = message; }
/**
void Cryption::setMessage(const std::string& message)
{ this->message = message; }
*/
/**
std::string Cryption::getMessage() const
{ return message; }
*/
+2 -3
View File
@@ -10,9 +10,8 @@ public:
~Cryption() = default;
Cryption(const std::string&);
std::string getMessage() const;
protected:
void setMessage(const std::string&);
virtual void setMessage(const std::string&) = 0;
virtual std::string getMessage() const = 0;
std::string message;
};
+18 -5
View File
@@ -2,30 +2,32 @@
#include<fstream>
#include<sstream>
#include<ios>
#include<string>
#include<vector>
#include"Decryption.h"
#include"GenerateKeys.h"
#include"KeyRetrieval.h"
Decryption::Decryption(const std::string& message)
{
this->message = message;
setupMap();
decryptMessage();
}
void Decryption::setDecryptedMessage(const std::string& message)
{ this->decryptedMessage = message; }
void Decryption::setMessage(const std::string& message)
{ this->message = message; }
void Decryption::decryptMessage()
{
GenerateKeys gk{};
std::fstream ioEvent{};
ioEvent.open("encryptedFile.txt", std::ios::in);
std::string messageToBeDecrypted{}, decryptedMessage{};
while(ioEvent >> messageToBeDecrypted)
while(ioEvent >> messageToBeDecrypted);
ioEvent.close();
@@ -35,7 +37,7 @@ void Decryption::decryptMessage()
for (auto index = 0; index!=5; ++index)
cryptedKey[index] = (messageToBeDecrypted.at(indexOfChar + index));
decryptedMessage += gk.decryptedCharacters[cstringToString(cryptedKey, 5)];
decryptedMessage += decryptedCharacters[cstringToString(cryptedKey, 5)];
}
setDecryptedMessage(decryptedMessage);
}
@@ -43,7 +45,18 @@ void Decryption::decryptMessage()
std::string Decryption::getDecryptedMessage() const
{ return decryptedMessage; }
std::string Decryption::getMessage() const
{ return message; }
void Decryption::setupMap()
{
KeyRetrieval kr{};
std::vector<std::string> k{kr.keyStructure()};
std::vector<int> c{kr.codeCharacterStructure()};
for (auto index = 0; index!=c.size(); ++index)
decryptedCharacters[k[index]] = c[index];
}
std::string Decryption::cstringToString(char tmp[], const int& SIZE)
{
std::string tmpString{};
+5 -2
View File
@@ -3,7 +3,7 @@
#include<fstream>
#include<string>
#include<map>
#include"Cryption.h"
class Decryption : public Cryption
@@ -14,13 +14,16 @@ public:
Decryption(const std::string&);
void setDecryptedMessage(const std::string&);
void setMessage(const std::string&) override;
void decryptMessage();
std::string getDecryptedMessage() const;
std::string getMessage() const override;
private:
void setupMap();
std::string cstringToString(char[], const int&);
std::string decryptedMessage;
std::map<std::string, char> decryptedCharacters;
};
#endif
+2 -1
View File
@@ -9,11 +9,12 @@ QT += widgets
INCLUDEPATH += .
# Input
HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h
HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h KeyRetrieval.h
SOURCES += Decryption.cpp \
Encryption.cpp \
Cryption.cpp \
KeyManagementWindow.cpp \
KeyRetrieval.cpp \
GenerateKeys.cpp \
Main.cpp \
MessagingControls.cpp
+19 -3
View File
@@ -5,31 +5,47 @@
#include"Encryption.h"
#include"GenerateKeys.h"
#include"KeyRetrieval.h"
Encryption::Encryption(const std::string& message)
{
this->message = message;
setupMap();
encryptMessage();
}
void Encryption::setEncryptedMessage(const std::string& encryptedMessage)
{ this->encryptedMessage = encryptedMessage; }
void Encryption::setMessage(const std::string& message)
{ this->message = message; }
void Encryption::encryptMessage()
{
GenerateKeys gk{};
std::fstream ioEvent{};
ioEvent.open("encryptedFile.txt", std::ios::out);
for (auto indexOfString = 0u; indexOfString!=message.size(); ++indexOfString)
{
ioEvent << gk.encryptedCharacters[message.at(indexOfString)];
encryptedMessage += (gk.encryptedCharacters[message.at(indexOfString)]);
ioEvent << encryptedCharacters[message.at(indexOfString)];
encryptedMessage += encryptedCharacters[message.at(indexOfString)];
}
this->encryptedMessage = encryptedMessage;
ioEvent.close();
}
void Encryption::setupMap()
{
KeyRetrieval kr{};
std::vector<std::string> k{kr.keyStructure()};
std::vector<int> c{kr.codeCharacterStructure()};
for (auto index = 0; index!=k.size(); ++index)
encryptedCharacters[c[index]] = k[index];
}
std::string Encryption::getEncryptedMessage() const
{ return encryptedMessage; }
std::string Encryption::getMessage() const
{ return message; }
std::map<char, std::string> Encryption::encryptedCharactersStructure() { return encryptedCharacters; }
+6 -1
View File
@@ -3,7 +3,7 @@
#include<fstream>
#include<string>
#include<map>
#include"Cryption.h"
class Encryption : public Cryption
@@ -14,11 +14,16 @@ public:
Encryption(const std::string&);
void setEncryptedMessage(const std::string&);
void setMessage(const std::string&) override;
void encryptMessage();
std::string getEncryptedMessage() const;
std::string getMessage() const override;
std::map<char, std::string> encryptedCharactersStructure();
private:
void setupMap();
std::string encryptedMessage;
std::map<char, std::string> encryptedCharacters;
};
#endif
+12 -14
View File
@@ -31,8 +31,6 @@ void GenerateKeys::populateDecryptedValues()
tmpKey.assign("");
}
readKeys.close();
decryptedCharacters["#2a9u"] = (char) 32;
decryptedCharacters["6*dw4"] = (char) 10;
}
void GenerateKeys::populateEncryptedValues()
{
@@ -46,17 +44,14 @@ void GenerateKeys::populateEncryptedValues()
tmpKey.assign("");
}
readKeys.close();
encryptedCharacters[(char) 32] = "#2a9u";
encryptedCharacters[(char) 10] = "6*dw4";
}
void GenerateKeys::populateSymbols()
{
symbols.push_back(33);
addRange(symbols, 35, 38);
addRange(symbols, 40, 46);
addRange(symbols, 10, 10);
addRange(symbols, 32, 47);
addRange(symbols, 58, 64);
addRange(symbols, 93, 96);
addRange(symbols, 91, 96);
addRange(symbols, 123, 126);
}
void GenerateKeys::populateNumbers()
@@ -94,15 +89,15 @@ void GenerateKeys::generateKey(std::string& key, const int& SIZE)
std::stringstream charToString;
for (auto index = 0; index!=SIZE; ++index)
charToString << ky[index];
while (charToString>>key)
charToString >> key;
while (charToString>>key);
}
void GenerateKeys::keyMove()
{
for (auto index = 0; index!=totalCharacters; ++index)
for (auto index = 0; index!=allCharacters.size(); ++index)
{
std::string tmpKey{};
generateKey(tmpKey, keySize);
std::cout << "Generated Key: " << tmpKey << std::endl;
keys.push_back(tmpKey);
}
if (isThereRepetition())
@@ -120,11 +115,11 @@ void GenerateKeys::print(const std::vector<int>& ch)
}
bool GenerateKeys::isThereRepetition()
{
for (auto index = 0; index!=totalCharacters; ++index)
for (auto index = 0; index!=allCharacters.size(); ++index)
{
std::string chosenOne{keys[index]};
auto repetition{0};
for (auto innerIndex = 0; innerIndex!=totalCharacters; ++innerIndex)
for (auto innerIndex = 0; innerIndex!=allCharacters.size(); ++innerIndex)
if (chosenOne == keys[innerIndex])
++repetition;
if (repetition > 1)
@@ -147,6 +142,9 @@ void GenerateKeys::keyDump()
char GenerateKeys::getChar(std::vector<int>& ch)
{
auto hc = ch[rand() % ch.size()];
char hc = ch[rand() % ch.size()];
while (hc==32 || hc==10)
hc = getChar(ch);
return hc;
}
-3
View File
@@ -19,7 +19,6 @@ public:
friend class Encryption;
friend class Decryption;
friend class KeyManagementWindow;
private:
void populateSymbols();
@@ -46,8 +45,6 @@ private:
std::string defaultKeyFileName{"default_keys.txt"};
//KeySize show equal the value in key
const int keySize{5};
const int totalCharacters{63};
char key[5]{};
};
#endif
+10 -7
View File
@@ -1,5 +1,7 @@
#include"KeyManagementWindow.h"
#include"Encryption.h"
#include"GenerateKeys.h"
#include"KeyRetrieval.h"
#include<QtWidgets>
#include<QString>
@@ -13,8 +15,6 @@
KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent)
{
GenerateKeys gk{};
comboBoxOfKeys = unique_ptr<QComboBox>{new QComboBox{}};
valueOfKey = unique_ptr<QLineEdit>{new QLineEdit{}};
generateNewDefaultKeys = unique_ptr<QPushButton>{new QPushButton(tr("Generate New Default Key"))};
@@ -46,18 +46,21 @@ KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent)
void KeyManagementWindow::setContentsOfComboBox()
{
GenerateKeys gk;
KeyRetrieval kr{};
std::vector<int> c{kr.codeCharacterStructure()};
for (auto index = 0u; index!=gk.allCharacters.size(); ++index)
for (auto index = 0u; index!=c.size(); ++index)
{
std::string ch{static_cast<char>(gk.allCharacters[index])};
std::string ch{static_cast<char>(c[index])};
QString bl = QString::fromStdString(ch);
comboBoxOfKeys.get()->addItem(bl);
}
}
void KeyManagementWindow::test()
{
GenerateKeys gk{};
std::string emp{"d"};
Encryption ec{emp};
std::map<char, std::string> encrypted{ec.encryptedCharactersStructure()};
QString bo{comboBoxOfKeys.get()->currentText()};
std::string k{bo.toStdString()};
char f{};
@@ -66,7 +69,7 @@ void KeyManagementWindow::test()
lazy << k;
lazy >> f;
std::string value{gk.encryptedCharacters[f]};
std::string value{encrypted[f]};
QString v{QString::fromStdString(value)};
valueOfKey.get()->setText(v);
}
+52
View File
@@ -0,0 +1,52 @@
#include<fstream>
#include<iostream>
#include<ios>
#include"KeyRetrieval.h"
KeyRetrieval::KeyRetrieval()
{
retrieveKey();
addToList();
}
void KeyRetrieval::retrieveKey()
{
std::fstream readKeys{};
readKeys.open(fileName.c_str());
char keyChar[keySize];
while (!readKeys.eof())
{
readKeys.getline(keyChar, '\n');
if (keyChar[0]!='\0')
{
keys.push_back(keyChar);
}
}
}
void KeyRetrieval::addToList()
{
addRange(10, 10);
addRange(32, 47);
addRange(58, 64);
addRange(91, 96);
addRange(123, 126);
addRange(48, 57);
addRange(65, 90);
addRange(97, 122);
}
void KeyRetrieval::addRange(int from, const int to)
{
for (; from<=to; ++from)
codeCharacter.push_back(from);
}
std::vector<std::string> KeyRetrieval::keyStructure()
{
return keys;
}
std::vector<int> KeyRetrieval::codeCharacterStructure()
{
return codeCharacter;
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef KEYRETRIEVAL_H
#define KEYRETRIEVAL_H
#include<vector>
#include<string>
#include<list>
class KeyRetrieval
{
public:
KeyRetrieval();
~KeyRetrieval() = default;
void retrieveKey();
void addToList();
void addRange(int, const int);
std::vector<std::string> keyStructure();
std::vector<int> codeCharacterStructure();
private:
std::string fileName{"default_keys.txt"};
std::vector<std::string> keys;
std::vector<char> characters;
std::vector<int> codeCharacter;
int keySize{5};
};
#endif
+1
View File
@@ -7,6 +7,7 @@
#include"KeyManagementWindow.h"
#include"Encryption.h"
#include"Decryption.h"
#include"KeyRetrieval.h"
MessagingControls::MessagingControls()
{
+96 -63
View File
@@ -1,63 +1,96 @@
sn{31
ac`13
iu?82
aw*45
sw$84
gx:27
nt]22
ka:00
bn;69
cq*82
xa=77
yn`54
qm.96
tp$39
ok#60
mm<43
xj^90
oa=57
yx&98
be|11
mi*76
yj+43
en]97
yq?07
qg`77
yg&37
kf|26
ai-54
il,80
bp;07
di>95
zx*10
sj.81
yc`49
aw,25
in&73
du$50
cl~92
kp<89
rg^60
eu`66
el@80
ap]55
qr$02
aw.98
zq;91
fb_62
oc(50
rm)77
eg]20
lu~74
uv+04
vj_75
vt!80
xf+13
uf<32
xc;16
qj=91
ly!49
ic=46
dp,74
rp?04
ar;46
nW(53
bm-91
CD=93
Ow{26
HI<92
SC>75
Jm)89
rx)12
Bl<94
FS{50
By(06
dy/61
xp#76
Re$37
MP-54
fW-30
Pk}88
QH<49
kU}89
sq#49
bu_87
JJ-72
wM\06
tB<49
MV(77
bl|97
ns$65
qf^48
fA>39
Ws%50
Bc:85
qH.59
sA_10
pQ[44
eH!31
vF#21
No^74
pq,97
jx}83
zY|64
bH-60
QC)89
dt%43
GD%92
Fc/59
iq&22
Yt$54
gD!89
Ta{02
HO(11
qK&06
sG|86
qm)72
Ag;29
ny}13
zl+34
wP$19
wb~38
aD}64
Uu=62
cD`69
eT_82
Xh{02
HW*32
xX&91
YX,07
dQ>04
WA}96
Ju)22
ci"52
bU]83
me-09
dr]25
iA:93
oG*02
fM&51
vI`14
uv+22
OY@28
Ul]88
mU#38
HZ<80
Cf-89
gG)79
iP.37
ZU?52
ML%79
Yk%10
ow\08
oO{79
PL%83
wp>89
jn[92
HD/99
wd|57
My*99