From 3f5d67e4cf5c94ee9bf82552a5b7722e95eb3a73 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 27 Mar 2017 21:27:28 -0500 Subject: [PATCH] Issue #1 #2 #4 are addressed in this commit. Added another window with controls for key management. Simple ASCII encryption --- EncryptedMessaging.pro | 3 +- Encryption.cpp | 15 ++++++++ Encryption.h | 4 ++ GenerateKeys.cpp | 9 ++--- GenerateKeys.h | 4 ++ KeyManagementWindow.cpp | 85 +++++++++++++++++++++++++++++++++++++++++ KeyManagementWindow.h | 25 ++++++++++++ Makefile | 31 +++++++++++---- MessagingControls.cpp | 33 ++++++++++++++-- MessagingControls.h | 1 + 10 files changed, 194 insertions(+), 16 deletions(-) create mode 100644 KeyManagementWindow.cpp create mode 100644 KeyManagementWindow.h diff --git a/EncryptedMessaging.pro b/EncryptedMessaging.pro index 9f1c43c..8a20f2c 100644 --- a/EncryptedMessaging.pro +++ b/EncryptedMessaging.pro @@ -9,10 +9,11 @@ QT += widgets INCLUDEPATH += . # Input -HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h +HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h SOURCES += Decryption.cpp \ Encryption.cpp \ Cryption.cpp \ + KeyManagementWindow.cpp \ GenerateKeys.cpp \ Main.cpp \ MessagingControls.cpp diff --git a/Encryption.cpp b/Encryption.cpp index 2a77ea8..e8491c8 100644 --- a/Encryption.cpp +++ b/Encryption.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "Encryption.h" #include "GenerateKeys.h" @@ -13,6 +15,10 @@ Encryption::Encryption(const std::string& message) } +void Encryption::setEncryptedMessage(const std::string& encryptedMessage) +{ + this->encryptedMessage = encryptedMessage; +} void Encryption::encryptMessage() { GenerateKeys* gk = new GenerateKeys; @@ -21,9 +27,18 @@ void Encryption::encryptMessage() for (unsigned short indexOfString = 0; indexOfString < message.size(); indexOfString++) { ioEvent << gk->encryptedCharacters[message.at(indexOfString)]; + encryptedMessage += std::to_string(gk->encryptedCharacters[message.at(indexOfString)]); + std::cout << encryptedMessage << std::endl; } + setEncryptedMessage(encryptedMessage); ioEvent.close(); delete gk; } + + +std::string Encryption::getEncryptedMessage() const +{ + return encryptedMessage; +} diff --git a/Encryption.h b/Encryption.h index 5f6b54b..5584168 100644 --- a/Encryption.h +++ b/Encryption.h @@ -13,9 +13,13 @@ public: Encryption(); Encryption(const std::string&); + void setEncryptedMessage(const std::string&); void encryptMessage(); + std::string getEncryptedMessage() const; + private: + std::string encryptedMessage; }; #endif diff --git a/GenerateKeys.cpp b/GenerateKeys.cpp index 80d4ed0..d30aa6a 100644 --- a/GenerateKeys.cpp +++ b/GenerateKeys.cpp @@ -4,7 +4,6 @@ GenerateKeys::GenerateKeys() { - populateDecryptedValues(); populateEncryptedValues(); } @@ -16,22 +15,22 @@ void GenerateKeys::populateDecryptedValues() char character; character = static_cast(number); - if (number >= 97 && number <= 122) + if (number >= startingCharacter && number <= endingCharacter) { decryptedCharacters[number] = character; //std::cout << decryptedCharacters[number] << " "; } } //std::cout << std::endl; - decryptedCharacters[32] = ' '; + decryptedCharacters[whiteSpaceCharacter] = ' '; } void GenerateKeys::populateEncryptedValues() { - for (unsigned short key = 97; key <= 122; key++) + for (unsigned short key = startingCharacter; key <= endingCharacter; key++) { encryptedCharacters[decryptedCharacters[key]] = key; //std::cout << encryptedCharacters[decryptedCharacters[key]] << " "; } - encryptedCharacters[decryptedCharacters[32]] = 32; + encryptedCharacters[decryptedCharacters[startingCharacter]] = startingCharacter; //std::cout << std::endl; } diff --git a/GenerateKeys.h b/GenerateKeys.h index 22842d6..eea32c5 100644 --- a/GenerateKeys.h +++ b/GenerateKeys.h @@ -15,10 +15,14 @@ public: friend class Encryption; friend class Decryption; + friend class KeyManagementWindow; private: std::map decryptedCharacters; std::map encryptedCharacters; + unsigned short startingCharacter = 97; + unsigned short endingCharacter = 122; + unsigned short whiteSpaceCharacter = 32; }; #endif diff --git a/KeyManagementWindow.cpp b/KeyManagementWindow.cpp new file mode 100644 index 0000000..36ea25f --- /dev/null +++ b/KeyManagementWindow.cpp @@ -0,0 +1,85 @@ +#include "KeyManagementWindow.h" +#include "GenerateKeys.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent) +{ + GenerateKeys gk; + + comboBoxOfKeys = new QComboBox; + valueOfKey = new QLineEdit; + + QVBoxLayout* vBox = new QVBoxLayout; + QHBoxLayout* hBox = new QHBoxLayout; + + setContentsOfComboBox(); + hBox->addWidget(comboBoxOfKeys); + hBox->addWidget(valueOfKey); + + vBox->addLayout(hBox); + + setLayout(vBox); + + setFixedWidth(400); + setFixedHeight(400); + + setWindowTitle("Key Management Window"); + + QObject::connect(comboBoxOfKeys, SIGNAL(currentIndexChanged(int)), SLOT(test())); +} + + +void KeyManagementWindow::setContentsOfComboBox() +{ + GenerateKeys gk; + + for (unsigned short keyIndex = gk.startingCharacter; keyIndex <= gk.endingCharacter; keyIndex++) + { + std::string ch = ""; + ch += gk.decryptedCharacters[keyIndex]; + QString bl = QString::fromStdString(ch); + comboBoxOfKeys->addItem(bl); + } +} +void KeyManagementWindow::test() +{ + GenerateKeys gk; + //std::cout << "bang bang!" << std::endl; + QString bo = comboBoxOfKeys->currentText(); + std::string k = bo.toStdString(); + char f; + + std::fstream in; + in.open("strToCh.txt", std::ios::out); + + in << k; + in.close(); + + in.open("strToCh.txt", std::ios::in); + + in >> f; + in.close(); + + in.open("strToCh.txt", std::ios::out); + in << gk.encryptedCharacters[f]; + in.close(); + + unsigned short yek; + in.open("strToCh.txt", std::ios::in); + in >> yek; + in.close(); + + std::string key = ""; + key += std::to_string(yek); + //std::cout << gk.encryptedCharacters[f] << std::endl; + QString v = QString::fromStdString(key); + valueOfKey->setText(v); +} diff --git a/KeyManagementWindow.h b/KeyManagementWindow.h new file mode 100644 index 0000000..d1f8da4 --- /dev/null +++ b/KeyManagementWindow.h @@ -0,0 +1,25 @@ +#ifndef KEYMANAGEMENTWINDOW_H +#define KEYMANAGEMENTWINDOW_H + +#include +#include +#include + +class QComboBox; +class QLineEdit; + +class KeyManagementWindow : public QDialog +{ + Q_OBJECT +public: + KeyManagementWindow(QWidget* parent = 0); + + void setContentsOfComboBox(); +private slots: + void test(); +private: + QComboBox* comboBoxOfKeys; + QLineEdit* valueOfKey; +}; + +#endif diff --git a/Makefile b/Makefile index fbfb9ed..c92d560 100644 --- a/Makefile +++ b/Makefile @@ -51,16 +51,20 @@ OBJECTS_DIR = ./ SOURCES = Decryption.cpp \ Encryption.cpp \ Cryption.cpp \ + KeyManagementWindow.cpp \ GenerateKeys.cpp \ Main.cpp \ - MessagingControls.cpp moc_MessagingControls.cpp + MessagingControls.cpp moc_MessagingControls.cpp \ + moc_KeyManagementWindow.cpp OBJECTS = Decryption.o \ Encryption.o \ Cryption.o \ + KeyManagementWindow.o \ GenerateKeys.o \ Main.o \ MessagingControls.o \ - moc_MessagingControls.o + moc_MessagingControls.o \ + moc_KeyManagementWindow.o DIST = /usr/lib/qt/mkspecs/features/spec_pre.prf \ /usr/lib/qt/mkspecs/common/unix.conf \ /usr/lib/qt/mkspecs/common/linux.conf \ @@ -170,9 +174,11 @@ DIST = /usr/lib/qt/mkspecs/features/spec_pre.prf \ Encryption.h \ GenerateKeys.h \ MessagingControls.h \ - Cryption.h Decryption.cpp \ + Cryption.h \ + KeyManagementWindow.h Decryption.cpp \ Encryption.cpp \ Cryption.cpp \ + KeyManagementWindow.cpp \ GenerateKeys.cpp \ Main.cpp \ MessagingControls.cpp @@ -421,8 +427,8 @@ distdir: FORCE @test -d $(DISTDIR) || mkdir -p $(DISTDIR) $(COPY_FILE) --parents $(DIST) $(DISTDIR)/ $(COPY_FILE) --parents /usr/lib/qt/mkspecs/features/data/dummy.cpp $(DISTDIR)/ - $(COPY_FILE) --parents Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h $(DISTDIR)/ - $(COPY_FILE) --parents Decryption.cpp Encryption.cpp Cryption.cpp GenerateKeys.cpp Main.cpp MessagingControls.cpp $(DISTDIR)/ + $(COPY_FILE) --parents Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h $(DISTDIR)/ + $(COPY_FILE) --parents Decryption.cpp Encryption.cpp Cryption.cpp KeyManagementWindow.cpp GenerateKeys.cpp Main.cpp MessagingControls.cpp $(DISTDIR)/ clean: compiler_clean @@ -454,14 +460,19 @@ compiler_moc_predefs_clean: moc_predefs.h: /usr/lib/qt/mkspecs/features/data/dummy.cpp g++ -pipe -O2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -Wall -W -dM -E -o moc_predefs.h /usr/lib/qt/mkspecs/features/data/dummy.cpp -compiler_moc_header_make_all: moc_MessagingControls.cpp +compiler_moc_header_make_all: moc_MessagingControls.cpp moc_KeyManagementWindow.cpp compiler_moc_header_clean: - -$(DEL_FILE) moc_MessagingControls.cpp + -$(DEL_FILE) moc_MessagingControls.cpp moc_KeyManagementWindow.cpp moc_MessagingControls.cpp: MessagingControls.h \ moc_predefs.h \ /usr/bin/moc /usr/bin/moc $(DEFINES) --include ./moc_predefs.h -I/usr/lib/qt/mkspecs/linux-g++ -I/home/monde/Programming/Qt/EncryptedMessaging -I/home/monde/Programming/Qt/EncryptedMessaging -I/usr/include/qt -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore -I/usr/include/c++/6.3.1 -I/usr/include/c++/6.3.1/x86_64-pc-linux-gnu -I/usr/include/c++/6.3.1/backward -I/usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/include -I/usr/local/include -I/usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/include-fixed -I/usr/include MessagingControls.h -o moc_MessagingControls.cpp +moc_KeyManagementWindow.cpp: KeyManagementWindow.h \ + moc_predefs.h \ + /usr/bin/moc + /usr/bin/moc $(DEFINES) --include ./moc_predefs.h -I/usr/lib/qt/mkspecs/linux-g++ -I/home/monde/Programming/Qt/EncryptedMessaging -I/home/monde/Programming/Qt/EncryptedMessaging -I/usr/include/qt -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore -I/usr/include/c++/6.3.1 -I/usr/include/c++/6.3.1/x86_64-pc-linux-gnu -I/usr/include/c++/6.3.1/backward -I/usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/include -I/usr/local/include -I/usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/include-fixed -I/usr/include KeyManagementWindow.h -o moc_KeyManagementWindow.cpp + compiler_moc_source_make_all: compiler_moc_source_clean: compiler_uic_make_all: @@ -489,6 +500,9 @@ Encryption.o: Encryption.cpp Encryption.h \ Cryption.o: Cryption.cpp Cryption.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o Cryption.o Cryption.cpp +KeyManagementWindow.o: KeyManagementWindow.cpp KeyManagementWindow.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o KeyManagementWindow.o KeyManagementWindow.cpp + GenerateKeys.o: GenerateKeys.cpp GenerateKeys.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o GenerateKeys.o GenerateKeys.cpp @@ -501,6 +515,9 @@ MessagingControls.o: MessagingControls.cpp MessagingControls.h moc_MessagingControls.o: moc_MessagingControls.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_MessagingControls.o moc_MessagingControls.cpp +moc_KeyManagementWindow.o: moc_KeyManagementWindow.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_KeyManagementWindow.o moc_KeyManagementWindow.cpp + ####### Install install: FORCE diff --git a/MessagingControls.cpp b/MessagingControls.cpp index 5d1383a..4c2207c 100644 --- a/MessagingControls.cpp +++ b/MessagingControls.cpp @@ -3,6 +3,8 @@ #include #include "MessagingControls.h" +#include "KeyManagementWindow.h" +#include "Encryption.h" MessagingControls::MessagingControls() { @@ -28,10 +30,12 @@ MessagingControls::~MessagingControls() void MessagingControls::setupMainWindow() { textForCryption = new QTextEdit; + textForCryption->setText("sdfs"); + textForCryption->clear(); cryptionButon = new QPushButton(tr("XO")); - cryptionSwitch = new QPushButton(tr("encrypt")); + cryptionSwitch = new QPushButton(tr("encrypt chosen")); buttonLayout = new QVBoxLayout; @@ -55,6 +59,7 @@ void MessagingControls::setupMainWindow() QObject::connect(closeApplication, SIGNAL(triggered()), this, SLOT(close())); QObject::connect(cryptionSwitch, SIGNAL(clicked()), this, SLOT(changeCryptionType())); QObject::connect(cryptionButon, SIGNAL(clicked()), this, SLOT(determineCryption())); + QObject::connect(keyEdit, SIGNAL(triggered()), this, SLOT(keyManagementWindow())); setWindowTitle("Encryption Decryption Messaging"); setFixedHeight(mainWindowHeight); @@ -78,12 +83,12 @@ void MessagingControls::changeCryptionType() { if (cryptionChoice) { - cryptionSwitch->setText("decrypt"); + cryptionSwitch->setText("decrypt chosen"); cryptionChoice = false; } else { - cryptionSwitch->setText("encrypt"); + cryptionSwitch->setText("encrypt chosen"); cryptionChoice = true; } } @@ -91,6 +96,21 @@ void MessagingControls::determineCryption() { if (cryptionChoice) { + QString content; + content = textForCryption->toPlainText(); + std::string message = content.toStdString(); + + Encryption ec; + ec.setMessage(message); + ec.encryptMessage(); + + std::cout << ec.getEncryptedMessage() << std::endl; + QString messageQ = QString::fromStdString(ec.getEncryptedMessage()); + textForCryption->clear(); + textForCryption->setText(messageQ); + + cryptionSwitch->setText("decrypt chosen"); + cryptionChoice = false; /** * Decrypt Message */ @@ -110,3 +130,10 @@ void MessagingControls::determineCryption() //std::cout << textForCryption->toPlainText() << std::endl; } +void MessagingControls::keyManagementWindow() +{ + KeyManagementWindow* kh = new KeyManagementWindow; + + kh->show(); + +} diff --git a/MessagingControls.h b/MessagingControls.h index 3821ee4..e81a9d0 100644 --- a/MessagingControls.h +++ b/MessagingControls.h @@ -36,6 +36,7 @@ signals: private slots: void changeCryptionType(); void determineCryption(); + void keyManagementWindow(); private: void setupMainWindow();