lab7
#include <iostream>
#include <string>
using namespace std;
string xorOp(const string &text, const string &key) {
string res = text;
for (size_t i = 0; i < text.size(); i++)
res[i] = text[i] ^ key[i % key.size()];
return res;
}
int main() {
string pt, key, enc, dec, inKey;
cout << "Enter text: ";
getline(cin, pt);
cout << "Enter key: ";
getline(cin, key);
enc = xorOp(pt, key);
cout << "Encrypted: ";
for (char c : enc) cout << hex << (int)(unsigned char)c << " ";
cout << "\nEnter key to decrypt: ";
getline(cin, inKey);
dec = xorOp(enc, inKey);
cout << (dec == pt ? "Decryption successful: " + dec : "Decryption failed!") << endl;
return 0;
}
Comments
Post a Comment