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