Changed from ASCII cryption to key Cryption

This commit is contained in:
monde
2017-04-25 13:05:01 -05:00
parent c82d81fdc1
commit fbee689234
12 changed files with 1700 additions and 329 deletions
-1
View File
@@ -2,7 +2,6 @@
Cryption::Cryption()
{
}
Cryption::Cryption(const std::string& message)
{
+3 -5
View File
@@ -9,16 +9,14 @@ class Cryption
public:
Cryption();
Cryption(const std::string&);
void setMessage(const std::string&);
//void setMessage(const std::string&);
std::string getMessage() const;
protected:
void setMessage(const std::string&);
std::string message;
std::fstream ioEvent;
private:
};
#endif
+23 -32
View File
@@ -1,4 +1,5 @@
#include <iostream>
#include<sstream>
#include <ios>
#include <string>
@@ -7,7 +8,6 @@
Decryption::Decryption()
{
}
Decryption::Decryption(const std::string& message)
{
@@ -21,55 +21,46 @@ void Decryption::setDecryptedMessage(const std::string& message)
}
void Decryption::decryptMessage()
{
GenerateKeys gk;
GenerateKeys gk{};
ioEvent.open("encryptedFile.txt", std::ios::in);
std::string messageToBeDecrypted, decryptedMessage;
std::string messageToBeDecrypted{}, decryptedMessage{};
while(!ioEvent.eof())
{
ioEvent >> messageToBeDecrypted;
}
while(ioEvent >> messageToBeDecrypted)
ioEvent.close();
unsigned short encryptedTwoKey = 2;
unsigned short beginningOfAlphabetLowerCase = 97;
unsigned short endingOfAplphabetLowerCase = 122;
unsigned short whiteSpaceASCIICode = 32;
for (unsigned short index = 0; index < messageToBeDecrypted.size(); index++)
for (auto indexOfChar = 0; indexOfChar<messageToBeDecrypted.size(); indexOfChar+=5)
{
if ((index + encryptedTwoKey) < messageToBeDecrypted.size())
char cryptedKey[5]{};
for (auto index = 0; index!=5; ++index)
{
char otherElements[] = {messageToBeDecrypted.at(index), messageToBeDecrypted.at(index + 1), messageToBeDecrypted.at(index + 2)};
unsigned short number = atoi(otherElements);
cryptedKey[index] = (messageToBeDecrypted.at(indexOfChar + index));
if ((number > endingOfAplphabetLowerCase || number < beginningOfAlphabetLowerCase) && (number != whiteSpaceASCIICode))
{
otherElements[0] = {messageToBeDecrypted.at(index)};
otherElements[1] = {messageToBeDecrypted.at(index + 1)};
otherElements[2] = {' '};
number = atoi(otherElements);
decryptedMessage += gk.decryptedCharacters[number];
index += 1;
}
else
{
decryptedMessage += gk.decryptedCharacters[number];
index += 2;
}
}
decryptedMessage += gk.decryptedCharacters[cstringToString(cryptedKey, 5)];
}
//std::cout << "Decrypted Message: " << decryptedMessage << std::endl;
setDecryptedMessage(decryptedMessage);
}
std::string Decryption::getDecryptedMessage() const
{
return decryptedMessage;
}
std::string Decryption::cstringToString(char tmp[], const int& SIZE)
{
std::string tmpString{};
std::stringstream cToS{};
for (auto index = 0; index!=5; ++index)
{
cToS << tmp[index];
}
cToS >> tmpString;
return tmpString;
}
unsigned short getTwoNumberedKey(unsigned short& number)
@@ -80,6 +71,6 @@ unsigned short getTwoNumberedKey(unsigned short& number)
char one[] {wholeNumber.at(2)};
unsigned short hundread, ten, one;
hundread = number.at()
unsigned short hundread, tenN, oneN;
hundread = number;
}
+2 -1
View File
@@ -19,7 +19,8 @@ public:
std::string getDecryptedMessage() const;
private:
std::string decryptedMessage;
std::string cstringToString(char[], const int&);
std::string decryptedMessage{};
};
#endif
+6 -18
View File
@@ -7,7 +7,6 @@
Encryption::Encryption()
{
}
Encryption::Encryption(const std::string& message)
{
@@ -22,34 +21,23 @@ void Encryption::setEncryptedMessage(const std::string& encryptedMessage)
void Encryption::encryptMessage()
{
int numberOfNewLines{0};
GenerateKeys* gk = new GenerateKeys;
GenerateKeys gk{};
ioEvent.open("encryptedFile.txt", std::ios::out);
std::cout << "String to encrypt: " << message << std::endl;
//while (numberOfNewLines != message.size())
for (unsigned int index = 0; index != message.size(); ++index)
//std::cout << "String to encrypt: " << message << std::endl;
for (auto index = 0; index != message.size(); ++index)
{
char stringIndex = message.at(index);
//if (message.at(index) == "\n")
//if (message.at(index).compare("\n") == 0)
if (stringIndex == '\n')
{
++numberOfNewLines;
}
}
std::cout << "The message has " << numberOfNewLines << " new lines" << std::endl;
for (unsigned short indexOfString = 0; indexOfString < message.size(); indexOfString++)
for (auto indexOfString = 0; indexOfString!=message.size(); ++indexOfString)
{
ioEvent << gk->encryptedCharacters[message.at(indexOfString)];
//std::cout << gk->encryptedCharacters[message.at(indexOfString)] << std::endl;
encryptedMessage += std::to_string(gk->encryptedCharacters[message.at(indexOfString)]);
//std::cout << encryptedMessage << std::endl;
ioEvent << gk.encryptedCharacters[message.at(indexOfString)];
encryptedMessage += (gk.encryptedCharacters[message.at(indexOfString)]);
}
setEncryptedMessage(encryptedMessage);
ioEvent.close();
delete gk;
}
+1 -2
View File
@@ -9,7 +9,6 @@
class Encryption : public Cryption
{
public:
Encryption();
Encryption(const std::string&);
@@ -19,7 +18,7 @@ public:
std::string getEncryptedMessage() const;
private:
std::string encryptedMessage;
std::string encryptedMessage{};
};
#endif
+125 -21
View File
@@ -1,41 +1,145 @@
#include <iostream>
#include<sstream>
#include<fstream>
#include<ios>
#include "GenerateKeys.h"
GenerateKeys::GenerateKeys()
{
populateSymbols();
populateNumbers();
populateLetters();
for (auto index = 0; index!=symbols.size(); ++index)
allCharacters.push_back(symbols[index]);
for (auto index = 0; index!=numbers.size(); ++index)
allCharacters.push_back(numbers[index]);
for (auto index = 0; index!=letters.size(); ++index)
allCharacters.push_back(letters[index]);
populateDecryptedValues();
populateEncryptedValues();
}
void GenerateKeys::populateDecryptedValues()
{
for (unsigned short number = 0; number < 300; number++)
std::fstream readKeys{};
readKeys.open("default_keys.txt", std::ios::in);
std::string tmpKey{};
for (auto index = 0; index!=allCharacters.size(); ++index)
{
char character;
character = static_cast<char>(number);
if (number >= startingCharacter && number <= endingCharacter)
{
decryptedCharacters[number] = character;
//std::cout << decryptedCharacters[number] << " ";
readKeys >> tmpKey;
decryptedCharacters[tmpKey] = static_cast<char>(allCharacters[index]);
tmpKey = "";
}
}
decryptedCharacters[32] = (char) 32;
decryptedCharacters[10] = (char) 10;
//std::cout << std::endl;
//decryptedCharacters[whiteSpaceCharacter] = ' ';
//std::cout << "Whitespace character" << decryptedCharacters[whiteSpaceCharacter] << "d" << std::endl;
readKeys.close();
decryptedCharacters["#2a9u"] = (char) 32;
decryptedCharacters["6*dw4"] = (char) 10;
}
void GenerateKeys::populateEncryptedValues()
{
for (unsigned short key = startingCharacter; key <= endingCharacter; key++)
std::fstream readKeys{};
readKeys.open("default_keys.txt", std::ios::in);
std::string tmpKey{};
for (auto index = 0; index!=allCharacters.size(); ++index)
{
encryptedCharacters[decryptedCharacters[key]] = key;
//std::cout << encryptedCharacters[decryptedCharacters[key]] << " ";
readKeys >> tmpKey;
encryptedCharacters[decryptedCharacters[tmpKey]] = tmpKey;
tmpKey = "";
}
encryptedCharacters[(char) 32] = 32;
encryptedCharacters[(char) 10] = 10;
//encryptedCharacters[decryptedCharacters[startingCharacter]] = startingCharacter;
//std::cout << std::endl;
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, 58, 64);
addRange(symbols, 93, 96);
addRange(symbols, 123, 126);
}
void GenerateKeys::populateNumbers()
{
addRange(numbers, 48, 57);
}
void GenerateKeys::populateLetters()
{
addRange(letters, 97, 122);
}
void GenerateKeys::addRange(std::vector<int>& ch, int from, const int to)
{
auto range = to - from;
for (auto index = 0; index<=range; ++index)
{
ch.push_back(from);
++from;
}
}
void GenerateKeys::generateKey(std::string& key, const int& SIZE)
{
char ky[SIZE]{};
for (auto index = 0; index!=SIZE; ++index)
{
if (index < 2)
ky[index] = getChar(letters);
else if (index < 3 && index > 1)
ky[index] = getChar(symbols);
else
ky[index] = getChar(numbers);
}
std::stringstream charToString;
for (auto index = 0; index!=SIZE; ++index)
charToString << ky[index];
while (charToString>>key)
charToString >> key;
}
void GenerateKeys::keyMove()
{
std::string tmpKey{};
for (auto index = 0; index!=totalCharacters; ++index)
{
generateKey(tmpKey, keySize);
keys.push_back(tmpKey);
tmpKey = "";
}
}
void GenerateKeys::print(const std::vector<int>& ch)
{
for (auto index = 0; index!=ch.size(); ++index)
std::cout << ch[index] << " ";
std::cout << std::endl;
}
void GenerateKeys::checkRepetition()
{
for (auto index = 0; index!=totalCharacters; ++index)
{
std::string chosenOne{keys[index]};
auto repetition{0};
for (auto innerIndex = 0; innerIndex!=totalCharacters; ++innerIndex)
{
if (chosenOne == keys[innerIndex])
++repetition;
}
if (repetition > 1)
std::cout << "Too much repetitive stuff" << std::endl;
}
}
void GenerateKeys::keyDump()
{
std::fstream dump{};
dump.open("default_keys.txt", std::ios::out);
for (auto index = 0; index!=keys.size(); ++index)
dump << keys[index] << '\n';
dump.close();
}
char GenerateKeys::getChar(std::vector<int>& ch)
{
auto hc = ch[rand() % ch.size()];
return hc;
}
+30 -6
View File
@@ -1,7 +1,9 @@
#ifndef GENERATEKEYS_H
#define GENERATEKEYS_H
#include <map>
#include<string>
#include<vector>
#include<map>
class GenerateKeys
{
@@ -12,17 +14,39 @@ public:
void populateDecryptedValues();
void populateEncryptedValues();
void keyMove();
void keyDump();
friend class Encryption;
friend class Decryption;
friend class KeyManagementWindow;
private:
std::map<unsignedShort, char> decryptedCharacters;
std::map<char, unsignedShort> encryptedCharacters;
unsigned short startingCharacter = 97;
unsigned short endingCharacter = 122;
unsigned short whiteSpaceCharacter = 32;
void populateSymbols();
void populateNumbers();
void populateLetters();
void addRange(std::vector<int>&, int, const int);
void generateKey(std::string&, const int&);
void checkRepetition();
void print(const std::vector<int>&);
char getChar(std::vector<int>&);
std::vector<std::string> keys{};
std::vector<int> symbols{};
std::vector<int> numbers{};
std::vector<int> letters{};
std::vector<int> allCharacters{};
std::map<char, std::string> encryptedCharacters{};
std::map<std::string, char> decryptedCharacters{};
//KeySize show equal the value in key
const int keySize{5};
const int totalCharacters{63};
char key[5]{};
};
#endif
+10 -27
View File
@@ -5,6 +5,7 @@
#include <QHBoxLayout>
#include <QString>
#include <string>
#include<sstream>
#include <iostream>
#include <cstdlib>
#include <fstream>
@@ -41,10 +42,10 @@ void KeyManagementWindow::setContentsOfComboBox()
{
GenerateKeys gk;
for (unsigned short keyIndex = gk.startingCharacter; keyIndex <= gk.endingCharacter; keyIndex++)
std::cout << "got here" << std::endl;
for (auto index = 0; index!=gk.allCharacters.size(); ++index)
{
std::string ch = "";
ch += gk.decryptedCharacters[keyIndex];
std::string ch{static_cast<char>(gk.allCharacters[index])};
QString bl = QString::fromStdString(ch);
comboBoxOfKeys->addItem(bl);
}
@@ -52,34 +53,16 @@ void KeyManagementWindow::setContentsOfComboBox()
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);
std::stringstream lazy{};
lazy << k;
lazy >> f;
//std::cout << "character: " << f << std::endl;
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);
std::string value{gk.encryptedCharacters[f]};
QString v = QString::fromStdString(value);
valueOfKey->setText(v);
}
+1432 -194
View File
File diff suppressed because it is too large Load Diff
+2 -19
View File
@@ -10,9 +10,6 @@
MessagingControls::MessagingControls()
{
/**
* Initialize controls
*/
textForCryption = new QTextEdit;
cryptionChoice = true;
@@ -20,9 +17,6 @@ MessagingControls::MessagingControls()
}
MessagingControls::~MessagingControls()
{
/**
* Delete pointer types
*/
delete lblOfEncryptedBox;
delete cryptionButon;
delete textForCryption;
@@ -32,11 +26,9 @@ MessagingControls::~MessagingControls()
void MessagingControls::setupMainWindow()
{
cryptionButon = new QPushButton(tr("XO"));
cryptionSwitch = new QPushButton(tr("encrypt chosen"));
buttonLayout = new QVBoxLayout;
buttonWidget = new QWidget;
buttonDockWidget = new QDockWidget;
@@ -98,8 +90,7 @@ void MessagingControls::determineCryption()
{
if (cryptionChoice)
{
Encryption ec;
ec.setMessage(grabCryptionText());
Encryption ec{grabCryptionText()};
ec.encryptMessage();
textForCryption->clear();
@@ -107,14 +98,10 @@ void MessagingControls::determineCryption()
cryptionSwitch->setText("decrypt chosen");
cryptionChoice = false;
/**
* Encrypt Message
*/
}
else
{
Decryption dc;
dc.setMessage(grabCryptionText());
Decryption dc{grabCryptionText()};
dc.decryptMessage();
textForCryption->clear();
@@ -122,15 +109,11 @@ void MessagingControls::determineCryption()
cryptionSwitch->setText("encrypt chosen");
cryptionChoice = true;
/**
* Decrypt Message
*/
}
}
void MessagingControls::keyManagementWindow()
{
KeyManagementWindow* kh = new KeyManagementWindow;
kh->show();
}
+63
View File
@@ -0,0 +1,63 @@
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