-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (59 loc) · 2.26 KB
/
Copy pathmain.cpp
File metadata and controls
71 lines (59 loc) · 2.26 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
66
67
68
69
70
71
#include <cctype> //for checking with std::isalpha()
#include <cstdint> // for uint8_t and toLower
#include <iostream> //the ins and outs
#include <string> //c++ is goated wilson
#include <vector> //Hmm i wonder what this does
//TODO: add a decrypter to this and allow user to switch between
//! Bad practice ik, fuck you
using namespace std;
//* maps a string and highlights the indexes of "message"; where each character in message is stored in alphabet. then it returns mapped_message for later use
vector<uint8_t> map(const string& message, const vector<unsigned char>& alphabet) {
vector<uint8_t> mapped_message;
mapped_message.reserve(message.size());
for (char ch : message) {
bool found = false;
for (unsigned char alpha_char : alphabet) {
if (tolower(static_cast<unsigned char>(ch)) == alpha_char) {
mapped_message.push_back(static_cast<uint8_t>(tolower(static_cast<unsigned char>(ch))));
found = true;
break;
}
}
if (!found) {
mapped_message.push_back(static_cast<uint8_t>(ch));
}
}
return mapped_message;
}
//* shifts and vector by a certain amount, and returns the value
vector<uint8_t> shift_sequence(const vector<uint8_t>& values, int shift_amount) {
vector<uint8_t> shifted;
shifted.reserve(values.size());
for (uint8_t value : values) {
if (isalpha(value)) {
char ch = static_cast<char>(value);
char base = islower(ch) ? 'a' : 'A';
int position = ch - base;
int new_position = (position + shift_amount + 26) % 26;
shifted.push_back(static_cast<uint8_t>(base + new_position));
} else {
shifted.push_back(value);
}
}
return shifted;
}
int main() {
vector<unsigned char> alpha = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int shift = 2;
string message;
cout << "Please enter your message: ";
getline(cin, message);
//gets shifted sequence which will be used to display or encrypted message.
vector<uint8_t> mapped_message = map(message, alpha);
vector<uint8_t> shifted_message = shift_sequence(mapped_message, shift);
cout << "Encrypted message: ";
for (uint8_t value : shifted_message) {
cout << static_cast<char>(value);
}
cout << '\n';
}