From be846db403c29c77bfb72633d87a7c72a48562b6 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Wed, 15 Apr 2015 12:44:35 +0200 Subject: Adds multiple rotors and comments --- src/crypt/cryptclasscaesar.cpp | 66 +++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) (limited to 'src/crypt/cryptclasscaesar.cpp') 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]); } } -- cgit v1.2.3-70-g09d2