summaryrefslogtreecommitdiffstats
path: root/src/crypt
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-04-14 14:19:11 +0200
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-04-14 14:19:11 +0200
commit8022ab5725540b6ae71e1f353a3ff4ba72d31646 (patch)
tree8365b82c8480281db15d83efa16f93afcd6c295f /src/crypt
parent806943c0c488948c7c0a8b9d8202024a4ddeec8a (diff)
downloadIT-Sicherheit-8022ab5725540b6ae71e1f353a3ff4ba72d31646.tar.gz
IT-Sicherheit-8022ab5725540b6ae71e1f353a3ff4ba72d31646.zip
Enigmafied the Caesar chipher
Diffstat (limited to 'src/crypt')
-rw-r--r--src/crypt/cryptclasscaesar.cpp69
-rw-r--r--src/crypt/cryptclasscaesar.h3
2 files changed, 29 insertions, 43 deletions
diff --git a/src/crypt/cryptclasscaesar.cpp b/src/crypt/cryptclasscaesar.cpp
index c9942a8..e2fdf50 100644
--- a/src/crypt/cryptclasscaesar.cpp
+++ b/src/crypt/cryptclasscaesar.cpp
@@ -1,11 +1,12 @@
#include "cryptclasscaesar.h"
-const int CryptClassCaesar::LOWERBOUND = 0x30;
-const int CryptClassCaesar::UPPERBOUND = 0x7A;
+const int CryptClassCaesar::LOWERBOUND = 'A';
+const int CryptClassCaesar::UPPERBOUND = 'Z';
CryptClassCaesar::CryptClassCaesar()
{
- oldKey = -1;
+ mapOffset = 0;
+ buildMap();
}
CryptClassCaesar::~CryptClassCaesar()
@@ -16,11 +17,10 @@ CryptClassCaesar::~CryptClassCaesar()
void CryptClassCaesar::encrypt()
{
qDebug("CryptClassCaesar::encrypt");
- buildMap();
+ m_clearText = m_clearText.toUpper();
m_cryptText.clear();
-
- m_clearText = stripUmlauts(m_clearText);
+ mapOffset = 0;
int spaceCount = 0;
@@ -28,7 +28,8 @@ void CryptClassCaesar::encrypt()
{
if (m_clearText[i] >= (char) LOWERBOUND && m_clearText[i] <= (char) UPPERBOUND)
{
- m_cryptText.append(substitutionsMap[m_clearText[i]]);
+ m_cryptText.append(substitutionsMap[LOWERBOUND + (((m_clearText[i] - LOWERBOUND)
+ + mapOffset++) % (26))]);
if (!(++spaceCount % 5))
{
@@ -42,57 +43,43 @@ void CryptClassCaesar::encrypt()
void CryptClassCaesar::decrypt()
{
qDebug("CryptClassCaesar::decrypt");
- buildMap();
- m_clearText.clear();
+ int tmp = 0;
+
+ m_cryptText = m_cryptText.toUpper();
- m_cryptText = stripUmlauts(m_cryptText);
+ 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)
{
- m_clearText.append(substitutionsMap.key(m_cryptText[i]));
+ 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()
{
- bool ok = false;
-
- if (oldKey == getKey().toInt(&ok))
- {
- if (ok == false)
- {
- QMessageBox::warning(NULL, "Key invalid",
- "The key for Ceasar should only be a number.");
- }
-
- return;
- }
-
- oldKey = getKey().toInt();
+ char tmp[] = "EKMFLGDQVZNTOWYHXUSPAIBRCJ";
substitutionsMap.clear();
for (int i = 0; i <= (UPPERBOUND - LOWERBOUND); i++)
{
- substitutionsMap.insert((char)(LOWERBOUND + i),
- (char)(LOWERBOUND + (i + oldKey) % (UPPERBOUND - LOWERBOUND)));
-
+ substitutionsMap.insert((char)(LOWERBOUND + i), tmp[i]);
}
}
-
-QByteArray CryptClassCaesar::stripUmlauts(QByteArray umlautText)
-{
- umlautText.replace(QByteArray("ß"), QByteArray("SS"));
-
- umlautText = umlautText.toUpper();
-
- umlautText.replace(QByteArray("Ä"), QByteArray("AE"));
- umlautText.replace(QByteArray("Ö"), QByteArray("OE"));
- umlautText.replace(QByteArray("Ü"), QByteArray("UE"));
-
- return umlautText;
-}
diff --git a/src/crypt/cryptclasscaesar.h b/src/crypt/cryptclasscaesar.h
index 899136b..95fc479 100644
--- a/src/crypt/cryptclasscaesar.h
+++ b/src/crypt/cryptclasscaesar.h
@@ -20,12 +20,11 @@ public:
private:
void buildMap();
- int oldKey;
+ int mapOffset;
QMap<char, char> substitutionsMap;
const static int LOWERBOUND;
const static int UPPERBOUND;
- QByteArray stripUmlauts(QByteArray umlautText);
};
#endif // CRYPTCLASSCAESAR_H