Загрузка данных
int CIPHER() {
system("cls");
info();
cout << "==========================================\n";
cout << " ШИФРОВАНИЕ/ДЕШИФРОВАНИЕ\n";
cout << "==========================================\n\n";
string text, original;
cout << "Введите текст: ";
if (!ReadStringWithESC(text)) return 0;
original = text;
int lastMethod = 0; // 1-Цезарь, 2-Побитовый, 3-Динамический
int lastShift = 3; // последний использованный сдвиг для Цезаря
while (true) {
system("cls");
info();
cout << "\n==========================================\n";
cout << "ТЕКУЩИЙ ТЕКСТ: " << text << "\n";
cout << "==========================================\n";
cout << "1 - Зашифровать (Цезарь)\n";
cout << "2 - Зашифровать (Побитовый)\n";
cout << "3 - Зашифровать (Динамический)\n";
cout << "4 - Расшифровать (последним методом)\n";
cout << "5 - Сбросить к исходному\n";
cout << "0 - Выход в меню\n";
cout << "Выбор: ";
char ch = _getch(); cout << ch << endl;
if (ch == '0') break;
string res = text;
int len = text.length();
if (ch == '1') { // Цезарь
cout << "Введите сдвиг: ";
if (!ReadIntWithESC(lastShift)) 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 + lastShift) % 26;
}
}
text = res;
lastMethod = 1;
cout << "\nГотово!\n";
}
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;
lastMethod = 2;
delete[] tmp;
cout << "\nГотово!\n";
}
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;
lastMethod = 3;
cout << "\nГотово!\n";
}
else if (ch == '4') { // Расшифровать последним методом
if (lastMethod == 0) {
cout << "Сначала зашифруйте что-нибудь!\n";
_getch();
continue;
}
res = text;
if (lastMethod == 1) { // Цезарь
for (int i = 0; i < len; i++) {
if (isalpha(res[i])) {
char base = isupper(res[i]) ? 'A' : 'a';
res[i] = base + (res[i] - base - lastShift + 26) % 26;
}
}
}
else if (lastMethod == 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];
}
delete[] tmp;
}
else if (lastMethod == 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;
cout << "\nРасшифровано!\n";
}
else if (ch == '5') {
text = original;
lastMethod = 0;
cout << "\nСброшено к исходному!\n";
}
else {
cout << "\nНеверный выбор!\n";
}
_getch(); // пауза, чтобы пользователь увидел сообщение
}
return 0;
}