Загрузка данных
int CIPHER() {
system("cls");
info();
cout << "\n========== ШИФРОВАНИЕ ==========\n";
string txt, orig;
cout << "Текст: ";
if (!ReadStringWithESC(txt)) return 0;
orig = txt;
int lastMethod = 0, lastShift = 3;
char c;
do {
system("cls");
info();
cout << "\nТЕКСТ: " << txt << "\n\n";
cout << "1. Цезарь\n2. Побитовый\n3. Динамический\n4. Расшифровать (последним)\n5. Сброс\n0. Выход\n";
c = _getch(); cout << c << "\n";
if (c == '0') break;
if (c == '5') { txt = orig; lastMethod = 0; continue; }
string res = txt;
int len = txt.length();
// ---------- ШИФРОВАНИЕ ----------
if (c == '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;
}
txt = res;
lastMethod = 1;
}
else if (c == '2') { // Побитовый
if (len == 0) continue;
unsigned char* tmp = new unsigned char[len+1];
for (int i = 0; i < len; i++) tmp[i] = txt[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];
}
txt = res;
lastMethod = 2;
delete[] tmp;
}
else if (c == '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;
}
txt = res;
lastMethod = 3;
}
// ---------- РАСШИФРОВАНИЕ ----------
else if (c == '4') {
if (lastMethod == 0) { cout << "Сначала зашифруйте!\n"; _getch(); continue; }
res = txt;
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] = txt[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;
}
}
txt = res;
lastMethod = 0;
}
else continue;
cout << "Готово!\n";
_getch();
} while (true);
return 0;
}