wercyngetoryks

Projekt Szyfrant v 1.0

Jan 1st, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #define SZYFRANT "Projekt Szyfrant 1.0"
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class KeyNumber {
  8. private:
  9.     int liczba;
  10. public:
  11.     void setKeyNumber(int n) {
  12.     liczba = n;
  13.     }
  14.     int getKeyNumber() {
  15.     return liczba;
  16.     }
  17.     };
  18.  
  19. class Enigma {
  20. private:
  21.     char * Alokacja(char bufor[], int buforSize) {
  22.     char * alokowany = new char[buforSize];
  23.     for (int i = 0; i <= buforSize; i++) {
  24.         alokowany[i] = bufor[i];
  25.     }
  26.     return alokowany;
  27.     }
  28.     void Enigmuj(char text[], KeyNumber liczbaKlucz) {
  29.         int i = 0;
  30.     while (text[i] != NULL) {
  31.         text[i] = text[i] + liczbaKlucz.getKeyNumber();
  32.         i++;
  33.     }
  34.     i = 0;
  35.     while (text[i] != NULL) {
  36.         cout<<text[i];
  37.         i++;
  38.     }
  39.     cout<<""<<endl;
  40.     }
  41.     void DeEnigmuj(char text[], KeyNumber liczbaKlucz){
  42.     int i = 0;
  43.     while (text[i] != NULL) {
  44.         text[i] = text[i] - liczbaKlucz.getKeyNumber();
  45.         i++;
  46.     }
  47.     i = 0;
  48.     while (text[i] != NULL) {
  49.         cout<<text[i];
  50.         i++;
  51.     }
  52.     cout<<""<<endl;
  53.     }
  54. public:
  55.     void Szyfruj() {
  56.     cout<<"Enter the text to encrypt:"<<endl;
  57.     char cbufor[1000];
  58.     cin.get(cbufor, 1000);
  59.     cin.get();
  60.     int buforSize = sizeof(cbufor) / sizeof(cbufor[0]);
  61.     KeyNumber liczbaKlucz;
  62.     Enigmuj(Enigma::Alokacja(cbufor, buforSize), liczbaKlucz);
  63.     }
  64.     void DeSzyfruj() {
  65.     cout<<"Enter the text to decrypt:"<<endl;
  66.     char cbufor[1000];
  67.     cin.get(cbufor, 1000);
  68.     cin.get();
  69.     int buforSize = sizeof(cbufor) / sizeof(cbufor[0]);
  70.     KeyNumber liczbaKlucz;
  71.     DeEnigmuj(Enigma::Alokacja(cbufor, buforSize), liczbaKlucz);
  72.     }
  73. };
  74.  
  75. int main()
  76. {
  77.     cout<<"Welcome to "<<SZYFRANT<<"!"<<endl;
  78.     while(1) {
  79.         cout<<"Please, choose one of the following options:"<<endl;
  80.         cout<<"1. Set a key number;"<<endl;
  81.         cout<<"2. Encrypt a message;"<<endl;
  82.         cout<<"3. Decrypt a message;"<<endl;
  83.         cout<<"Press q to exit "<<SZYFRANT<<"."<<endl;
  84.         char main_menu;
  85.         cin>>main_menu;
  86.         switch(main_menu) {
  87.         case '1':
  88.             KeyNumber liczbaKlucz;
  89.             cout<<"Please enter a key number:"<<endl;
  90.             int buff_liczbaklucz;
  91.             cin>>buff_liczbaklucz;
  92.             liczbaKlucz.setKeyNumber(buff_liczbaklucz);
  93.             cout<<"Key number has been set to "<<liczbaKlucz.getKeyNumber()<<"."<<endl;
  94.         break;
  95.         case '2':
  96.             cin.get();
  97.             Enigma enigma;
  98.             enigma.Szyfruj();
  99.         break;
  100.         case '3':
  101.             cin.get();
  102.             Enigma deenigma;
  103.             deenigma.DeSzyfruj();
  104.         break;
  105.         case 'q':
  106.             cout<<"Thanks for using "<<SZYFRANT<<". Press any key."<<endl;
  107.             cin.get();
  108.             return 0;
  109.         break;
  110.         }
  111.  
  112.     }
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment