-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.cpp
More file actions
43 lines (35 loc) · 1.44 KB
/
Copy pathCaesarCipher.cpp
File metadata and controls
43 lines (35 loc) · 1.44 KB
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
#include <cstring>
#include "CipherAlgorithm.h"
#include "CipherView.h"
#include "CaesarCipher.h"
#ifdef MEMTRACE
#include "memtrace.h"
#endif
CaesarCipher::CaesarCipher(int shift) : shift(shift) {}
char CaesarCipher::transform(Algorithm::Mode cipher_mode, char unciphered_char) const {
int cipher = unciphered_char;
if (cipher_mode == Algorithm::Mode::encrypt)
return (cipher + shift) % 256;
else
return (cipher - shift) % 256;
}
void CaesarCipher::append_ciphered(CipherView &destination, ConstCipherView source) const {
for (size_t i = 0; i < source.len; ++i)
destination.data[i + destination.len] = source.data[i];
destination.len = destination.len + source.len;
}
void CaesarCipher::append_unciphered(CipherView &destination, ConstCipherView source) const {
for (size_t i = 0; i < source.len; ++i)
destination.data[i + destination.len] = transform(Algorithm::Mode::encrypt, source.data[i]);
destination.len = destination.len + source.len;
}
bool CaesarCipher::is_compatible_with(Algorithm* other_cipher) const {
// dynamic cast then check whether algo specific things match (key, for caesar shift)
// then cipher shall decipher if this is false and then append unciphered
CaesarCipher* cast_other_cipher = dynamic_cast<CaesarCipher*>(other_cipher);
if (cast_other_cipher == nullptr) return false;
return cast_other_cipher->shift == this->shift;
}
Algorithm* CaesarCipher::clone() const {
return new CaesarCipher(shift);
}