Ребзя, нужна помощь. Программу по шифровке и расшифровке смог сделать только для латиницы. Теперь хочу, чтобы она работала и с русским алфавитом.
Вот модули программы.
main.cpp
C++ |
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
| #include <iostream>
#include <cctype>
#include "modAlphaCipher.h"
using namespace std;
// проверка, чтобы строка состояла только из прописных букв
bool isValid(const string& s)
{
for(auto c:s)
if (!isalpha(c) || !isupper(c))
return false;
return true;
}
int main(int argc, char **argv)
{
string key;
string text;
unsigned op;
cout<<"Cipher ready. Input key: ";
cin>>key;
if (!isValid(key)) {
cerr<<"key not valid\n";
return 1;
}
cout<<"Key loaded\n";
modAlphaCipher cipher(key);
do {
cout<<"Cipher ready. Input operation (0-exit, 1-encrypt, 2-decrypt): ";
cin>>op;
if (op > 2) {
cout<<"Illegal operation\n";
} else if (op >0) {
cout<<"Cipher ready. Input text: ";
cin>>text;
if (isValid(text)) {
if (op==1) {
cout<<"Encrypted text: "<<cipher.encrypt(text)<<endl;
} else {
cout<<"Decrypted text: "<<cipher.decrypt(text)<<endl;
}
} else {
cout<<"Operation aborted: invalid text\n";
}
}
} while (op!=0);
return 0;
} |
|
modAlphaCipher.cpp
C++ |
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
| #include "modAlphaCipher.h"
modAlphaCipher::modAlphaCipher(const std::string& skey)
{
for (unsigned i=0; i<numAlpha.size(); i++) {
alphaNum[numAlpha[i]]=i;
}
key = convert(skey);
}
std::string modAlphaCipher::encrypt(const std::string& open_text)
{
std::vector<int> work = convert(open_text);
for(unsigned i=0; i < work.size(); i++) {
work[i] = (work[i] + key[i % key.size()]) % alphaNum.size();
}
return convert(work);
}
std::string modAlphaCipher::decrypt(const std::string& cipher_text)
{
std::vector<int> work = convert(cipher_text);
for(unsigned i=0; i < work.size(); i++) {
work[i] = (work[i] + alphaNum.size() - key[i % key.size()]) % alphaNum.size();
}
return convert(work);
}
inline std::vector<int> modAlphaCipher::convert(const std::string& s)
{
std::vector<int> result;
for(auto c:s) {
result.push_back(alphaNum[c]);
}
return result;
}
inline std::string modAlphaCipher::convert(const std::vector<int>& v)
{
std::string result;
for(auto i:v) {
result.push_back(numAlpha[i]);
}
return result;
} |
|
modAlphaCipher.h
C++ |
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
| #pragma once
#include <vector>
#include <string>
#include <map>
class modAlphaCipher
{
private:
std::string numAlpha =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//алфавит по порядку
std::map <char,int> alphaNum;
//ассоциативный массив "номер по символу"
std::vector <int> key;
//ключ
std::vector<int> convert(const std::string& s);
//преобразование строка-вектор
std::string convert(const std::vector<int>& v);
//преобразование вектор-строка
public:
modAlphaCipher()=delete;
//запретим конструктор без параметров
modAlphaCipher(const std::string& skey);
//конструктор для установки ключа
std::string encrypt(const std::string& open_text); //зашифрование
std::string decrypt(const std::string& cipher_text);//расшифрование
}; |
|