fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Funkcja szyfrująca tekst szyfrem Cezara
  7. string szyfrCezara(const string& text, int key) {
  8. string result = "";
  9. for (char c : text) {
  10. if (isalpha(c)) {
  11. char base = isupper(c) ? 'A' : 'a';
  12. result += char((c - base + key) % 26 + base);
  13. } else {
  14. result += c;
  15. }
  16. }
  17. return result;
  18. }
  19.  
  20. // Funkcja szyfrująca wiadomość szyfrem Vigenère'a
  21. string szyfrVigenere(const string& message, const string& key) {
  22. string result = "";
  23. int keyLen = key.length();
  24. int j = 0;
  25.  
  26. for (char c : message) {
  27. if (isalpha(c)) {
  28. char base = isupper(c) ? 'A' : 'a';
  29. int shift = key[j % keyLen] - base;
  30. result += char((c - base + shift) % 26 + base);
  31. j++;
  32. } else {
  33. result += c;
  34. }
  35. }
  36. return result;
  37. }
  38.  
  39. int main() {
  40. string tekst, wiadomosc;
  41. int kluczCezar;
  42.  
  43. // Pobranie danych od użytkownika
  44. cout << "Podaj tekst do zakodowania szyfrem Cezara: ";
  45. getline(cin, tekst);
  46. cout << "Podaj przesunięcie dla szyfru Cezara: ";
  47. cin >> kluczCezar;
  48. cin.ignore(); // Usuwanie znaku nowej linii po 'cin'
  49. cout << "Podaj wiadomość do zaszyfrowania szyfrem Vigenère'a: ";
  50. getline(cin, wiadomosc);
  51.  
  52. // Kodowanie tekstu szyfrem Cezara
  53. string zakodowanyTekst = szyfrCezara(tekst, kluczCezar);
  54. cout << "Zakodowany tekst (klucz Vigenère'a): " << zakodowanyTekst << endl;
  55.  
  56. // Użycie zakodowanego tekstu jako klucza do szyfru Vigenère'a
  57. string zaszyfrowanaWiadomosc = szyfrVigenere(wiadomosc, zakodowanyTekst);
  58. cout << "Zaszyfrowana wiadomość: " << zaszyfrowanaWiadomosc << endl;
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Podaj tekst do zakodowania szyfrem Cezara: Podaj przesunięcie dla szyfru Cezara: Podaj wiadomość do zaszyfrowania szyfrem Vigenère'a: Zakodowany tekst (klucz Vigenère'a): 
Zaszyfrowana wiadomość: