Wojtekd

Xorowy Sekret

May 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. char char_xor( char A, char B );
  8.  
  9. string xor_string( string sekret, string losowy )
  10. {
  11.     string result;
  12.     int dlugosc = sekret.length();
  13.     for(int i = 0; i < dlugosc; ++i)
  14.     {
  15.         char c = char_xor( sekret[i], losowy[i] );
  16.         result += c;
  17.     }
  18.     return result;
  19. }
  20.  
  21. string losowy_ciag( string sekret )
  22. {
  23.     string result;
  24.     int dlugosc = sekret.length();
  25.    
  26.     for(int i = 0; i < dlugosc; ++i)
  27.     {
  28.         int los = rand() % 2;
  29.         if(los)
  30.         {
  31.             result += '1';
  32.         }
  33.         else
  34.         {
  35.             result += '0';
  36.         }
  37.     }
  38.     return result;
  39. }
  40.  
  41. char char_xor( char A, char B )
  42. {
  43.     if(A == '0' and B == '0') return '0';
  44.     if(A == '1' and B == '1') return '0';
  45.     if(A == '1' and B == '0') return '1';
  46.     if(A == '0' and B == '1') return '1';  
  47. }
  48.  
  49. int main()
  50. {
  51.     srand(time(NULL));
  52.     string sekret;
  53.    
  54.     cout << "podaj cyfry sekretu: ";
  55.     cin >> sekret; 
  56.    
  57.     string losowy = losowy_ciag( sekret ); 
  58.     cout << "losowy ciag: " << losowy << endl;
  59.    
  60.     string xor_str = xor_string(sekret, losowy);
  61.    
  62.     cout << "zxorowany string: " << xor_str << endl;
  63.    
  64.     string input;
  65.     cout << "czy chcesz odszyfrować sekret? (y/n)";   
  66.     cin >> input;
  67.     if(input == "y")
  68.     {
  69.         string odzyskany_sekret = xor_string( xor_str, losowy );
  70.         cout << "odzyskany sekret: " << odzyskany_sekret << endl;
  71.     }
  72.        
  73.     system("pause");
  74.     return 0;
  75. }
Add Comment
Please, Sign In to add comment