Issue #2 added feature to generate a new default key

This commit is contained in:
amazing-username
2017-04-28 16:01:22 -05:00
parent 222f0c607b
commit e38f10e759
13 changed files with 161 additions and 144 deletions
+8 -11
View File
@@ -1,21 +1,18 @@
#include"Cryption.h" #include"Cryption.h"
Cryption::Cryption() Cryption::Cryption()
{ { }
}
Cryption::~Cryption()
{ }
Cryption::Cryption(const std::string& message) 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;
}
+1 -1
View File
@@ -8,8 +8,8 @@ class Cryption
{ {
public: public:
Cryption(); Cryption();
~Cryption();
Cryption(const std::string&); Cryption(const std::string&);
//void setMessage(const std::string&);
std::string getMessage() const; std::string getMessage() const;
protected: protected:
+12 -29
View File
@@ -7,18 +7,18 @@
#include"GenerateKeys.h" #include"GenerateKeys.h"
Decryption::Decryption() Decryption::Decryption()
{ { }
}
Decryption::~Decryption()
{ }
Decryption::Decryption(const std::string& message) Decryption::Decryption(const std::string& message)
{ { this->message = message; }
this->message = message;
}
void Decryption::setDecryptedMessage(const std::string& message) void Decryption::setDecryptedMessage(const std::string& message)
{ { this->decryptedMessage = message; }
this->decryptedMessage = message;
}
void Decryption::decryptMessage() void Decryption::decryptMessage()
{ {
GenerateKeys gk{}; GenerateKeys gk{};
@@ -31,7 +31,7 @@ void Decryption::decryptMessage()
ioEvent.close(); ioEvent.close();
for (auto indexOfChar = 0; indexOfChar<messageToBeDecrypted.size(); indexOfChar+=5) for (auto indexOfChar = 0u; indexOfChar<messageToBeDecrypted.size(); indexOfChar+=5)
{ {
char cryptedKey[5]{}; char cryptedKey[5]{};
for (auto index = 0; index!=5; ++index) for (auto index = 0; index!=5; ++index)
@@ -41,36 +41,19 @@ void Decryption::decryptMessage()
} }
decryptedMessage += gk.decryptedCharacters[cstringToString(cryptedKey, 5)]; decryptedMessage += gk.decryptedCharacters[cstringToString(cryptedKey, 5)];
} }
//std::cout << "Decrypted Message: " << decryptedMessage << std::endl;
setDecryptedMessage(decryptedMessage); setDecryptedMessage(decryptedMessage);
} }
std::string Decryption::getDecryptedMessage() const std::string Decryption::getDecryptedMessage() const
{ { return decryptedMessage; }
return decryptedMessage;
}
std::string Decryption::cstringToString(char tmp[], const int& SIZE) std::string Decryption::cstringToString(char tmp[], const int& SIZE)
{ {
std::string tmpString{}; std::string tmpString{};
std::stringstream cToS{}; std::stringstream cToS{};
for (auto index = 0; index!=5; ++index) for (auto index = 0; index!=SIZE; ++index)
{
cToS << tmp[index]; cToS << tmp[index];
}
cToS >> tmpString; cToS >> tmpString;
return tmpString; return tmpString;
} }
unsigned short getTwoNumberedKey(unsigned short& number)
{
std::string wholeNumber{std::to_string(number)};
char hundred[] = {wholeNumber.at(0)};
char ten[] = {wholeNumber.at(1)};
char one[] {wholeNumber.at(2)};
unsigned short hundread, tenN, oneN;
hundread = number;
}
+1
View File
@@ -11,6 +11,7 @@ class Decryption : public Cryption
public: public:
Decryption(); Decryption();
~Decryption();
Decryption(const std::string&); Decryption(const std::string&);
void setDecryptedMessage(const std::string&); void setDecryptedMessage(const std::string&);
+9 -13
View File
@@ -6,25 +6,23 @@
#include"GenerateKeys.h" #include"GenerateKeys.h"
Encryption::Encryption() Encryption::Encryption()
{ { }
}
Encryption::~Encryption()
{ }
Encryption::Encryption(const std::string& message) Encryption::Encryption(const std::string& message)
{ { this->message = message; }
this->message = message;
}
void Encryption::setEncryptedMessage(const std::string& encryptedMessage) void Encryption::setEncryptedMessage(const std::string& encryptedMessage)
{ { this->encryptedMessage = encryptedMessage; }
this->encryptedMessage = encryptedMessage;
}
void Encryption::encryptMessage() void Encryption::encryptMessage()
{ {
int numberOfNewLines{0}; auto numberOfNewLines = 0;
GenerateKeys gk{}; GenerateKeys gk{};
ioEvent.open("encryptedFile.txt", std::ios::out); ioEvent.open("encryptedFile.txt", std::ios::out);
//std::cout << "String to encrypt: " << message << std::endl;
for (auto index = 0; index != message.size(); ++index) for (auto index = 0; index != message.size(); ++index)
{ {
char stringIndex = message.at(index); char stringIndex = message.at(index);
@@ -42,6 +40,4 @@ void Encryption::encryptMessage()
std::string Encryption::getEncryptedMessage() const std::string Encryption::getEncryptedMessage() const
{ { return encryptedMessage; }
return encryptedMessage;
}
+1
View File
@@ -10,6 +10,7 @@ class Encryption : public Cryption
{ {
public: public:
Encryption(); Encryption();
~Encryption();
Encryption(const std::string&); Encryption(const std::string&);
void setEncryptedMessage(const std::string&); void setEncryptedMessage(const std::string&);
+24 -13
View File
@@ -11,23 +11,25 @@ GenerateKeys::GenerateKeys()
populateNumbers(); populateNumbers();
populateLetters(); populateLetters();
for (auto index = 0; index!=symbols.size(); ++index) for (auto index = 0u; index!=symbols.size(); ++index)
allCharacters.push_back(symbols[index]); allCharacters.push_back(symbols[index]);
for (auto index = 0; index!=numbers.size(); ++index) for (auto index = 0u; index!=numbers.size(); ++index)
allCharacters.push_back(numbers[index]); allCharacters.push_back(numbers[index]);
for (auto index = 0; index!=letters.size(); ++index) for (auto index = 0u; index!=letters.size(); ++index)
allCharacters.push_back(letters[index]); allCharacters.push_back(letters[index]);
populateDecryptedValues(); populateDecryptedValues();
populateEncryptedValues(); populateEncryptedValues();
} }
GenerateKeys::~GenerateKeys()
{ }
void GenerateKeys::populateDecryptedValues() void GenerateKeys::populateDecryptedValues()
{ {
std::fstream readKeys{}; std::fstream readKeys{};
readKeys.open("default_keys.txt", std::ios::in); readKeys.open(defaultKeyFileName, std::ios::in);
std::string tmpKey{}; std::string tmpKey{};
for (auto index = 0; index!=allCharacters.size(); ++index) for (auto index = 0u; index!=allCharacters.size(); ++index)
{ {
readKeys >> tmpKey; readKeys >> tmpKey;
decryptedCharacters[tmpKey] = static_cast<char>(allCharacters[index]); decryptedCharacters[tmpKey] = static_cast<char>(allCharacters[index]);
@@ -40,9 +42,9 @@ void GenerateKeys::populateDecryptedValues()
void GenerateKeys::populateEncryptedValues() void GenerateKeys::populateEncryptedValues()
{ {
std::fstream readKeys{}; std::fstream readKeys{};
readKeys.open("default_keys.txt", std::ios::in); readKeys.open(defaultKeyFileName, std::ios::in);
std::string tmpKey{}; std::string tmpKey{};
for (auto index = 0; index!=allCharacters.size(); ++index) for (auto index = 0u; index!=allCharacters.size(); ++index)
{ {
readKeys >> tmpKey; readKeys >> tmpKey;
encryptedCharacters[decryptedCharacters[tmpKey]] = tmpKey; encryptedCharacters[decryptedCharacters[tmpKey]] = tmpKey;
@@ -99,21 +101,26 @@ void GenerateKeys::generateKey(std::string& key, const int& SIZE)
} }
void GenerateKeys::keyMove() void GenerateKeys::keyMove()
{ {
std::string tmpKey{};
for (auto index = 0; index!=totalCharacters; ++index) for (auto index = 0; index!=totalCharacters; ++index)
{ {
std::string tmpKey{};
generateKey(tmpKey, keySize); generateKey(tmpKey, keySize);
keys.push_back(tmpKey); keys.push_back(tmpKey);
tmpKey = "";
} }
if (isThereRepetition())
{
keys.clear();
keyMove();
}
std::cout << "No repetitions in keys" << std::endl;
} }
void GenerateKeys::print(const std::vector<int>& ch) void GenerateKeys::print(const std::vector<int>& ch)
{ {
for (auto index = 0; index!=ch.size(); ++index) for (auto index = 0u; index!=ch.size(); ++index)
std::cout << ch[index] << " "; std::cout << ch[index] << " ";
std::cout << std::endl; std::cout << std::endl;
} }
void GenerateKeys::checkRepetition() bool GenerateKeys::isThereRepetition()
{ {
for (auto index = 0; index!=totalCharacters; ++index) for (auto index = 0; index!=totalCharacters; ++index)
{ {
@@ -125,14 +132,18 @@ void GenerateKeys::checkRepetition()
++repetition; ++repetition;
} }
if (repetition > 1) if (repetition > 1)
{
std::cout << "Too much repetitive stuff" << std::endl; std::cout << "Too much repetitive stuff" << std::endl;
return true;
} }
} }
return false;
}
void GenerateKeys::keyDump() void GenerateKeys::keyDump()
{ {
std::fstream dump{}; std::fstream dump{};
dump.open("default_keys.txt", std::ios::out); dump.open(defaultKeyFileName, std::ios::out);
for (auto index = 0; index!=keys.size(); ++index) for (auto index = 0u; index!=keys.size(); ++index)
dump << keys[index] << '\n'; dump << keys[index] << '\n';
dump.close(); dump.close();
} }
+7 -7
View File
@@ -8,9 +8,8 @@
class GenerateKeys class GenerateKeys
{ {
public: public:
typedef unsigned short unsignedShort;
GenerateKeys(); GenerateKeys();
~GenerateKeys();
void populateDecryptedValues(); void populateDecryptedValues();
void populateEncryptedValues(); void populateEncryptedValues();
@@ -29,20 +28,21 @@ private:
void addRange(std::vector<int>&, int, const int); void addRange(std::vector<int>&, int, const int);
void generateKey(std::string&, const int&); void generateKey(std::string&, const int&);
void checkRepetition(); bool isThereRepetition();
void print(const std::vector<int>&); void print(const std::vector<int>&);
char getChar(std::vector<int>&); char getChar(std::vector<int>&);
std::vector<std::string> keys{}; std::vector<std::string> keys{};
std::vector<int> symbols{}; std::vector<int> symbols;
std::vector<int> numbers{}; std::vector<int> numbers;
std::vector<int> letters{}; std::vector<int> letters;
std::vector<int> allCharacters{}; std::vector<int> allCharacters;
std::map<char, std::string> encryptedCharacters{}; std::map<char, std::string> encryptedCharacters{};
std::map<std::string, char> decryptedCharacters{}; std::map<std::string, char> decryptedCharacters{};
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}; const int totalCharacters{63};
+33 -15
View File
@@ -1,9 +1,9 @@
#include"KeyManagementWindow.h" #include"KeyManagementWindow.h"
#include"GenerateKeys.h" #include"GenerateKeys.h"
#include <QVBoxLayout> #include<QtWidgets>
#include <QHBoxLayout>
#include<QString> #include<QString>
#include<QWidget>
#include<string> #include<string>
#include<sstream> #include<sstream>
#include<iostream> #include<iostream>
@@ -13,19 +13,24 @@
KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent) KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent)
{ {
GenerateKeys gk; GenerateKeys gk{};
comboBoxOfKeys = new QComboBox; comboBoxOfKeys = new QComboBox{};
valueOfKey = new QLineEdit; valueOfKey = new QLineEdit{};
generateNewDefaultKeys = new QPushButton(tr("Generate New Default Key"));
QVBoxLayout* vBox = new QVBoxLayout; vBox = new QVBoxLayout;
QHBoxLayout* hBox = new QHBoxLayout; hBox = new QHBoxLayout;
QHBoxLayout* hBox2 = new QHBoxLayout;
setContentsOfComboBox(); setContentsOfComboBox();
hBox->addWidget(comboBoxOfKeys); hBox->addWidget(comboBoxOfKeys);
hBox->addWidget(valueOfKey); hBox->addWidget(valueOfKey);
hBox2->addWidget(generateNewDefaultKeys);
vBox->addLayout(hBox); vBox->addLayout(hBox);
vBox->addLayout(hBox2);
setLayout(vBox); setLayout(vBox);
@@ -35,6 +40,15 @@ KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent)
setWindowTitle("Key Management Window"); setWindowTitle("Key Management Window");
QObject::connect(comboBoxOfKeys, SIGNAL(currentIndexChanged(int)), SLOT(test())); QObject::connect(comboBoxOfKeys, SIGNAL(currentIndexChanged(int)), SLOT(test()));
QObject::connect(generateNewDefaultKeys, SIGNAL(clicked()), this, SLOT(generation()));
}
KeyManagementWindow::~KeyManagementWindow()
{
delete comboBoxOfKeys;
delete valueOfKey;
delete generateNewDefaultKeys;
delete hBox;
delete vBox;
} }
@@ -42,8 +56,7 @@ void KeyManagementWindow::setContentsOfComboBox()
{ {
GenerateKeys gk; GenerateKeys gk;
std::cout << "got here" << std::endl; for (auto index = 0u; index!=gk.allCharacters.size(); ++index)
for (auto index = 0; index!=gk.allCharacters.size(); ++index)
{ {
std::string ch{static_cast<char>(gk.allCharacters[index])}; std::string ch{static_cast<char>(gk.allCharacters[index])};
QString bl = QString::fromStdString(ch); QString bl = QString::fromStdString(ch);
@@ -52,17 +65,22 @@ void KeyManagementWindow::setContentsOfComboBox()
} }
void KeyManagementWindow::test() void KeyManagementWindow::test()
{ {
GenerateKeys gk; GenerateKeys gk{};
QString bo = comboBoxOfKeys->currentText(); QString bo{comboBoxOfKeys->currentText()};
std::string k = bo.toStdString(); std::string k{bo.toStdString()};
char f; char f{};
std::stringstream lazy{}; std::stringstream lazy{};
lazy << k; lazy << k;
lazy >> f; lazy >> f;
//std::cout << "character: " << f << std::endl;
std::string value{gk.encryptedCharacters[f]}; std::string value{gk.encryptedCharacters[f]};
QString v = QString::fromStdString(value); QString v{QString::fromStdString(value)};
valueOfKey->setText(v); valueOfKey->setText(v);
} }
void KeyManagementWindow::generation()
{
GenerateKeys gk{};
gk.keyMove();
gk.keyDump();
}
+12 -2
View File
@@ -2,24 +2,34 @@
#define KEYMANAGEMENTWINDOW_H #define KEYMANAGEMENTWINDOW_H
#include<QDialog> #include<QDialog>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QComboBox> #include<QComboBox>
#include<QLineEdit> #include<QLineEdit>
class QHBoxLayout;
class QVBoxLayout;
class QComboBox; class QComboBox;
class QLineEdit; class QLineEdit;
class QPushButton;
class KeyManagementWindow : public QDialog class KeyManagementWindow : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
KeyManagementWindow(QWidget* parent = 0); KeyManagementWindow(QWidget* parent = 0);
~KeyManagementWindow();
void setContentsOfComboBox(); void setContentsOfComboBox();
private slots: private slots:
void test(); void test();
void generation();
private: private:
QComboBox* comboBoxOfKeys; QHBoxLayout* hBox{};
QLineEdit* valueOfKey; QVBoxLayout* vBox{};
QComboBox* comboBoxOfKeys{};
QLineEdit* valueOfKey{};
QPushButton* generateNewDefaultKeys;
}; };
#endif #endif