-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdecryption.cpp
More file actions
66 lines (56 loc) · 2.56 KB
/
decryption.cpp
File metadata and controls
66 lines (56 loc) · 2.56 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//============================================================================
// Name : decryption.cpp
// Author : Ak0s
// Description : Exam 3
//============================================================================
#include "decryption.hpp"
const int UPPER_CASE_MIN = 65;
const int UPPER_CASE_MAX = 90;
const int LOWER_CASE_MIN = 97;
const int LOWER_CASE_MAX = 122;
bool is_alphabetic(std::string source_text, int case_min, int case_max, int i);
bool is_overflowing(std::string source_text, int case_min, int case_max, int key, int i);
int overflow_shift_if_key_positive(std::string source_text, int case_min, int case_max, int key, int i);
int overflow_shift_if_key_negative(std::string source_text, int case_min, int case_max, int key, int i);
void Decryption::decrypt_text(std::string source_text, int key) {
for (unsigned int i = 0; i < source_text.length(); i++) {
if (is_alphabetic(source_text,UPPER_CASE_MIN, UPPER_CASE_MAX, i)) {
if (is_overflowing(source_text, UPPER_CASE_MIN, UPPER_CASE_MAX, key, i)) {
source_text[i] += key;
}
else if (key > 0){
source_text[i] = overflow_shift_if_key_positive(source_text, UPPER_CASE_MIN, UPPER_CASE_MAX, key, i);
}
else {
source_text[i] = overflow_shift_if_key_negative(source_text, UPPER_CASE_MIN, UPPER_CASE_MAX, key, i);
}
}
if (is_alphabetic(source_text,LOWER_CASE_MIN, LOWER_CASE_MAX, i)) {
if (is_overflowing(source_text, LOWER_CASE_MIN, LOWER_CASE_MAX, key, i)) {
source_text[i] += key;
}
else if (key > 0) {
source_text[i] = overflow_shift_if_key_positive(source_text, LOWER_CASE_MIN, LOWER_CASE_MAX, key, i);
}
else {
source_text[i] = overflow_shift_if_key_negative(source_text, LOWER_CASE_MIN, LOWER_CASE_MAX, key, i);
}
}
}
decrypted_text = source_text;
}
bool is_alphabetic(std::string source_text, int case_min, int case_max, int i) {
return (source_text[i] >= case_min and source_text[i] <= case_max);
}
bool is_overflowing(std::string source_text, int case_min, int case_max, int key, int i) {
return source_text[i] + key > case_min and source_text[i] + key < case_max;
}
int overflow_shift_if_key_positive(std::string source_text, int case_min, int case_max, int key, int i) {
return case_min + key - (case_max - source_text[i]) - 1;
}
int overflow_shift_if_key_negative(std::string source_text, int case_min, int case_max, int key, int i) {
return case_max + (case_min - source_text[i] - key) - 1;
}
std::string Decryption::get_decrypted_text() {
return decrypted_text;
}