lab3

#include <iostream>

#include <string>

using namespace std;

string transform(const string& txt, const string& key, bool encrypt) {

string res = "";

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

char t = txt[i];

if (isalpha(t)) {

char base = isupper(t) ? 'A' : 'a';

char k = tolower(key[i % key.size()]) - 'a';

int shi = encrypt ? k : -k;

res += (char)(((tolower(t) - 'a' + shi + 26) % 26) + base);

} else {

res += t;

}

}

return res;

}

int main() {

string txt, key,en;

int opt;

cout << "=== Polyalphabe c (Vigenère) Cipher ===\n";

cout << "Enter the text: ";

getline(cin, txt);

cout << "Enter the key (only le ers): ";

getline(cin, key);

for (char c : key) {

if (!isalpha(c)) {

cout << "Key must only contain le ers.\n";

return 1;

}

}

en=transform(txt, key, true);

cout << "Encrypted: " << en << endl;

cout << "Decrypted: " << transform(en, key, false) << endl;

return 0;

}

Comments

Popular posts from this blog

lab1

lab8

lab9