Solution to issue #6

This commit is contained in:
amazing-username
2017-08-12 17:02:43 -05:00
parent c1d17799cd
commit 353a1dfef1
10 changed files with 186 additions and 48 deletions
+40 -4
View File
@@ -4,8 +4,12 @@
#include<string> #include<string>
#include"Encryption.h" #include"Encryption.h"
#include"GenerateKeys.h" #include"GenerateKeys.h"
#include"GeneratePasswordFileName.h"
#include"KeyRetrieval.h" #include"KeyRetrieval.h"
#include"FileNameRetrieval.h"
#include"FolderStructure.h"
/**
Encryption::Encryption(const std::string& message) Encryption::Encryption(const std::string& message)
{ {
this->message = message; this->message = message;
@@ -13,14 +17,21 @@ Encryption::Encryption(const std::string& message)
encryptMessage(); 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::setEncryptedMessage(const std::string& encryptedMessage) { this->encryptedMessage = encryptedMessage; }
void Encryption::setMessage(const std::string& message) { this->message = message; } void Encryption::setMessage(const std::string& message) { this->message = message; }
void Encryption::encryptMessage() void Encryption::encryptMessage()
{ {
std::fstream ioEvent{}; std::fstream ioEvent{};
ioEvent.open("encryptedFile.txt", std::ios::out); ioEvent.open(passwordFileName, std::ios::out);
for (auto indexOfString = 0u; indexOfString!=message.size(); ++indexOfString) 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<std::string> k{kr.keyStructure()}; std::vector<std::string> k{kr.keyStructure()};
std::vector<int> c{kr.codeCharacterStructure()}; std::vector<int> c{kr.codeCharacterStructure()};
for (auto index = 0u; index!=k.size(); ++index) for (auto index = 0u; index!=k.size(); ++index)
encryptedCharacters[c[index]] = k[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<std::string> 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::getEncryptedMessage() const { return encryptedMessage; }
std::string Encryption::getMessage() const { return message; } std::string Encryption::getMessage() const { return message; }
std::map<char, std::string> Encryption::encryptedCharactersStructure() { return encryptedCharacters; } std::map<char, std::string> Encryption::encryptedCharactersStructure() { return encryptedCharacters; }
+6 -2
View File
@@ -10,7 +10,8 @@ class Encryption : public Cryption
public: public:
Encryption() = default; Encryption() = default;
~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 setEncryptedMessage(const std::string&);
void setMessage(const std::string&) override; void setMessage(const std::string&) override;
@@ -20,8 +21,11 @@ public:
std::string getMessage() const override; std::string getMessage() const override;
std::map<char, std::string> encryptedCharactersStructure(); std::map<char, std::string> encryptedCharactersStructure();
private: private:
void setupMap(); void setupMap(const std::string&);
void configurePasswordFileName();
bool repetitive();
std::string encryptedMessage; std::string encryptedMessage;
std::string passwordFileName;
std::map<char, std::string> encryptedCharacters; std::map<char, std::string> encryptedCharacters;
}; };
#endif #endif
+10 -2
View File
@@ -6,6 +6,7 @@
FileNameRetrieval::FileNameRetrieval() { } FileNameRetrieval::FileNameRetrieval() { }
std::vector<std::string> FileNameRetrieval::fileNameContainer() const { return fileNames; } std::vector<std::string> FileNameRetrieval::fileNameContainer() const { return fileNames; }
std::vector<std::string> FileNameRetrieval::passwordNameContainer() const { return passwordNames; }
void FileNameRetrieval::retrieveFileNames() void FileNameRetrieval::retrieveFileNames()
{ {
boost::filesystem::path directory(FolderStructure::keyDirectory); boost::filesystem::path directory(FolderStructure::keyDirectory);
@@ -14,6 +15,13 @@ void FileNameRetrieval::retrieveFileNames()
for (; beg!=end; ++beg) for (; beg!=end; ++beg)
if (beg->path().extension()==".txt") if (beg->path().extension()==".txt")
fileNames.push_back(beg->path().filename().string()); fileNames.push_back(beg->path().filename().string());
for (auto fl: fileNames) }
std::cout<< fl<<std::endl; void FileNameRetrieval::retrievePasswordNames()
{
boost::filesystem::path directory(FolderStructure::passwordDirectory);
boost::filesystem::directory_iterator beg(directory), end;
for (; beg!=end; ++beg)
if (beg->path().extension()==".txt")
passwordNames.push_back(beg->path().filename().string());
} }
+3
View File
@@ -10,8 +10,11 @@ public:
FileNameRetrieval(); FileNameRetrieval();
void retrieveFileNames(); void retrieveFileNames();
void retrievePasswordNames();
std::vector<std::string> fileNameContainer() const; std::vector<std::string> fileNameContainer() const;
std::vector<std::string> passwordNameContainer() const;
private: private:
std::vector<std::string> fileNames; std::vector<std::string> fileNames;
std::vector<std::string> passwordNames;
}; };
#endif #endif
+55
View File
@@ -0,0 +1,55 @@
#include<iostream>
#include<ctime>
#include<cstdlib>
#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; }
+15
View File
@@ -0,0 +1,15 @@
#ifndef GENERATEPASSWORDFILENAME_H_
#define GENERATEPASSWORDFILENAME_H_
#include<string>
class GeneratePasswordFileName
{
public:
GeneratePasswordFileName();
std::string passwordFileNameString() const;
private:
void generatedFileName();
std::string filename;
};
#endif
+21 -2
View File
@@ -55,8 +55,25 @@ void KeyManagementWindow::setContentOfKeyView()
{ {
if (column==0) if (column==0)
{ {
std::string val{*asciiKeysIter++}; std::string val;
QString s{val.c_str()}; 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<char>(*asciiKeysIter++)});
s.append(val.c_str());
break;
}
elementView.get()->setItem(row, column, new QTableWidgetItem(s)); elementView.get()->setItem(row, column, new QTableWidgetItem(s));
} }
else else
@@ -126,6 +143,7 @@ void KeyManagementWindow::connections()
QObject::connect(closeButton.get(), SIGNAL(clicked()), this, SLOT(exitApplication())); QObject::connect(closeButton.get(), SIGNAL(clicked()), this, SLOT(exitApplication()));
QObject::connect(actionButton.get(), SIGNAL(clicked()), this, SLOT(setContentOfKeyView())); QObject::connect(actionButton.get(), SIGNAL(clicked()), this, SLOT(setContentOfKeyView()));
} }
/**
void KeyManagementWindow::test() void KeyManagementWindow::test()
{ {
std::string emp{"d"}; std::string emp{"d"};
@@ -142,6 +160,7 @@ void KeyManagementWindow::test()
std::string value{encrypted[f]}; std::string value{encrypted[f]};
QString v{QString::fromStdString(value)}; QString v{QString::fromStdString(value)};
} }
*/
void KeyManagementWindow::generation() void KeyManagementWindow::generation()
{ {
GenerateKeys gk{}; GenerateKeys gk{};
+1 -1
View File
@@ -14,7 +14,7 @@ public:
KeyManagementWindow(QWidget* parent = 0); KeyManagementWindow(QWidget* parent = 0);
~KeyManagementWindow() = default; ~KeyManagementWindow() = default;
private slots: private slots:
void test(); //void test();
void generation(); void generation();
void exitApplication(); void exitApplication();
void setContentOfKeyView(); void setContentOfKeyView();
+32 -32
View File
@@ -4,6 +4,8 @@
#include"Encryption.h" #include"Encryption.h"
#include"Decryption.h" #include"Decryption.h"
#include"KeyRetrieval.h" #include"KeyRetrieval.h"
#include"FileNameRetrieval.h"
#include"FolderStructure.h"
MainWindow::MainWindow() MainWindow::MainWindow()
{ {
@@ -38,6 +40,7 @@ void MainWindow::setupMainWindow()
addDockWidget(Qt::LeftDockWidgetArea, cryptionArea.get()); addDockWidget(Qt::LeftDockWidgetArea, cryptionArea.get());
createMenus(); createMenus();
setupContentOfComboBox();
setWindowTitle("Encryption Decryption Messaging"); setWindowTitle("Encryption Decryption Messaging");
setFixedHeight(windowHeight); setFixedHeight(windowHeight);
@@ -45,6 +48,19 @@ void MainWindow::setupMainWindow()
actionButton.get()->setEnabled(false); actionButton.get()->setEnabled(false);
connections(); connections();
} }
void MainWindow::setupContentOfComboBox()
{
selectionBox.get()->clear();
FileNameRetrieval fnr;
fnr.retrieveFileNames();
std::vector<std::string> fn{fnr.fileNameContainer()};
for (auto fle: fn)
{
QString bl{QString::fromStdString(fle)};
selectionBox.get()->addItem(bl);
}
}
void MainWindow::createMenus() void MainWindow::createMenus()
{ {
fileMenu = unique_ptr<QMenu>{menuBar()->addMenu(tr("File"))}; fileMenu = unique_ptr<QMenu>{menuBar()->addMenu(tr("File"))};
@@ -67,41 +83,20 @@ void MainWindow::connections()
QObject::connect(closeApplication.get(), SIGNAL(triggered()), this, SLOT(exitApplication())); QObject::connect(closeApplication.get(), SIGNAL(triggered()), this, SLOT(exitApplication()));
QObject::connect(keyEdit.get(), SIGNAL(triggered()), this, SLOT(keyManagementWindow())); QObject::connect(keyEdit.get(), SIGNAL(triggered()), this, SLOT(keyManagementWindow()));
QObject::connect(passwordManage.get(), SIGNAL(triggered()), this, SLOT(passwordManageWindow())); 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()
{ {
/** QString passwordToEncrypt{textForCryption.get()->toPlainText()}, keyForEncryption{selectionBox.get()->currentText()};
(cryptionChoice) ? cryptionSwitch->setText("decrypt chosen") : cryptionSwitch->setText("encrypt chosen"); auto passwordToEncryptString = passwordToEncrypt.toStdString(), keyForEncryptionString = FolderStructure::keyDirectory+keyForEncryption.toStdString();
cryptionChoice = (cryptionChoice) ? false : true; std::cout<<"Will encrypt \""<<passwordToEncryptString<<"\""<<std::endl;
*/ std::cout<<"With key \""<<keyForEncryptionString<<"\""<<std::endl;
} std::cout<<"Encrypting..."<<std::endl;
void MainWindow::determineCryption() Encryption ec{passwordToEncryptString, keyForEncryptionString};
{ std::cout<<"Encrypted"<<std::endl;
/**
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;
}
*/
} }
void MainWindow::keyManagementWindow() { kh.get()->show(); } void MainWindow::keyManagementWindow() { kh.get()->show(); }
void MainWindow::passwordManageWindow() { ph.get()->show(); } void MainWindow::passwordManageWindow() { ph.get()->show(); }
@@ -114,4 +109,9 @@ std::string MainWindow::grabCryptionText()
return placeHolder.toStdString(); return placeHolder.toStdString();
} }
void MainWindow::test() { std::cout<<"Action button Works"<<std::endl; } void MainWindow::activateButton()
{
QString content{textForCryption.get()->toPlainText()};
if (content.size()>0 && selectionBox.get()->count()>0) actionButton.get()->setEnabled(true);
else actionButton.get()->setEnabled(false);
}
+3 -5
View File
@@ -27,14 +27,14 @@ public:
signals: signals:
private slots: private slots:
void changeCryptionType(); void encryptPassword();
void determineCryption();
void keyManagementWindow(); void keyManagementWindow();
void passwordManageWindow(); void passwordManageWindow();
void exitApplication(); void exitApplication();
void test(); void activateButton();
private: private:
void setupMainWindow(); void setupMainWindow();
void setupContentOfComboBox();
void createMenus(); void createMenus();
void connections(); void connections();
@@ -59,7 +59,5 @@ private:
unique_ptr<PasswordManagementWindow> ph; unique_ptr<PasswordManagementWindow> ph;
std::string grabCryptionText(); std::string grabCryptionText();
bool cryptionChoice;
}; };
#endif #endif