-
Notifications
You must be signed in to change notification settings - Fork 0
/
VigenereCLI.cpp
62 lines (60 loc) · 2.01 KB
/
VigenereCLI.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
#include <bits/stdc++.h>
using namespace std;
int main(){
setlocale(LC_ALL,"Rus");
vector <char> all_chars;
for (char cur_char = 'A';cur_char <= 'Z';cur_char++){
all_chars.push_back(cur_char);
}
map <char,int> number_for_char;
for (int i = 0;i < all_chars.size();i++){
number_for_char[all_chars[i]] = i;
}
std::cout << "Введите сообщение, которое нужно зашифровать:\n";
string text;
getline(cin,text);
std::cout << "Введите ключ шифрования:\n";
string key;
std::cin >> key;
for (int i = 0;i < text.size();i++){
if (text[i] >= 'a' && text[i] <= 'z'){
text[i] = toupper(text[i]);
}
}
for (int i = 0;i < key.size();i++){
if (key[i] >= 'a' && key[i] <= 'z'){
key[i] = toupper(key[i]);
}
}
string pattern = "";
int pos = 0;
for (auto ch : text){
if (ch >= 'A' && ch <= 'Z'){
pattern += key[pos];
pos++;
if (pos == key.size()){
pos = 0;
}
continue;
}
pattern += ch;
}
string encrypted_text = "";
for (int i = 0;i < text.size();i++){
if (text[i] >= 'A' && text[i] <= 'Z'){
encrypted_text += all_chars[(number_for_char[text[i]] + number_for_char[pattern[i]]) % all_chars.size()];
continue;
}
encrypted_text += text[i];
}
string decrypted_text = "";
for (int i = 0;i < encrypted_text.size();i++){
if (encrypted_text[i] >= 'A' && encrypted_text[i] <= 'Z'){
decrypted_text += all_chars[(number_for_char[encrypted_text[i]] - number_for_char[pattern[i]] + all_chars.size()) % all_chars.size()];
continue;
}
decrypted_text += encrypted_text[i];
}
std::cout << "Зашифрованное сообщение - " << encrypted_text << "\n";
std::cout << "Расшифрованное сообщение - " << decrypted_text;
}