#include "cryptclasscaesar.h" const int CryptClassCaesar::LOWERBOUND = 'A'; const int CryptClassCaesar::UPPERBOUND = 'Z'; CryptClassCaesar::CryptClassCaesar() { mapOffset = 0; buildMap(); } CryptClassCaesar::~CryptClassCaesar() { } void CryptClassCaesar::encrypt() { qDebug("CryptClassCaesar::encrypt"); m_clearText = m_clearText.toUpper(); m_cryptText.clear(); mapOffset = 0; int spaceCount = 0; for (int i = 0; i < m_clearText.size(); i++) { if (m_clearText[i] >= (char) LOWERBOUND && m_clearText[i] <= (char) UPPERBOUND) { m_cryptText.append(substitutionsMap[LOWERBOUND + (((m_clearText[i] - LOWERBOUND) + mapOffset++) % (26))]); if (!(++spaceCount % 5)) { m_cryptText.append(" "); } } } } void CryptClassCaesar::decrypt() { qDebug("CryptClassCaesar::decrypt"); int tmp = 0; m_cryptText = m_cryptText.toUpper(); m_clearText.clear(); mapOffset = 0; for (int i = 0; i < m_cryptText.size(); i++) { if (m_cryptText[i] >= (char) LOWERBOUND && m_cryptText[i] <= (char) UPPERBOUND) { tmp = ((substitutionsMap.key(m_cryptText[i]) - LOWERBOUND) - mapOffset++) % (26); if (tmp < 0) { //Because tmp is negative adding it to UPPERBOUND subtracts it //Also plus 1 because -1 is the highest negative number m_clearText.append(UPPERBOUND + tmp + 1); } else { m_clearText.append(LOWERBOUND + tmp); } } } } void CryptClassCaesar::buildMap() { char tmp[] = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"; substitutionsMap.clear(); for (int i = 0; i <= (UPPERBOUND - LOWERBOUND); i++) { substitutionsMap.insert((char)(LOWERBOUND + i), tmp[i]); } }