Advertisement
Wojtekd

Łamanie szyfru (Statystyczny)

Mar 14th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. // statystyczny
  2.  
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. char* szyfr_cezara(int klucz, char* oryginal)
  12. {
  13.     char* alfabet_malych = "abcdefghijklmnopqrstuvwxyz";
  14.     char* alfabet_duzych = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15.     char* alfabet_cyfr = "0123456789";
  16.    
  17.     int n = strlen(oryginal);
  18.     char* result = new char[n+1];
  19.     strcpy(result, oryginal);
  20.    
  21.     for(int i = 0; i < n; i++)
  22.     {
  23.         char* alfabet;
  24.        
  25.         //cout << static_cast<int>(oryginal[i]) << endl;
  26.        
  27.         if(oryginal[i] >= 65 && oryginal[i] <= 90)
  28.         {
  29.             alfabet = alfabet_duzych;
  30.         }
  31.         else if(oryginal[i] >= 97 && oryginal[i] <= 122)
  32.         {
  33.             alfabet = alfabet_malych;
  34.         }
  35.         else if (oryginal[i] >= 48 && oryginal[i] <= 57)
  36.         {
  37.             alfabet = alfabet_cyfr;
  38.         }
  39.         else
  40.         {
  41.             //cout << "znaki musza byc alfanumeryczne";
  42.             //exit(1);
  43.             result[i] = oryginal[i];
  44.             continue;
  45.         }      
  46.        
  47.         int length = strlen(alfabet);
  48.         int indeks_szukanej;
  49.        
  50.         for(int j = 0; j < length; j++)
  51.         {
  52.             if(alfabet[j] == oryginal[i])  
  53.             {
  54.                 indeks_szukanej = j;
  55.             }
  56.         }      
  57.        
  58.         int spot = (indeks_szukanej + klucz) % length;
  59.        
  60.                    
  61.         if(spot < 0)
  62.         {
  63.             spot += length;
  64.         }              
  65.            
  66.         result[i] = alfabet[spot];
  67.     }      
  68.     return result;
  69. }
  70.  
  71. int roznica_do_e(char litera)
  72. {
  73.     if(litera == 'E') return 0;
  74.    
  75.     char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  76.    
  77.     int key = 0;
  78.    
  79.     if(litera > 'E')
  80.     {
  81.         key = 'E' - litera;
  82.         key += 26;
  83.     }
  84.     else
  85.     {
  86.         key = 'E' - litera;
  87.     }
  88.    
  89.     return key;
  90. }
  91.  
  92. int count_occurrences(string s, char literka)
  93. {
  94.     int count = 0;
  95.    
  96.     for (int i = 0; i < s.size(); i++)
  97.     if (s[i] == literka) count++;
  98.    
  99.     return count;
  100. }
  101.  
  102. int main()
  103. {
  104.     char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  105.     char litera = 'E';
  106.     int tab[26];
  107.    
  108.     for(int i = 0; i < 26; i++)
  109.     {
  110.         tab[i] = 0;
  111.     }  
  112.  
  113.     fstream plik;      
  114.     string line;
  115.    
  116.     plik.open( "szyfrogram.txt", ios::in );
  117.     if( plik.good() == true )
  118.     {              
  119.         while(getline(plik, line))     
  120.         {
  121.             for(int i = 0; i < 26; i++)
  122.             {
  123.                 char aktualna_literka = alfabet[i];
  124.                 int ilosc = count_occurrences(line,aktualna_literka);
  125.                 tab[i] += ilosc;
  126.             }          
  127.                        
  128.             //cout << line;            
  129.         }          
  130.     }      
  131.     plik.close();          
  132.    
  133.     // wypisz po zliczeniu
  134.     for(int i = 0; i < 26; i++)
  135.     {
  136.         cout << alfabet[i] << " ";
  137.         cout << tab[i] << endl;
  138.     }
  139.    
  140.     // znajdz maximum
  141.     int indeks_max,max = 0;
  142.     for(int i = 0; i < 26; i++)
  143.     {
  144.         if(tab[i] > max)
  145.         {
  146.             max = tab[i];
  147.             indeks_max = i;
  148.         }
  149.     }
  150.     cout << "najwiecej jest literek: " << alfabet[indeks_max] << endl;
  151.    
  152.     char najczestsza = alfabet[indeks_max];
  153.     int key = roznica_do_e(najczestsza);   
  154.    
  155.     cout << "klucz to: " << key << endl;
  156.    
  157.     cout << endl;
  158.     cout << endl;
  159.        
  160.     std::fstream plik2;    
  161.     //char oryginal[1000];
  162.     string line2;
  163.    
  164.     plik2.open( "szyfrogram.txt", ios::in );
  165.     if( plik2.good() == true )
  166.     {  
  167.         int klucz = key;
  168.        
  169.         while(getline(plik2, line2))       
  170.         {
  171.             char* line_cstr = const_cast<char*>(line2.c_str());
  172.             char* szyfrogram = szyfr_cezara( klucz , line_cstr );
  173.            
  174.             cout << szyfrogram;        
  175.         }  
  176.        
  177.     }
  178.     plik2.close();
  179.        
  180.    
  181.    
  182.     system("pause");
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement