fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6. string vigenere(string text, string key, bool decrypt = false) {
  7. string result = "";
  8. int j = 0;
  9. int dir = decrypt ? -1 : 1;
  10.  
  11. // Make key uppercase
  12. for (char &c : key) c = toupper(c);
  13.  
  14. for (char c : text) {
  15. if (isalpha(c)) {
  16. char base = isupper(c) ? 'A' : 'a';
  17. int shift = key[j % key.size()] - 'A';
  18. result += (c - base + dir * shift + 26) % 26 + base;
  19. j++;
  20. } else {
  21. result += c;
  22. }
  23. }
  24.  
  25. return result;
  26. }
  27.  
  28. int main() {
  29. string key, msg;
  30.  
  31. cout << "Enter key: ";
  32. cin >> key;
  33. cin.ignore();
  34.  
  35. cout << "Enter message: ";
  36. getline(cin, msg);
  37.  
  38. string enc = vigenere(msg, key);
  39. string dec = vigenere(enc, key, true);
  40.  
  41. cout << "Encrypted: " << enc << endl;
  42. cout << "Decrypted: " << dec << endl;
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Enter key: Enter message: Encrypted: 
Decrypted: