lab6a

#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);

}


int main() {

    string plaintext, key, encryptedText, userCipherText, userKey;

    cout << "Enter the plaintext: ";

    getline(cin, plaintext);

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

    cin >> key;

    encryptedText = transpositionEncrypt(plaintext, key);

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

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

    cin >> userCipherText;

    cout << "Enter the decryption key: ";

    cin >> userKey;

    string decryptedText = transpositionDecrypt(userCipherText, userKey);

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

    return 0;

}


Comments

Popular posts from this blog

lab7

lab12(euler)

lab6b