Загрузка данных
int CIPHER() {
system("cls");
info();
cout << "===========================================================\n";
cout << " ШИФРОВАНИЕ/ДЕШИФРОВАНИЕ \n";
cout << "===========================================================\n\n";
string text, original;
cout << "Введите текст: ";
if (!ReadStringWithESC(text)) return 0;
original = text;
int method = 0, shift = 3;
bool encrypted = false;
char ch;
do {
system("cls");
info();
cout << "\n===========================================================\n";
cout << "Текст: " << text << "\n";
cout << "===========================================================\n";
if (!encrypted) {
cout << "1 - Цезарь\n2 - Побитовый\n3 - Динамический\n0 - Выход\n";
} else {
cout << "4 - Расшифровать\n5 - Сброс\n0 - Выход\n";
}
cout << "Выбор: ";
ch = _getch(); cout << ch << endl;
if (ch == '0') break;
if (!encrypted) {
string res = text;
int len = text.length();
if (ch == '1') {
cout << "Введите сдвиг: ";
if (!ReadIntWithESC(shift)) return 0;
for (int i = 0; i < len; i++)
if (isalpha(res[i])) {
char base = isupper(res[i]) ? 'A' : 'a';
res[i] = base + (res[i] - base + shift) % 26;
}
text = res;
method = 1;
encrypted = true;
}
else if (ch == '2') {
if (len == 0) continue;
unsigned char* tmp = new unsigned char[len+1];
for (int i = 0; i < len; i++) tmp[i] = text[i];
tmp[len] = 0;
unsigned char first = (tmp[0] & 0x80) >> 7;
for (int i = 0; i < len; i++) {
unsigned char next = (i < len-1) ? (tmp[i+1] & 0x80) >> 7 : first;
tmp[i] = (tmp[i] << 1) | next;
res[i] = tmp[i];
}
text = res;
method = 2;
encrypted = true;
delete[] tmp;
}
else if (ch == '3') {
for (int i = 0; i < len; i++)
if (isalpha(res[i])) {
char base = isupper(res[i]) ? 'A' : 'a';
res[i] = base + (res[i] - base + i + 1) % 26;
}
text = res;
method = 3;
encrypted = true;
}
}
else {
string res = text;
int len = text.length();
if (ch == '4') {
if (method == 1) {
cout << "Введите сдвиг: ";
if (!ReadIntWithESC(shift)) return 0;
for (int i = 0; i < len; i++)
if (isalpha(res[i])) {
char base = isupper(res[i]) ? 'A' : 'a';
res[i] = base + (res[i] - base - shift + 26) % 26;
}
text = res;
}
else if (method == 2) {
if (len == 0) continue;
unsigned char* tmp = new unsigned char[len+1];
for (int i = 0; i < len; i++) tmp[i] = text[i];
tmp[len] = 0;
unsigned char lastBit = (tmp[len-1] & 1);
for (int i = len-1; i >= 0; i--) {
unsigned char prevBit = (i > 0) ? (tmp[i-1] & 1) : lastBit;
tmp[i] = (tmp[i] >> 1) | (prevBit << 7);
res[i] = tmp[i];
}
text = res;
delete[] tmp;
}
else if (method == 3) {
for (int i = 0; i < len; i++)
if (isalpha(res[i])) {
char base = isupper(res[i]) ? 'A' : 'a';
res[i] = base + (res[i] - base - (i + 1) + 26) % 26;
}
text = res;
}
encrypted = false;
method = 0;
}
else if (ch == '5') {
text = original;
encrypted = false;
method = 0;
}
}
} while (true);
return 0;
}