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