Advertisement
Wojtekd

Szyfr Vigenere'a

Apr 4th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int indeks_w_alfabecie(char c)
  10. {
  11.     char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12.     for(int i = 0; i < strlen(alfabet); i++)
  13.     {
  14.         if(alfabet[i] == c)
  15.         {
  16.             return i;
  17.         }
  18.     }
  19. }
  20.  
  21. char* obetnij_klucz(char* klucz)
  22. {
  23.     string result;
  24.    
  25.     int dlugosc_klucza = strlen(klucz);
  26.    
  27.     for(int i = 0; i < dlugosc_klucza; i++)
  28.     {
  29.         char letter = klucz[i];
  30.                
  31.         size_t n = std::count(result.begin(), result.end(), letter);
  32.        
  33.         if(n == 0)
  34.         {
  35.             result += klucz[i];
  36.         }              
  37.     }
  38.     return const_cast<char*>(result.c_str());
  39. }
  40.  
  41. char* szyfr_vigenere( char* tekst, char* klucz )
  42. {
  43.     klucz = obetnij_klucz(klucz);      
  44.  
  45.     char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  46.    
  47.     char tab[26][26];
  48.     int spot = 0;
  49.    
  50.     for(int i = 0; i < 26; i++)
  51.     {
  52.         for(int j = 0; j < 26; j++)
  53.         {
  54.             spot = (j + i) % 26;
  55.             if(spot < 0)
  56.             {
  57.                 spot += 26;
  58.             }                  
  59.             tab[i][j] = alfabet[spot];
  60.         }
  61.     }
  62.    
  63.     /* wydrukuj tablice
  64.     for(int i = 0; i < 26; i++)
  65.     {
  66.         cout << endl;
  67.         for(int j = 0; j < 26; j++)
  68.         {
  69.             cout << tab[i][j];
  70.         }
  71.     }
  72.     */
  73.    
  74.     int dlugosc_klucza = strlen(klucz);
  75.     int dlugosc_tekstu = strlen(tekst);
  76.    
  77.     char* result = new char[dlugosc_tekstu + 1];   
  78.    
  79.     // przejdz przez caly tekst
  80.     for(int i = 0; i < dlugosc_tekstu; i++)
  81.     {  
  82.         char literka_tekstu = tekst[i];    
  83.         int n = indeks_w_alfabecie(tekst[i]);
  84.  
  85.         int spot = i % dlugosc_klucza;     
  86.         int m = indeks_w_alfabecie(klucz[spot]);
  87.        
  88.         result[i] = tab[m][n];                                 
  89.     }
  90.    
  91.     return result;
  92. }
  93.  
  94.  
  95. int main()
  96. {
  97.     char klucz[50];
  98.     cout << "podaj klucz: " << endl;
  99.     cin >> klucz;
  100.    
  101.     cout << "wybrano klucz: " << klucz << endl;
  102.    
  103.     char tekst[1000];
  104.     cout << "podaj tekst " << endl;
  105.     cin >> tekst;
  106.    
  107.     char* szyfrogram = szyfr_vigenere(tekst,klucz);
  108.     cout << szyfrogram;
  109.    
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement