blob: d33b5d96408ff63b02d7e52871fbd0e2a27ce2ea (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#include "cryptclasscaesar.h"
const int CryptClassCaesar::LOWERBOUND = 0x30;
const int CryptClassCaesar::UPPERBOUND = 0x7A;
CryptClassCaesar::CryptClassCaesar()
{
oldKey = -1;
buildMap();
}
CryptClassCaesar::~CryptClassCaesar()
{
}
void CryptClassCaesar::encrypt()
{
qDebug("CryptClassCaesar::encrypt");
buildMap();
m_cryptText.clear();
m_clearText = stripUmlauts(m_clearText);
for (int i = 0; i < m_clearText.size(); i++)
{
if (m_clearText[i] >= (char) LOWERBOUND && m_clearText[i] <= (char) UPPERBOUND)
{
m_cryptText.append(substitutionsMap[m_clearText[i]]);
}
}
}
void CryptClassCaesar::decrypt()
{
qDebug("CryptClassCaesar::decrypt");
buildMap();
m_clearText.clear();
m_cryptText = stripUmlauts(m_cryptText);
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]));
}
}
}
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();
substitutionsMap.clear();
for (int i = 0; i <= (UPPERBOUND - LOWERBOUND); i++)
{
substitutionsMap.insert((char)(LOWERBOUND + i),
(char)(LOWERBOUND + (i + oldKey) % (UPPERBOUND - LOWERBOUND)));
}
}
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;
}
|