lab7

#include <iostream>

#include <string>

using namespace std;


string xorOperation(const string &text, const string &key) {

    string result = text;

    for (size_t i = 0; i < text.size(); i++) {

        result[i] = text[i] ^ key[i % key.size()];

    }

    return result;

}


string encryptDES(const string &plainText, const string &key) {

    return xorOperation(plainText, key);

}


string decryptDES(const string &encryptedText, const string &key) {

    return xorOperation(encryptedText, key);

}


int main() {

    string plainText, key;

    cout << "Enter a plain text sentence or paragraph: ";

    getline(cin, plainText);

    cout << "Enter a key (word or sentence): ";

    getline(cin, key);

    string encryptedText = encryptDES(plainText, key);

    cout << "Encrypted Message: ";

    for (char c : encryptedText) {

        cout << hex << (int)(unsigned char)c << " ";

    }

    cout << endl;

    string inputEncryptedText, inputKey;

    cout << "\nEnter the encrypted message exactly as shown above: ";

    getline(cin, inputEncryptedText);

    cout << "Enter the decryption key: ";

    getline(cin, inputKey);

    string decryptedText = decryptDES(encryptedText, inputKey);

    if (decryptedText == plainText) {

        cout << "Decryption successful! Decrypted Message: " << decryptedText << endl;

    } else {

        cout << "Decryption failed! The decrypted message does not match the original plaintext." << endl;

    }

    return 0;

}


Comments

Popular posts from this blog

lab12(euler)

lab6b