A password guess
As an example of a brute-force algorithm, let’s create a program to generate all possible passwords and trying to guess your secret one, which consists of small letters and digits only. The program starts with passwords of a length equal to 2 and proceeds until 8.
The first part of the code is presented here:
using System.Diagnostics; using System.Text; const string secretPassword = "csharp"; int charsCount = 0; char[] chars = new char[36]; for (char c = 'a'; c <= 'z'; c++) { chars[charsCount++] = c; } for (char c = '0'; c <= '9'; c++) { chars[charsCount++] = c; }
First, you specify a secret password, which you will try to guess in the remaining part of the code. Then, you create an array with available characters, namely small letters and digits. At the end of this code snippet, the charsCount
variable stores the number of available characters.