String Generator

This is a discussion on String Generator within the C++ Programming forums, part of the General Programming Boards category; Hello, for awhile now I've been trying to make a "string generator" that is, something similar to something that might ...

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    String Generator

    Hello, for awhile now I've been trying to make a "string generator" that is, something similar to something that might be used in a hash bruteforcer or something. Well, of course I thought it would be logical to take up the string permutations challenge, but how then would I allow it to make all combinations of a selected character set with a selected size.

    Well, being the noob that I am I gvae up and started translating a PHP one back into C++ for whatever reason. But, now I just seem to be encountering problems, mainly with the use of the position [] array I believe. I would try to follow the logic of the program through on pen and paper, but I really couldn't get my head around the logic for this program.

    How about I just give you the code and mark what I'm being stupid on:

    test.cpp
    Code:
    #include "test.h"
    BruteForce::BruteForce() 
    {
    	//Initialize variables
    	wordLength =   0;
    	charNumber =   0;
    	maxNumber =    0;
    	depth =        0;
    	characters =   new char[27];
    	
    	//Fill up the array
    	for(char i = 'A'; i <= 'Z'; i++) 
    		characters[i - 'A'] = i;
    	
    }
    BruteForce::BruteForce(int len, char* set) 
    {
    	//Initialize variables, sorry for C/C++ raping	
    	wordLength = len;
    	depth = 0;
    	memset(word, 0, len);
    	characters = new char[len + 1];
    	characters = set;
    	charNumber = strlen(characters);
    	maxNumber = pow(charNumber, len);
    
    	//I don't even understand position []
    	//So I'm giving it a lot of space to work with
    	//Set it all to zero. Bad n3wb technque
    	for(int i = 0; i <= 1000; i++) 
    		position[i] = 0;
    }
    
    char * BruteForce::getNext() 
    {
    	//Should be something here to tell it to stop
    	//By checking position[0] against wordLength?
    
    
    	position[0]++;
    	depth = 0;
    	getWord();
    	return word;
    }
    BOOL BruteForce::getWord() {
    	int result = true;
    	if(depth < wordLength) 
    	{
    		result = false;
    		depth++;
    
    
    		//Basically I'm not getting my head around what
    		//position[depth] should look like. 
    		sprintf(word, "%s%c", word, characters[position[depth]]); 
    		if(getWord()) 
    		{
    			position[depth]++;
    			if(position[depth] == charNumber) 
    			{
    				position[depth] = 0;
    				result = true;
    			}
    		}
    		depth--;
    	}
    	return result;
    }
    
    int main() {
    	BruteForce* bruteForce = new BruteForce(3, "aaa");
    	//Yes, should stop somewhere, not jsut while(1)
    	while(1) { printf("%s\n", bruteForce->getNext()); }
    
    	return 0;
    }



    test.h
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <math.h>
    #include <malloc.h>
    class BruteForce { 
    
    public: 
    	char* getNext();
    	BOOL  getWord();
    	BruteForce();
    	BruteForce(int len, char* set);
        
    private: 
    	int wordLength;
    	char word[5]; //I don't really know
    	int position[1000]; //I don't really know
    	int charNumber;
    	double maxNumber;
    	int depth;
    	char* characters; 
    };
    But anyways, the output seems "almost good" but not really, there's a clear attempt to make something though
    aaa
    aaaaaa
    aaaaaaaaa
    aaaaaaaabaa
    aaaaaaaacaaaaM
    aaaaaaaadaaaaMai
    *Crash*
    There's some clear attempt to do an abcd in column, what, column 9 of that for some reason. I'm just having a lot of trouble making a flow chart for my project, any help is appreciated. Sorry if maybe this looks like homework or if I'm dumping a big load on you. For some reason it's been my goal for awhile to make some sort of hash brute-forcer not for any malicious reasons at all just because it feels like a fun experience.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well it seems overly complicated to me
    Code:
    #include <stdio.h>
    void next ( char *test, int len ) {
      while ( --len >= 0 ) {
        if ( ++test[len] <= 'z' ) break;
        else test[len] = 'a';
      }
    }
    int main ( void ) {
      char test[] = "aaaa";
      while ( test[0] <= 'z' ) {
        printf( "%s\n", test );
        next( test, 4 );
      }
      return 0;
    }
    Yes, this does loop forever - fixing it is an easy exercise for the reader
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21