diff --git a/Cryption.cpp b/Cryption.cpp index 855b756..a7559d6 100644 --- a/Cryption.cpp +++ b/Cryption.cpp @@ -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; } +*/ diff --git a/Cryption.h b/Cryption.h index b7b1946..4125337 100644 --- a/Cryption.h +++ b/Cryption.h @@ -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; }; diff --git a/Decryption.cpp b/Decryption.cpp index 4f504dc..369f68f 100644 --- a/Decryption.cpp +++ b/Decryption.cpp @@ -2,30 +2,32 @@ #include #include #include -#include +#include #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 k{kr.keyStructure()}; + std::vector 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{}; diff --git a/Decryption.h b/Decryption.h index 5c34464..50afcf0 100644 --- a/Decryption.h +++ b/Decryption.h @@ -3,7 +3,7 @@ #include #include - +#include #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 decryptedCharacters; }; #endif diff --git a/EncryptedMessaging.pro b/EncryptedMessaging.pro index 8a20f2c..b9b96f7 100644 --- a/EncryptedMessaging.pro +++ b/EncryptedMessaging.pro @@ -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 diff --git a/Encryption.cpp b/Encryption.cpp index c7073a3..c16d15f 100644 --- a/Encryption.cpp +++ b/Encryption.cpp @@ -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 k{kr.keyStructure()}; + std::vector 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 Encryption::encryptedCharactersStructure() { return encryptedCharacters; } diff --git a/Encryption.h b/Encryption.h index 79d9d47..a478480 100644 --- a/Encryption.h +++ b/Encryption.h @@ -3,7 +3,7 @@ #include #include - +#include #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 encryptedCharactersStructure(); private: + void setupMap(); std::string encryptedMessage; + std::map encryptedCharacters; }; #endif diff --git a/GenerateKeys.cpp b/GenerateKeys.cpp index abec39b..4a79ada 100644 --- a/GenerateKeys.cpp +++ b/GenerateKeys.cpp @@ -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& 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& ch) { - auto hc = ch[rand() % ch.size()]; + char hc = ch[rand() % ch.size()]; + while (hc==32 || hc==10) + hc = getChar(ch); + return hc; } diff --git a/GenerateKeys.h b/GenerateKeys.h index dd8f023..d68092e 100644 --- a/GenerateKeys.h +++ b/GenerateKeys.h @@ -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 diff --git a/KeyManagementWindow.cpp b/KeyManagementWindow.cpp index 825ad88..d564a9d 100644 --- a/KeyManagementWindow.cpp +++ b/KeyManagementWindow.cpp @@ -1,5 +1,7 @@ #include"KeyManagementWindow.h" +#include"Encryption.h" #include"GenerateKeys.h" +#include"KeyRetrieval.h" #include #include @@ -13,8 +15,6 @@ KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent) { - GenerateKeys gk{}; - comboBoxOfKeys = unique_ptr{new QComboBox{}}; valueOfKey = unique_ptr{new QLineEdit{}}; generateNewDefaultKeys = unique_ptr{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 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(gk.allCharacters[index])}; + std::string ch{static_cast(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 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); } diff --git a/KeyRetrieval.cpp b/KeyRetrieval.cpp new file mode 100644 index 0000000..03bdc96 --- /dev/null +++ b/KeyRetrieval.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#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 KeyRetrieval::keyStructure() +{ + return keys; +} +std::vector KeyRetrieval::codeCharacterStructure() +{ + return codeCharacter; +} diff --git a/KeyRetrieval.h b/KeyRetrieval.h new file mode 100644 index 0000000..8031f18 --- /dev/null +++ b/KeyRetrieval.h @@ -0,0 +1,27 @@ +#ifndef KEYRETRIEVAL_H +#define KEYRETRIEVAL_H + +#include +#include +#include + +class KeyRetrieval +{ +public: + KeyRetrieval(); + ~KeyRetrieval() = default; + + void retrieveKey(); + void addToList(); + void addRange(int, const int); + + std::vector keyStructure(); + std::vector codeCharacterStructure(); +private: + std::string fileName{"default_keys.txt"}; + std::vector keys; + std::vector characters; + std::vector codeCharacter; + int keySize{5}; +}; +#endif diff --git a/MessagingControls.cpp b/MessagingControls.cpp index 07c0949..314928c 100644 --- a/MessagingControls.cpp +++ b/MessagingControls.cpp @@ -7,6 +7,7 @@ #include"KeyManagementWindow.h" #include"Encryption.h" #include"Decryption.h" +#include"KeyRetrieval.h" MessagingControls::MessagingControls() { diff --git a/default_keys.txt b/default_keys.txt index 61de8b0..fa51902 100644 --- a/default_keys.txt +++ b/default_keys.txt @@ -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