-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciphers.cpp
160 lines (130 loc) · 4.11 KB
/
ciphers.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* \file ciphers.cpp
*
* Ebben a fájlban kell megvalósítania az CaesarCipher, MyCipher, CipherQueue osztályok metódusait.
*
* Ezt a fájlt be kell adni (fel kell tölteni) a megoldással.
*/
#include "memtrace.h"
#include "ciphers.h"
/// CaesarCipher függvények
std::string CaesarCipher::encode(const std::string& message){
std::string::const_iterator begin = message.begin();
std::string::const_iterator end = message.end();
std::string uj;
while (begin != end){
if (*begin != ' ' && (int(*begin) < 97 || int(*begin) > 122))
throw "X079FB";
if (*begin == ' ')
uj.push_back(' ');
else if (shift + int(*begin) > 122)
uj.push_back(97 + shift + int(*begin) - 123);
else
uj.push_back(shift + int(*begin));
++begin;
}
return uj;
}
std::string CaesarCipher::decode(const std::string& ciphertext){
std::string::const_iterator begin = ciphertext.begin();
std::string::const_iterator end = ciphertext.end();
std::string uj;
while (begin != end){
if (*begin != ' ' && (int(*begin) < 97 || int(*begin) > 122))
throw "X079FB";
if (*begin == ' ')
uj.push_back(' ');
else if (int(*begin) - shift < 97)
uj.push_back(-97 + int(*begin) - shift + 123);
else
uj.push_back(int(*begin) - shift);
++begin;
}
return uj;
}
Cipher* CaesarCipher::clone() const{
return new CaesarCipher(*this);
}
/// MyCipher függvények
std::string MyCipher::encode(const std::string& message){
std::string::const_iterator begin = message.begin();
std::string::const_iterator end = message.end();
std::string uj;
int i = 0, j = 0;
while (begin != end){
if (*begin != ' ' && (int(*begin) < 97 || int(*begin) > 122))
throw "X079FB";
if (*begin == ' ')
uj.push_back(' ');
else if (int(key[i]) - 97 + offset + j + int(*begin) > 122)
uj.push_back(((int(key[i]) - 97 + offset + j + int(*begin) - 97) % 26) + 97);
else
uj.push_back(int(key[i]) - 97 + offset + j + int(*begin));
++i;
++j;
++begin;
if (i >= int(key.size()))
i = 0;
}
return uj;
}
std::string MyCipher::decode(const std::string& ciphertext){
std::string::const_iterator begin = ciphertext.begin();
std::string::const_iterator end = ciphertext.end();
std::string uj;
int i = 0, j = 0;
while (begin != end){
if (*begin != ' ' && (int(*begin) < 97 || int(*begin) > 122))
throw "X079FB";
if (*begin == ' ')
uj.push_back(' ');
else if (-int(key[i]) + 97 - j + int(*begin) - offset < 97)
uj.push_back(((26-abs((-int(key[i]) - j + int(*begin) - offset) % 26)) % 26) + 97);
else
uj.push_back(-int(key[i]) + 97 - j + int(*begin) - offset);
++i;
++j;
++begin;
if (i >= int(key.size()))
i = 0;
}
return uj;
}
Cipher* MyCipher::clone() const{
return new MyCipher(*this);
}
/// CipherQueue függvények
void CipherQueue::add(Cipher* cipher){
ciphers.push_back(cipher);
}
std::string CipherQueue::encode(const std::string& message){
std::vector<Cipher*>::iterator begin = ciphers.begin();
std::vector<Cipher*>::iterator end = ciphers.end();
std::string uj = message;
while (begin != end){
uj = (*begin++)->encode(uj);
}
return uj;
}
std::string CipherQueue::decode(const std::string& ciphertext){
std::vector<Cipher*>::iterator end = ciphers.begin();
--end;
std::vector<Cipher*>::iterator begin = ciphers.end();
--begin;
std::string uj = ciphertext;
while (begin != end){
uj = (*begin--)->decode(uj);
}
return uj;
}
Cipher* CipherQueue::clone() const{
CipherQueue* uj = new CipherQueue;
uj->ciphers = this->ciphers;
for (size_t i = 0; i < this->ciphers.size(); ++i)
uj->ciphers[i] = this->ciphers[i]->clone();
return uj;
}
CipherQueue::~CipherQueue(){
for (size_t i = 0; i < ciphers.size(); ++i)
delete ciphers[i];
}