Загрузка данных


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++) {
                unsigned char ch = txt[i];
                if (ch >= 'A' && ch <= 'Z')
                    res[i] = 'A' + (ch - 'A' + lastShift) % 26;
                else if (ch >= 'a' && ch <= 'z')
                    res[i] = 'a' + (ch - 'a' + lastShift) % 26;
                else if (ch >= 192 && ch <= 223) // А-Я
                    res[i] = 192 + (ch - 192 + lastShift) % 32;
                else if (ch >= 224 && ch <= 255) // а-я
                    res[i] = 224 + (ch - 224 + lastShift) % 32;
            }
            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++) {
                unsigned char ch = txt[i];
                int shift = (i + 1) % 26;
                if (ch >= 'A' && ch <= 'Z')
                    res[i] = 'A' + (ch - 'A' + shift) % 26;
                else if (ch >= 'a' && ch <= 'z')
                    res[i] = 'a' + (ch - 'a' + shift) % 26;
                else if (ch >= 192 && ch <= 223)
                    res[i] = 192 + (ch - 192 + shift) % 32;
                else if (ch >= 224 && ch <= 255)
                    res[i] = 224 + (ch - 224 + shift) % 32;
            }
            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++) {
                    unsigned char ch = txt[i];
                    if (ch >= 'A' && ch <= 'Z')
                        res[i] = 'A' + (ch - 'A' - lastShift + 26) % 26;
                    else if (ch >= 'a' && ch <= 'z')
                        res[i] = 'a' + (ch - 'a' - lastShift + 26) % 26;
                    else if (ch >= 192 && ch <= 223)
                        res[i] = 192 + (ch - 192 - lastShift + 32) % 32;
                    else if (ch >= 224 && ch <= 255)
                        res[i] = 224 + (ch - 224 - lastShift + 32) % 32;
                }
            }
            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++) {
                    unsigned char ch = txt[i];
                    int shift = (i + 1) % 26;
                    if (ch >= 'A' && ch <= 'Z')
                        res[i] = 'A' + (ch - 'A' - shift + 26) % 26;
                    else if (ch >= 'a' && ch <= 'z')
                        res[i] = 'a' + (ch - 'a' - shift + 26) % 26;
                    else if (ch >= 192 && ch <= 223)
                        res[i] = 192 + (ch - 192 - shift + 32) % 32;
                    else if (ch >= 224 && ch <= 255)
                        res[i] = 224 + (ch - 224 - shift + 32) % 32;
                }
            }
            txt = res;
            lastMethod = 0;
        }
    } while (true);
    
    return 0;
}