Загрузка данных
int CIPHER() {
system("cls");
info();
cout << "************************************\n";
cout << "* ШИФРОВАНИЕ/ДЕШИФРОВАНИЕ *\n";
cout << "************************************\n";
cout << "* 1. Цезарь (сдвиг букв на 3) *\n";
cout << "* 2. Побитовый сдвиг *\n";
cout << "* 3. Динамический (сдвиг на позицию)*\n";
cout << "************************************\n";
cout << "Выберите пункт: ";
char choice = _getch();
cout << choice << endl;
cin.ignore();
string input;
int len;
if (choice == '1') {
cout << "Введите текст: ";
if (!ReadStringWithESC(input)) return 0;
len = (int)input.length();
// Копируем в массив для совместимости с остальным кодом
unsigned char* txt = new unsigned char[len + 1];
for (int i = 0; i < len; i++) txt[i] = input[i];
txt[len] = '\0';
int shift = 3;
cout << "Выберите: 1 - зашифровать, 2 - расшифровать: ";
char op = _getch(); cout << op << endl;
if (op == '1') {
for (int i = 0; i < len; i++)
if (isalpha(txt[i])) {
char base = isupper(txt[i]) ? 'A' : 'a';
txt[i] = base + (txt[i] - base + shift) % 26;
}
cout << "Зашифрованный текст: " << txt << endl;
} else if (op == '2') {
for (int i = 0; i < len; i++)
if (isalpha(txt[i])) {
char base = isupper(txt[i]) ? 'A' : 'a';
txt[i] = base + (txt[i] - base - shift + 26) % 26;
}
cout << "Расшифрованный текст: " << txt << endl;
} else cout << "Неверный выбор!" << endl;
delete[] txt;
}
else if (choice == '2') {
cout << "Введите текст: ";
if (!ReadStringWithESC(input)) return 0;
len = (int)input.length();
unsigned char* txt = new unsigned char[len + 1];
for (int i = 0; i < len; i++) txt[i] = input[i];
txt[len] = '\0';
cout << "Выберите: 1 - зашифровать, 2 - расшифровать: ";
char op = _getch(); cout << op << endl;
if (op == '1') {
if (len == 0) { cout << "Пустая строка!" << endl; _getch(); delete[] txt; return 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;
}
cout << "Зашифрованный текст: " << txt << endl;
} else if (op == '2') {
if (len == 0) { cout << "Пустая строка!" << endl; _getch(); delete[] txt; return 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);
}
cout << "Расшифрованный текст: " << txt << endl;
} else cout << "Неверный выбор!" << endl;
delete[] txt;
}
else if (choice == '3') {
cout << "Введите текст: ";
if (!ReadStringWithESC(input)) return 0;
len = (int)input.length();
cout << "Выберите: 1 - зашифровать, 2 - расшифровать: ";
char op = _getch(); cout << op << endl;
// Динамический сдвиг (на номер позиции)
if (op == '1' || op == '2') {
bool encrypt = (op == '1');
string result = input;
for (int i = 0; i < len; i++) {
char c = result[i];
if (isalpha(c)) {
char base = isupper(c) ? 'A' : 'a';
int shift = i + 1; // номер позиции (начинается с 1)
if (!encrypt) shift = -shift;
result[i] = base + (c - base + shift + 26) % 26;
}
}
cout << (encrypt ? "Зашифрованный" : "Расшифрованный") << " текст: " << result << endl;
} else {
cout << "Неверный выбор!" << endl;
}
}
else {
cout << "Неверный пункт меню!" << endl;
}
_getch();
return 0;
}