lab2

 #include <iostream>

#include <string>

#include <unordered_map>

#include <cctype>

using namespace std;

string encMono(const string& txt, const string& key) {

string res = "";

unordered_map<char, char> mp;

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

mp['a' + i] = tolower(key[i]);

mp['A' + i] = toupper(key[i]);

}

for (char ch : txt)

res += isalpha(ch) ? mp[ch] : ch;

return res;

}

string decMono(const string& txt, const string& key) {

string res = "";

unordered_map<char, char> mp;

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

mp[tolower(key[i])] = 'a' + i;

mp[toupper(key[i])] = 'A' + i;

}

for (char ch : txt)

res += isalpha(ch) ? mp[ch] : ch;

return res;

}

bool validKey(const string& key) {

if (key.size() != 26) return false;

bool seen[26] = {false};

for (char ch : key) {

if (!isalpha(ch)) return false;

int idx = tolower(ch) - 'a';

if (seen[idx]) return false;

seen[idx] = true;

}

return true;

}

int main() {

string txt, key,enc;

int opt;

cout << "Enter 26-le er key: ";

getline(cin, key);

if (!validKey(key)) {

cout << "Invalid key. Must be 26 unique le ers.\n";

return 1;

}

cout << "Enter text: ";

getline(cin, txt);

enc=encMono(txt, key);

cout << "Encrypted: " << enc << endl;

cout << "Decrypted: " << decMono(enc, key) << endl;

return 0;

}

Comments

Popular posts from this blog

lab1

lab8

lab9