-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar.cpp
47 lines (36 loc) · 922 Bytes
/
Caesar.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
#ifndef CAESAR_C
#define CAESAR_C
#include "StreamCipher.cpp"
class Caesar : public StreamCipher {
private:
int shift;
public:
Caesar(int shift) {
this->shift = shift;
}
std::string encrypt(std::string plain) override {
plain = toLowerCase(plain);
std::cout << plain << std::endl;
std::string cipher;
for (int i = 0; i < plain.size(); i++) {
if (plain[i] == ' ' || plain[i] == '\n' || plain[i] == '\t') {
cipher.push_back(plain[i]);
continue;
}
cipher.push_back('a' + (((plain[i] - 'a') + shift) % 26));
}
return cipher;
}
std::string decrypt(std::string cipher) override {
std::string plain;
for (int i = 0; i < cipher.size(); i++) {
if (cipher[i] == ' ' || cipher[i] == '\n' || cipher[i] == '\t') {
plain.push_back(cipher[i]);
continue;
}
plain.push_back('a' + (((cipher[i] - 'a') - shift + 26) % 26));
}
return plain;
}
};
#endif // !CAESAR_C