Advertisement
Kordan

test BruteForce

Nov 13th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. // Artem Korshenko
  2. // cppstudio.com/post/8059
  3. // Linux C++11
  4. #include <iostream>
  5. #include <cmath> // pow() - function
  6.  
  7. std::string input ( std::string str ) {
  8.   std::cout << str;
  9.   std::getline ( std::cin, str );
  10.   return str;
  11.   } // Python style
  12.  
  13.  
  14. int main() {
  15.   std::string str_password;
  16.   do {
  17.     str_password = input ( "Enter password: " );
  18.  
  19.     if ( str_password.size() < 1 ) std::cout << "Please, enter password" << std::endl;
  20.     } while ( str_password.size() < 1 );
  21.  
  22.   std::string str_chars;
  23.   do {
  24.     std::cout << "Type '+' to include... or press 'Enter' to skip" << std::endl;
  25.     if ( input ( "Small chars: " ) == "+" ) str_chars += "qwertyuiopasdfghjklzxcvbnm";
  26.     if ( input ( "Large chars: " ) == "+" ) str_chars += "QWERTYUIOPASDFGHJKLZXCVBNM";
  27.     if ( input ( "Digits: "      ) == "+" ) str_chars += "0123456789";
  28.     if ( input ( "Symbols: "     ) == "+" ) str_chars += " `~!@#$%^&*()_+=-][}{\';|:/.,<>?\\\"";
  29.  
  30.     if ( str_chars.size() < 1 ) std::cout << "Please, select characters" << std::endl;
  31.     } while ( str_chars.size() < 1 );
  32.  
  33.   bool cycle = true;
  34.   bool founded = false;
  35.   int size_password = str_password.size();
  36.   int size_chars    = str_chars.size();
  37.   int indexer[ size_password ];
  38.   std::string str_bruteforce;
  39.   str_bruteforce.resize ( size_password );
  40.   for ( int i = 0; i < size_password; ++i ) indexer[i] = 0;
  41.  
  42.   std::cout << "Selected characters: " << str_chars << std::endl;
  43.   std::cout << "Lenght of password: " << size_password << std::endl;
  44.   std::cout << "Length of dictionary: " <<  size_chars << std::endl;
  45.   std::cout << "Number of variants password: " << ulong ( std::pow ( size_chars, size_password ) ) << std::endl; // variants = library ^ password
  46.   std::cout << "Please wait..." << std::endl;
  47.  
  48.   while ( true ) {
  49.     for ( int i = size_password - 1; i >= 0; --i ) {
  50.       if ( i != 0 ) {
  51.         if ( indexer[i] == size_chars ) {
  52.           indexer[i] = 0;
  53.           indexer[i - 1]++;
  54.           }
  55.         }
  56.       }
  57.  
  58.     for ( int i = 0; i < size_password; ++i ) str_bruteforce[i] = str_chars[indexer[i]];
  59.  
  60.     cycle = true;
  61.     if ( str_bruteforce == str_password ) {
  62.       std::cout << "You password: " << str_bruteforce << std::endl;
  63.       cycle = false;
  64.       founded = true;
  65.       } if ( !cycle ) break;
  66.  
  67.     cycle = false;
  68.     for ( int i = 0; i < size_password; ++i ) {
  69.       if ( indexer[i] != size_chars - 1 ) {
  70.         cycle = true;
  71.         break;
  72.         }
  73.       } if ( !cycle ) break;
  74.  
  75.     indexer[ size_password - 1 ]++;
  76.     }
  77.   if ( !founded ) std::cout << "Error selected characters" << std::endl;
  78.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement