Password Cracker (brute force) THIS IS JUST FOR LEARNING PURPOSES. I WON'T BE USING THIS TO HACK ANYONE

Here is a small algorithm I made to crack a passwork. This is just the background of a more complex code I'm going to make.

To be honest, this code is TERRIBLY written, so I need help on making it more efficient and flexible. It is limited to 5-characters password and not easy to expand.

#include&lt;algorithm&gt;
#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;vector&gt;

std::string checkCharacters = {
    '%',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    '1','2','3','4','5','6','7','8','9','0'
};

std::string search(std::string password) {
    for (int a = 0; a &lt; checkCharacters.size(); a++)
        for (int b = 0; b &lt; checkCharacters.size(); b++)
            for (int c = 0; c &lt; checkCharacters.size(); c++)
                for (int d = 0; d &lt; checkCharacters.size(); d++)
                    for (int e = 0; e &lt; checkCharacters.size(); e++)
                    {
                        std::string checkString = "";
                        checkString += checkCharacters[a];
                        checkString += checkCharacters[b];
                        checkString += checkCharacters[c];
                        checkString += checkCharacters[d];
                        checkString += checkCharacters[e];

                        checkString.erase(std::remove(checkString.begin(), checkString.end(), '%'), checkString.end());

                        if (checkString == password)
                            return checkString;
                    }

    return "TARGET_PASSWORD_NOT_FOUND";
}

int main() {
    while (1) {
        std::string password;
        std::cout &lt;&lt; "Enter password : ";
        std::cin &gt;&gt; password;
        std::cout &lt;&lt; "Your password is : " &lt;&lt; password &lt;&lt; std::endl;
        std::string attempt = search(password);
        std::cout &lt;&lt; attempt &lt;&lt; std::endl &lt;&lt; std::endl;
    }
}
This is a critique, and a rewrite of the code you showed, that is, the code for checking if a given password only contains characters from a given character set; for an actual brute force password cracker, see e.g. this  question.



I think you should look at the problem a bit differently.

Why don't you go through the characters in the password, and check if every character is in the set of allowed characters? 
If you get that every character of the password is in the set of allowed characters, then the password is valid, otherwise it isn't. 

I'd implement this in the following way.

#include&lt;algorithm&gt;
#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;vector&gt;
#include&lt;set&gt;
#include&lt;numeric&gt;

const static std::set&lt;char&gt; charSet = {
    '%',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    '1','2','3','4','5','6','7','8','9','0'
};

bool checkPassword(const std::string password) {
  return std::accumulate( password.begin(), password.end(), true,
    [](bool acc, char c) { return acc &amp;&amp; charSet.find(c) != charSet.end(); } );
}

int main() {
  for (;;) {
    std::string password;
    std::cout &lt;&lt; "Enter password : ";
    std::cin &gt;&gt; password;
    std::cout &lt;&lt; "Your password is : " &lt;&lt; password &lt;&lt; std::endl;
    if(checkPassword(password)) {
      std::cout &lt;&lt; "VALID" &lt;&lt; std::endl &lt;&lt; std::endl;
    } else {
      std::cout &lt;&lt; "INVALID" &lt;&lt; std::endl &lt;&lt; std::endl;
    }
  }
  return 0;
}


This will work with passwords of any length. 
Set lookups have logarithmic complexity, so checking a password of length \$n\$ given a character set of size \$m\$ should be \$\mathcal O(n\cdot \log(m))\$.