From 353a1dfef16fc17b49f9d2b5cadeada2f1bd747c Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sat, 12 Aug 2017 17:02:43 -0500 Subject: [PATCH] Solution to issue #6 --- Encryption.cpp | 44 ++++++++++++++++++++++--- Encryption.h | 8 +++-- FileNameRetrieval.cpp | 12 +++++-- FileNameRetrieval.h | 3 ++ GeneratePasswordFileName.cpp | 55 +++++++++++++++++++++++++++++++ GeneratePasswordFileName.h | 15 +++++++++ KeyManagementWindow.cpp | 23 +++++++++++-- KeyManagementWindow.h | 2 +- MainWindow.cpp | 64 ++++++++++++++++++------------------ MainWindow.h | 8 ++--- 10 files changed, 186 insertions(+), 48 deletions(-) create mode 100644 GeneratePasswordFileName.cpp create mode 100644 GeneratePasswordFileName.h diff --git a/Encryption.cpp b/Encryption.cpp index afe62fb..f2761e1 100644 --- a/Encryption.cpp +++ b/Encryption.cpp @@ -4,8 +4,12 @@ #include #include"Encryption.h" #include"GenerateKeys.h" +#include"GeneratePasswordFileName.h" #include"KeyRetrieval.h" +#include"FileNameRetrieval.h" +#include"FolderStructure.h" +/** Encryption::Encryption(const std::string& message) { this->message = message; @@ -13,14 +17,21 @@ Encryption::Encryption(const std::string& message) encryptMessage(); } - +*/ +Encryption::Encryption(const std::string& message, const std::string& keyPath) +{ + this->message = message; + setupMap(keyPath); + configurePasswordFileName(); + encryptMessage(); +} void Encryption::setEncryptedMessage(const std::string& encryptedMessage) { this->encryptedMessage = encryptedMessage; } void Encryption::setMessage(const std::string& message) { this->message = message; } void Encryption::encryptMessage() { std::fstream ioEvent{}; - ioEvent.open("encryptedFile.txt", std::ios::out); + ioEvent.open(passwordFileName, std::ios::out); for (auto indexOfString = 0u; indexOfString!=message.size(); ++indexOfString) { @@ -32,15 +43,40 @@ void Encryption::encryptMessage() } -void Encryption::setupMap() +void Encryption::setupMap(const std::string& keyPath) { - KeyRetrieval kr{}; + KeyRetrieval kr{keyPath}; std::vector k{kr.keyStructure()}; std::vector c{kr.codeCharacterStructure()}; for (auto index = 0u; index!=k.size(); ++index) encryptedCharacters[c[index]] = k[index]; } +void Encryption::configurePasswordFileName() +{ + for (auto passwordNameDoesNotExist = true; passwordNameDoesNotExist; passwordNameDoesNotExist = repetitive()) + { + GeneratePasswordFileName gp{}; + passwordFileName.assign(gp.passwordFileNameString()); + } + std::string tmpString{passwordFileName}; + passwordFileName.assign(FolderStructure::passwordDirectory); + passwordFileName.append(tmpString); +} +bool Encryption::repetitive() +{ + passwordFileName.append(".txt"); + FileNameRetrieval fnr; + fnr.retrievePasswordNames(); + std::vector createdPasswordNames{fnr.passwordNameContainer()}; + auto count = 0; + for (auto cPNBegin = createdPasswordNames.begin(); cPNBegin!=createdPasswordNames.end(); ++cPNBegin) + { + if (passwordFileName.compare(*cPNBegin)==0) ++count; + if (count>0) return true; + } + return false; +} 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 21c0df4..6697abe 100644 --- a/Encryption.h +++ b/Encryption.h @@ -10,7 +10,8 @@ class Encryption : public Cryption public: Encryption() = default; ~Encryption() = default; - Encryption(const std::string&); + //Encryption(const std::string&); + explicit Encryption(const std::string&, const std::string&); void setEncryptedMessage(const std::string&); void setMessage(const std::string&) override; @@ -20,8 +21,11 @@ public: std::string getMessage() const override; std::map encryptedCharactersStructure(); private: - void setupMap(); + void setupMap(const std::string&); + void configurePasswordFileName(); + bool repetitive(); std::string encryptedMessage; + std::string passwordFileName; std::map encryptedCharacters; }; #endif diff --git a/FileNameRetrieval.cpp b/FileNameRetrieval.cpp index a3e4042..3e5f319 100644 --- a/FileNameRetrieval.cpp +++ b/FileNameRetrieval.cpp @@ -6,6 +6,7 @@ FileNameRetrieval::FileNameRetrieval() { } std::vector FileNameRetrieval::fileNameContainer() const { return fileNames; } +std::vector FileNameRetrieval::passwordNameContainer() const { return passwordNames; } void FileNameRetrieval::retrieveFileNames() { boost::filesystem::path directory(FolderStructure::keyDirectory); @@ -14,6 +15,13 @@ void FileNameRetrieval::retrieveFileNames() for (; beg!=end; ++beg) if (beg->path().extension()==".txt") fileNames.push_back(beg->path().filename().string()); - for (auto fl: fileNames) - std::cout<< fl<path().extension()==".txt") + passwordNames.push_back(beg->path().filename().string()); } diff --git a/FileNameRetrieval.h b/FileNameRetrieval.h index 81fd255..21e1f66 100644 --- a/FileNameRetrieval.h +++ b/FileNameRetrieval.h @@ -10,8 +10,11 @@ public: FileNameRetrieval(); void retrieveFileNames(); + void retrievePasswordNames(); std::vector fileNameContainer() const; + std::vector passwordNameContainer() const; private: std::vector fileNames; + std::vector passwordNames; }; #endif diff --git a/GeneratePasswordFileName.cpp b/GeneratePasswordFileName.cpp new file mode 100644 index 0000000..848a542 --- /dev/null +++ b/GeneratePasswordFileName.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include"GeneratePasswordFileName.h" + +GeneratePasswordFileName::GeneratePasswordFileName() +{ + srand(time(0)); + generatedFileName(); +} +void GeneratePasswordFileName::generatedFileName() +{ + time_t epochTime = time(0); + tm* currentTime = localtime(&epochTime); + + auto year = 1900 + currentTime->tm_year; + auto month = 1 + currentTime->tm_mon; + auto dayOfMonth = currentTime->tm_mday; + + if (month<10 && dayOfMonth<10) + { + filename.assign(std::to_string(year)); + filename.append("0"+std::to_string(month)); + filename.append("0"+std::to_string(dayOfMonth)); + } + else if (month<10 && dayOfMonth>=10) + { + filename.assign(std::to_string(year)); + filename.append("0"+std::to_string(month)); + filename.append(std::to_string(dayOfMonth)); + } + else if (month>=10 && dayOfMonth<10) + { + filename.assign(std::to_string(year)); + filename.append(std::to_string(month)); + filename.append("0"+std::to_string(dayOfMonth)); + } + else + { + filename.assign(std::to_string(year)); + filename.append(std::to_string(month)); + filename.append(std::to_string(dayOfMonth)); + } + for (auto index = 0; index!=8; ++index) + { + if (index==0) filename.append("_"); + else + { + auto randomInteger = rand() % 10; + filename.append(std::to_string(randomInteger)); + } + } +} + +std::string GeneratePasswordFileName::passwordFileNameString() const { return filename; } diff --git a/GeneratePasswordFileName.h b/GeneratePasswordFileName.h new file mode 100644 index 0000000..076807c --- /dev/null +++ b/GeneratePasswordFileName.h @@ -0,0 +1,15 @@ +#ifndef GENERATEPASSWORDFILENAME_H_ +#define GENERATEPASSWORDFILENAME_H_ + +#include + +class GeneratePasswordFileName +{ +public: + GeneratePasswordFileName(); + std::string passwordFileNameString() const; +private: + void generatedFileName(); + std::string filename; +}; +#endif diff --git a/KeyManagementWindow.cpp b/KeyManagementWindow.cpp index 7bb774a..36bf9e5 100644 --- a/KeyManagementWindow.cpp +++ b/KeyManagementWindow.cpp @@ -55,8 +55,25 @@ void KeyManagementWindow::setContentOfKeyView() { if (column==0) { - std::string val{*asciiKeysIter++}; - QString s{val.c_str()}; + std::string val; + QString s; + switch (*asciiKeysIter) + { + case 10: + val.assign("New Line"); + s.append(val.c_str()); + ++asciiKeysIter; + break; + case 32: + val.assign("White space"); + s.append(val.c_str()); + ++asciiKeysIter; + break; + default: + val.assign(std::string{static_cast(*asciiKeysIter++)}); + s.append(val.c_str()); + break; + } elementView.get()->setItem(row, column, new QTableWidgetItem(s)); } else @@ -126,6 +143,7 @@ void KeyManagementWindow::connections() QObject::connect(closeButton.get(), SIGNAL(clicked()), this, SLOT(exitApplication())); QObject::connect(actionButton.get(), SIGNAL(clicked()), this, SLOT(setContentOfKeyView())); } +/** void KeyManagementWindow::test() { std::string emp{"d"}; @@ -142,6 +160,7 @@ void KeyManagementWindow::test() std::string value{encrypted[f]}; QString v{QString::fromStdString(value)}; } +*/ void KeyManagementWindow::generation() { GenerateKeys gk{}; diff --git a/KeyManagementWindow.h b/KeyManagementWindow.h index 908b274..964dd9c 100644 --- a/KeyManagementWindow.h +++ b/KeyManagementWindow.h @@ -14,7 +14,7 @@ public: KeyManagementWindow(QWidget* parent = 0); ~KeyManagementWindow() = default; private slots: - void test(); + //void test(); void generation(); void exitApplication(); void setContentOfKeyView(); diff --git a/MainWindow.cpp b/MainWindow.cpp index bcad0eb..f49578f 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -4,6 +4,8 @@ #include"Encryption.h" #include"Decryption.h" #include"KeyRetrieval.h" +#include"FileNameRetrieval.h" +#include"FolderStructure.h" MainWindow::MainWindow() { @@ -38,6 +40,7 @@ void MainWindow::setupMainWindow() addDockWidget(Qt::LeftDockWidgetArea, cryptionArea.get()); createMenus(); + setupContentOfComboBox(); setWindowTitle("Encryption Decryption Messaging"); setFixedHeight(windowHeight); @@ -45,6 +48,19 @@ void MainWindow::setupMainWindow() actionButton.get()->setEnabled(false); connections(); } +void MainWindow::setupContentOfComboBox() +{ + selectionBox.get()->clear(); + FileNameRetrieval fnr; + fnr.retrieveFileNames(); + std::vector fn{fnr.fileNameContainer()}; + + for (auto fle: fn) + { + QString bl{QString::fromStdString(fle)}; + selectionBox.get()->addItem(bl); + } +} void MainWindow::createMenus() { fileMenu = unique_ptr{menuBar()->addMenu(tr("File"))}; @@ -67,41 +83,20 @@ void MainWindow::connections() QObject::connect(closeApplication.get(), SIGNAL(triggered()), this, SLOT(exitApplication())); QObject::connect(keyEdit.get(), SIGNAL(triggered()), this, SLOT(keyManagementWindow())); QObject::connect(passwordManage.get(), SIGNAL(triggered()), this, SLOT(passwordManageWindow())); - QObject::connect(actionButton.get(), SIGNAL(clicked()), this, SLOT(test())); + QObject::connect(actionButton.get(), SIGNAL(clicked()), this, SLOT(encryptPassword())); + QObject::connect(textForCryption.get(), SIGNAL(textChanged()), this, SLOT(activateButton())); } -void MainWindow::changeCryptionType() +void MainWindow::encryptPassword() { - /** - (cryptionChoice) ? cryptionSwitch->setText("decrypt chosen") : cryptionSwitch->setText("encrypt chosen"); - cryptionChoice = (cryptionChoice) ? false : true; - */ -} -void MainWindow::determineCryption() -{ - /** - if (cryptionChoice) - { - Encryption ec{grabCryptionText()}; - - textForCryption.get()->clear(); - textForCryption.get()->setText(QString::fromStdString(ec.getEncryptedMessage())); - - cryptionSwitch.get()->setText("decrypt chosen"); - cryptionChoice = false; - } - else - { - Decryption dc{grabCryptionText()}; - - textForCryption.get()->clear(); - textForCryption.get()->setText(QString::fromStdString(dc.getDecryptedMessage())); - - cryptionSwitch.get()->setText("encrypt chosen"); - cryptionChoice = true; - } - */ + QString passwordToEncrypt{textForCryption.get()->toPlainText()}, keyForEncryption{selectionBox.get()->currentText()}; + auto passwordToEncryptString = passwordToEncrypt.toStdString(), keyForEncryptionString = FolderStructure::keyDirectory+keyForEncryption.toStdString(); + std::cout<<"Will encrypt \""<show(); } void MainWindow::passwordManageWindow() { ph.get()->show(); } @@ -114,4 +109,9 @@ std::string MainWindow::grabCryptionText() return placeHolder.toStdString(); } -void MainWindow::test() { std::cout<<"Action button Works"<toPlainText()}; + if (content.size()>0 && selectionBox.get()->count()>0) actionButton.get()->setEnabled(true); + else actionButton.get()->setEnabled(false); +} diff --git a/MainWindow.h b/MainWindow.h index 51ba603..97c7eb0 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -27,14 +27,14 @@ public: signals: private slots: - void changeCryptionType(); - void determineCryption(); + void encryptPassword(); void keyManagementWindow(); void passwordManageWindow(); void exitApplication(); - void test(); + void activateButton(); private: void setupMainWindow(); + void setupContentOfComboBox(); void createMenus(); void connections(); @@ -59,7 +59,5 @@ private: unique_ptr ph; std::string grabCryptionText(); - - bool cryptionChoice; }; #endif