-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonoAlphabetic.cpp
71 lines (68 loc) · 1.68 KB
/
monoAlphabetic.cpp
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
#include "MonoAlphabetic.h"
MonoAlphabetic::MonoAlphabetic(std::string key)
{
MESSAGE_FILE_ADDR = "MessageFiles/CaesarMSG.txt";
ENCRYPTED_FILE_ADDR = "MessageFiles/MonoAlphabeticCRPT.txt";
for (int i = 0; i < key.length(); i++)
{
if (key[i] >= 'A' && key[i] <= 'Z')
{
key[i] += 'a' - 'A';
}
}
key += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < key.length(); i++)
{
if (!(key[i] >= 'A' && key[i] <= 'Z') && !(key[i] >= 'a' && key[i] <= 'z'))
{
continue;
}
if (key.find(key[i]) == i)
{
mkey += key[i];
if (mkey.length() == 52)
{
break;
}
}
}
ShowAlphabet(mkey);
}
MonoAlphabetic::~MonoAlphabetic()
{
}
void MonoAlphabetic::encrypt(std::string &m, int mLen, std::string &c)
{
c.clear();
for (int i = 0; i < m.length(); i++)
{
if (m[i] >= 'A' && m[i] <= 'Z')
{
c.push_back(mkey[m[i] - 'A' + 26]);
}
else if (m[i] >= 'a' && m[i] <= 'z')
{
c.push_back(mkey[m[i] - 'a']);
}
else
c.push_back(m[i]);
}
}
void MonoAlphabetic::decrypt(std::string &c, int cLen, std::string &m)
{
m.clear();
for (int i = 0; i < c.length(); i++)
{
int index = mkey.find(c[i]);
if (index == std::string::npos)
{
m.push_back(c[i]);
}
else if (index < 26)
{
m.push_back('a' + index);
}
else
m.push_back('A' + index - 26);
}
}