diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-15 12:44:35 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-15 12:44:35 +0200 |
| commit | be846db403c29c77bfb72633d87a7c72a48562b6 (patch) | |
| tree | ea22aa86bbb160d641d289de62d110e4f77ccda3 /src/crypt | |
| parent | 8022ab5725540b6ae71e1f353a3ff4ba72d31646 (diff) | |
| download | IT-Sicherheit-be846db403c29c77bfb72633d87a7c72a48562b6.tar.gz IT-Sicherheit-be846db403c29c77bfb72633d87a7c72a48562b6.zip | |
Adds multiple rotors and comments
Diffstat (limited to 'src/crypt')
| -rw-r--r-- | src/crypt/cryptclasscaesar.cpp | 66 | ||||
| -rw-r--r-- | src/crypt/cryptclasscaesar.h | 4 |
2 files changed, 60 insertions, 10 deletions
diff --git a/src/crypt/cryptclasscaesar.cpp b/src/crypt/cryptclasscaesar.cpp index e2fdf50..f2d0960 100644 --- a/src/crypt/cryptclasscaesar.cpp +++ b/src/crypt/cryptclasscaesar.cpp @@ -3,61 +3,83 @@ const int CryptClassCaesar::LOWERBOUND = 'A'; const int CryptClassCaesar::UPPERBOUND = 'Z'; +/** + * @brief CryptClassCaesar::CryptClassCaesar constructor for caesar encryption + */ CryptClassCaesar::CryptClassCaesar() { - mapOffset = 0; - buildMap(); + qDebug("CryptClassCaesar::CryptClassCaesar()"); } +/** + * @brief CryptClassCaesar::~CryptClassCaesar destructor for caesar encryption + */ CryptClassCaesar::~CryptClassCaesar() { - + qDebug("CryptClassCaesar::~CryptClassCaesar()"); } +/** + * @brief CryptClassCaesar::encrypt encrypt the PlainText with the specific rotation + */ void CryptClassCaesar::encrypt() { - qDebug("CryptClassCaesar::encrypt"); + qDebug() << "CryptClassCaesar::encrypt()"; + buildMap(); + // convert into upper letters m_clearText = m_clearText.toUpper(); + // clear cryptText m_cryptText.clear(); + // Set offset into 0 mapOffset = 0; - + // Space counter int spaceCount = 0; for (int i = 0; i < m_clearText.size(); i++) { + // only accept letters between 'A' and 'Z' if (m_clearText[i] >= (char) LOWERBOUND && m_clearText[i] <= (char) UPPERBOUND) { + // encrypt Letters and rotate roter m_cryptText.append(substitutionsMap[LOWERBOUND + (((m_clearText[i] - LOWERBOUND) + mapOffset++) % (26))]); + // insert spaces if (!(++spaceCount % 5)) { m_cryptText.append(" "); } } } - } +/** + * @brief CryptClassCaesar::decrypt encrypt the PlainText with the specific rotation + */ void CryptClassCaesar::decrypt() { - qDebug("CryptClassCaesar::decrypt"); + qDebug("CryptClassCaesar::decrypt()"); + buildMap(); int tmp = 0; m_cryptText = m_cryptText.toUpper(); - + // delete clear text m_clearText.clear(); + // set offset into 0 mapOffset = 0; for (int i = 0; i < m_cryptText.size(); i++) { + // only accept letters between 'A' and 'Z' if (m_cryptText[i] >= (char) LOWERBOUND && m_cryptText[i] <= (char) UPPERBOUND) { + // decrypt letters and rotate rotor backwards tmp = ((substitutionsMap.key(m_cryptText[i]) - LOWERBOUND) - mapOffset++) % (26); + // to hack the modulo operator if (tmp < 0) { //Because tmp is negative adding it to UPPERBOUND subtracts it @@ -72,14 +94,38 @@ void CryptClassCaesar::decrypt() } } +/** + * @brief CryptClassCaesar::buildMap fill the map with information + */ void CryptClassCaesar::buildMap() { - char tmp[] = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"; + bool ok = false; + int rotor = getKey().toInt(&ok); + + if (ok == false || rotor < 1 || rotor > 5) + { + QMessageBox::warning(NULL, "Key invalid", + "The key for Enigma should only be a number between 1 and 5."); + return; + } + + rotor--; + + // Char Array to simple implement the rotor + char rotors[5][27] = + { + "EKMFLGDQVZNTOWYHXUSPAIBRCJ", + "AJDKSIRUXBLHWTMCQGZNPYFVOE", + "BDFHJLCPRTXVZNYEIWGAKMUSQO", + "ESOVPZJAYQUIRHXLNFTGKDCMWB", + "VZBRGITYUPSDNHLXAWMJQOFECK" + }; + // Clear substitution Map substitutionsMap.clear(); for (int i = 0; i <= (UPPERBOUND - LOWERBOUND); i++) { - substitutionsMap.insert((char)(LOWERBOUND + i), tmp[i]); + substitutionsMap.insert((char)(LOWERBOUND + i), rotors[rotor][i]); } } diff --git a/src/crypt/cryptclasscaesar.h b/src/crypt/cryptclasscaesar.h index 95fc479..dd1bbd6 100644 --- a/src/crypt/cryptclasscaesar.h +++ b/src/crypt/cryptclasscaesar.h @@ -3,9 +3,13 @@ #include "cryptclassbase.h" #include <QMap> +#include <QByteArray> #include <QDebug> #include <QMessageBox> +/** + * @brief The CryptClassCaesar class defines the caesar encryption + */ class CryptClassCaesar : public CryptClassBase { public: |
