Wojtekd

Łamanie szyfru (brute-force)

Mar 14th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. // brute force
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9.  
  10. char* szyfr_cezara(int klucz, char* oryginal)
  11. {
  12.     char* alfabet_malych = "abcdefghijklmnopqrstuvwxyz";
  13.     char* alfabet_duzych = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  14.     char* alfabet_cyfr = "0123456789";
  15.    
  16.     int n = strlen(oryginal);
  17.     char* result = new char[n+1];
  18.     strcpy(result, oryginal);
  19.    
  20.     for(int i = 0; i < n; i++)
  21.     {
  22.         char* alfabet;
  23.        
  24.         //cout << static_cast<int>(oryginal[i]) << endl;
  25.        
  26.         if(oryginal[i] >= 65 && oryginal[i] <= 90)
  27.         {
  28.             alfabet = alfabet_duzych;
  29.         }
  30.         else if(oryginal[i] >= 97 && oryginal[i] <= 122)
  31.         {
  32.             alfabet = alfabet_malych;
  33.         }
  34.         else if (oryginal[i] >= 48 && oryginal[i] <= 57)
  35.         {
  36.             alfabet = alfabet_cyfr;
  37.         }
  38.         else
  39.         {
  40.             //cout << "znaki musza byc alfanumeryczne";
  41.             //exit(1);
  42.             result[i] = oryginal[i];
  43.             continue;
  44.         }      
  45.        
  46.         int length = strlen(alfabet);
  47.         int indeks_szukanej;
  48.        
  49.         for(int j = 0; j < length; j++)
  50.         {
  51.             if(alfabet[j] == oryginal[i])  
  52.             {
  53.                 indeks_szukanej = j;
  54.             }
  55.         }      
  56.        
  57.         int spot = (indeks_szukanej + klucz) % length;
  58.        
  59.                    
  60.         if(spot < 0)
  61.         {
  62.             spot += length;
  63.         }              
  64.            
  65.         result[i] = alfabet[spot];
  66.     }      
  67.     return result;
  68. }
  69.  
  70. int main()
  71. {
  72.     /*
  73.     int klucz = 0;
  74.    
  75.     cout << "podaj klucz: " << endl;
  76.     cin >> klucz;  
  77.    
  78.     char oryginal[1000];
  79.     cout << "podaj tekst do zaszyfrowania: " << endl;
  80.     cin >> oryginal;
  81.    
  82.     char* szyfrogram = szyfr_cezara(klucz, oryginal);
  83.     cout << "szyfrogram: " << szyfrogram << endl;
  84.     */
  85.    
  86.     std::fstream plik;     
  87.     //char oryginal[1000];
  88.     string oryginal;
  89.    
  90.     plik.open( "szyfrogram.txt", ios::in );
  91.     if( plik.good() == true )
  92.     {
  93.         for(int klucz = 0; klucz < 27; klucz++)
  94.         {
  95.             getline( plik, oryginal );
  96.             char* oryginal_cstr = const_cast<char*>(oryginal.c_str());
  97.            
  98.             cout << oryginal_cstr << endl;
  99.            
  100.             char* szyfrogram = szyfr_cezara( klucz, oryginal_cstr );               
  101.             cout << "klucz: " << klucz << "      szyfrogram: " << szyfrogram << endl;
  102.         }
  103.     }
  104.     plik.close();
  105.    
  106.    
  107.     std::fstream plik2;    
  108.     //char oryginal[1000];
  109.     string line;
  110.    
  111.     plik2.open( "szyfrogram.txt", ios::in );
  112.     if( plik2.good() == true )
  113.     {  
  114.         cout << "podaj prawidlowy klucz aby wyswietlic caly plik: " << endl;
  115.         int klucz;
  116.         cin >> klucz;
  117.        
  118.         while(getline(plik2, line))    
  119.         {
  120.             char* line_cstr = const_cast<char*>(line.c_str());
  121.             char* szyfrogram = szyfr_cezara( klucz , line_cstr );
  122.            
  123.             cout << szyfrogram;        
  124.         }  
  125.        
  126.     }
  127.     plik2.close();
  128.  
  129.     return 0;
  130. }
Add Comment
Please, Sign In to add comment