#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string vigenere(string text, string key, bool decrypt = false) {
    string result = "";
    int j = 0;
    int dir = decrypt ? -1 : 1;

    // Make key uppercase
    for (char &c : key) c = toupper(c);

    for (char c : text) {
        if (isalpha(c)) {
            char base = isupper(c) ? 'A' : 'a';
            int shift = key[j % key.size()] - 'A';
            result += (c - base + dir * shift + 26) % 26 + base;
            j++;
        } else {
            result += c;
        }
    }

    return result;
}

int main() {
    string key, msg;

    cout << "Enter key: ";
    cin >> key;
    cin.ignore();

    cout << "Enter message: ";
    getline(cin, msg);

    string enc = vigenere(msg, key);
    string dec = vigenere(enc, key, true);

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

    return 0;
}
