lab6b

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

using namespace std;


vector<int> getPermutationOrder(string key) {

    vector<pair<char, int>> keyOrder;

    vector<int> order(key.length());

    for (int i = 0; i < key.length(); i++) {

        keyOrder.push_back(make_pair(key[i], i));

    }

    sort(keyOrder.begin(), keyOrder.end());

    for (int i = 0; i < keyOrder.size(); i++) {

        order[keyOrder[i].second] = i;

    }

    return order;

}


string preprocessText(string text) {

    for (char &ch : text) {

        if (ch == ' ') {

            ch = '~';

        }

    }

    return text;

}


string restoreSpaces(string text) {

    for (char &ch : text) {

        if (ch == '~') {

            ch = ' ';

        }

    }

    return text;

}


string transpositionEncrypt(string text, string key) {

    text = preprocessText(text);

    int columns = key.length();

    int rows = (text.length() + columns - 1) / columns;

    vector<vector<char>> grid(rows, vector<char>(columns, '_'));

    int index = 0;

    for (int r = 0; r < rows && index < text.length(); r++) {

        for (int c = 0; c < columns && index < text.length(); c++) {

            grid[r][c] = text[index++];

        }

    }

    vector<int> order = getPermutationOrder(key);

    string ciphertext = "";

    for (int i = 0; i < columns; i++) {

        for (int r = 0; r < rows; r++) {

            ciphertext += grid[r][order[i]];

        }

    }

    return ciphertext;

}


string transpositionDecrypt(string text, string key) {

    int columns = key.length();

    int rows = (text.length() + columns - 1) / columns;

    vector<vector<char>> grid(rows, vector<char>(columns, '_'));

    vector<int> order = getPermutationOrder(key);

    int index = 0;

    for (int i = 0; i < columns; i++) {

        for (int r = 0; r < rows; r++) {

            if (index < text.length()) {

                grid[r][order[i]] = text[index++];

            }

        }

    }

    string plaintext = "";

    for (int r = 0; r < rows; r++) {

        for (int c = 0; c < columns; c++) {

            if (grid[r][c] != '_') {

                plaintext += grid[r][c];

            }

        }

    }

    return restoreSpaces(plaintext);

}


string doubleTranspositionEncrypt(string text, string key1, string key2) {

    string firstPass = transpositionEncrypt(text, key1);

    string secondPass = transpositionEncrypt(firstPass, key2);

    return secondPass;

}


string doubleTranspositionDecrypt(string text, string key1, string key2) {

    string firstPass = transpositionDecrypt(text, key2);

    string secondPass = transpositionDecrypt(firstPass, key1);

    return secondPass;

}


int main() {

    string plaintext, key1, key2, encryptedText, userCipherText, userKey1, userKey2;

    cout << "Enter the plaintext: ";

    getline(cin, plaintext);

    cout << "Enter the first encryption key (numeric string without spaces, e.g., '3214'): ";

    cin >> key1;

    cout << "Enter the second encryption key: ";

    cin >> key2;

    encryptedText = doubleTranspositionEncrypt(plaintext, key1, key2);

    cout << "\nEncrypted Text (Double Transposition): " << encryptedText << endl;

    cout << "\nNow enter the encrypted text to decrypt: ";

    cin >> userCipherText;

    cout << "Enter the first decryption key: ";

    cin >> userKey1;

    cout << "Enter the second decryption key: ";

    cin >> userKey2;

    string decryptedText = doubleTranspositionDecrypt(userCipherText, userKey1, userKey2);

    cout << "\nDecrypted Text: " << decryptedText << endl;

    return 0;

}


Comments

Popular posts from this blog

lab7

lab12(euler)