Thread: Brute Force String Generator help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Brute Force String Generator help

    Yesterday, I posted a thread asking for help on a random string generator. Well, I have gotten that to work properly now, but now I need help on something else. Also, you may notice my directions have changed. I misunderstood the instructions on my homework, and they have since been fixed.

    The instructions on my project are to write a program in C++ that will randomly generate a string with only capital letters to serve as a password. This password will then be subjected to a brute force attack (AAAAAAA, AAAAAAB, AAAAAAC... AAAAAAZ, AAAAABA, ... ZZZZZZZ), and this is where I need help. I don't know how I would write something in C++ that would logically count upwards in letters, and not numbers (since this is essentially counting in base-26).

    tl;dr, how would I write something that would count upward with only letters instead of numbers, using the same sort of logic?

    Here is the code. The function for the brute force attack is at the very bottom, and as you can see, I did copy the variable declarations from my random generator since it should be somewhat the same in composition.

    Code:
    // Trey Brumley
    // Topics in Computer Security
    // Dr. Nelson J. Passos
    // Class Project:  Password Cracking
    // =================================
    
    
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    
    int x;
    
    
    string randomStrGen(int length);
    string bruteForceGen(int length);
    
    
    int main()
    {
        char d = 'Y';
    
    
        while (d != 'N')
        {
            restart:
        cout << "Enter a value to serve as string length:  ";
        cin >> x;
        cout << endl << endl;
    
    
        string a = randomStrGen(x);
        string b;
    
    
        int c = 0;                                            // running the two separately.
    
    
        cout << "The compare string is "<< a << "." << endl << endl;    // Makes sure a string is being properly stored in a.
    
    
        while (b != a)
        {
            c++;
            b = randomStrGen(x);
        }
    
    
        cout << "It took " << c << " attempts to crack this password." << endl << endl;
        cout << "Would you like to crack another password? (Y or N)  ";
        cin >> d;
        cout << endl;
    
    
        if (d == 'N')
        {
            system("pause");
            return 0;
        }
        else if (d == 'Y')
            goto restart;
        else
        {
            cout << "Invalid character.  Please enter Y to run again or N to quit:  ";
            cin >> d;
            cout << endl << endl;
        }
        }
        system("pause");
        return 0;
    }
    
    
    string randomStrGen(int length)
    {
        static string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string result;
        result.resize(length);
    
    
        for (int i = 0; i < length; i++)
            result[i] = charset[rand() % charset.length()];
    
    
        return result;
    }
    
    
    string bruteForceGen(int length)
    {
        static string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string result;
        result.resize(length);
    
    
    
    
    
    
        return result;
    }
    Last edited by Trey Brumley; 06-26-2013 at 02:34 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you have some character variable, C, then adding one to it will move it to the next letter in the alphabet. Well, okay, this may or may not be guaranteed, but on a PC platform, it will most likely work just fine. Eg:

    char C = 'A';
    C++; // C is now 'B'
    C++; // C is now 'C'
    //etc
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Think about how you do numbers in school - to add one, look at the digit in the 1's column and see if you can up it by one. Overflow? Reset it to zero and increment the digit in the 10's column. Overflow? Reset... ...overflow on the last digit? Add a 1 on the front - although in your case that means you're done. The process would be analogous for your brute-force attack, only you go from A-Z and back to A instead of 0-9 and back to 0.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Brute Force
    By 123sample in forum C Programming
    Replies: 4
    Last Post: 09-12-2010, 12:37 AM
  2. Brute Force
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-16-2007, 01:41 AM
  3. Help : Brute Force String KeyGen
    By Tangy in forum C++ Programming
    Replies: 11
    Last Post: 03-11-2002, 09:01 PM
  4. Brute Force
    By Wiz_Nil in forum C++ Programming
    Replies: 13
    Last Post: 02-15-2002, 01:28 PM
  5. Brute-Force
    By red11514 in forum C Programming
    Replies: 1
    Last Post: 11-13-2001, 12:53 AM