I'm interested in creating a bruteforce program. What is the most computer resource efficient method to count in base 62? (1,2,3...a,b,c...A,B,C)

Recommended Answers

All 17 Replies

probably something like below: count using normal integers but convert to base62 for display.

#include <cstring>
#include <iostream>
using namespace std;

int main( )
{
	long n = 1234576;
	char buf[255];
	 _itoa( n, buf, 62 );
	cout << buf << "\n";
    return 0;
}

Warning: The above code is not ansi standard. Here is a link you might be able to use if you need something ansi standard.

[edit]I have done a little testing with the algorithms in the link I posted and none of them produce the same result as _itoa() function. The first algorithm for my_atoi() might be a fairly simple fix -- it just leaves off the last digit.[/edit]

Well here is a c++ version that should be portable and returns the same as itoa().

string my_itoa(int value, int base)
{
	string s;
	for(int i = base; value && i ; --i, value /= base)
	{
		s = "0123456789abcdefghijklmnopqrstuvwxyz"[value % base] + s;
	}
	return s;

}

The problem here is that it wouldn't be able to crack a string such as 000 or 0zzz

commented: 3 years late. -1

Here's my code, tested for errors:

#include <iostream>
#include <string>
using namespace std;
char chars[]={'0','1','2','3','4','5','6','7','8','9','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'};
string t;
void checkPassword(string password);
void recurse(int width, int position, string baseString);
int  main() {
  cout << "Enter a string: " << endl;
  cin >> t;
  int maxChars = 13;
  for(int i=0;i<maxChars+1;i++) {
    cout << "checking passwords width [" << i << "]..." << endl;
    recurse(i,0,"");
  }
  return 0;
}
void recurse(int width, int position, string baseString) {
  for(int i=0;i<35;i++) {
    if (position < width-1) {
      recurse(width, position + 1, baseString+chars[i]);
    }
    checkPassword(baseString+chars[i]);
  }
}
void checkPassword(string password) {
  if (password==t) {
    cout << "match [" << password << "]" << endl;
    exit(1);
  }
}

it cracks the string h3ar7 in 16 seconds on my computer.

There should be 36 in for loop :)
regards

>>it cracks the string h3ar7 in 16 seconds on my computer.

Thats pretty slow. There is no reason to use recursion here.

How do i find out what the password is after it cracks it?

Great algo combustion! I have one question can anyone tell me a way with combustions code to make it so it will start with given characters? I've tried a few things but they always seem to reset back to the beginning.

s = "0123456789abcdefghijklmnopqrstuvwxyz"[value % base] + s;

offtopic:I'm having trouble understanding this line, so you're initializing s with "01..yz"
but what does the

[value % base] + s

part actually do?

Would it help if the code looked like this?

string digits = "0123456789abcdefghijklmnopqrstuvwxyz";
int index = value % base;

s = digits[index] + s;

Would it help if the code looked like this?

string digits = "0123456789abcdefghijklmnopqrstuvwxyz";
int index = value % base;

s = digits[index] + s;

yes, so how it's possible to define a string followed by an expression, how it's this possible ?

How would it not be possible? This isn't any different from adding two integer literals together (ie. 2 + 3 ). The compiler manages storage for the temporary objects, and you can use those objects in a valid expression. Just like + is a valid operator for integers, [] is a valid operator for a pointer to char (the value type of a string literal).

And '+' is defined as the concatenation-operator for the std::string class, so

string s = "foo";
    char c = 'a';
    s = c + s;
    cout << s << endl;

would display "afoo"

Great algo combustion! I have one question can anyone tell me a way with combustions code to make it so it will start with given characters? I've tried a few things but they always seem to reset back to the beginning.

Any ideas? Or is it not possible?

Anything is possible, but I can't read minds yet. Please post the current state of your code along with a description of what you're trying to do, what results you're seeing instead, any error messages, and finally whatever specific questions you have.

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
char chars[]={'0','1','2','3','4','5','6','7','8','9','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'};
string t;
int startchars[3];
bool start = true;
void checkPassword(string password);
void recurse(int width, int position, string baseString);
int  main() {
  cout << "Enter a string: " << endl;
  cin >> t;
  startchars[0] = 5;
  startchars[1] = 10;
  startchars[2] = 15;
  int maxChars = 13;
  for(int i=3;i<maxChars+1;i++) {
    cout << "checking passwords width [" << i << "]..." << endl;
    recurse(i,0,"");
  }
  return 0;
}
void recurse(int width, int position, string baseString) {
  printf("Width: %d Position: %d String: %s\n",width,position,baseString.c_str());
  for(int i=0;i<36;i++) {
    if (position < width-1) {
      printf("Recurse2 i: %d width: %d position: %d string: %s\n",i,width,position + 1,baseString.c_str());
      if(start)
      {
          recurse(width, position + 1, baseString+chars[startchars[i]]);
          if(i >= sizeof(startchars))
              start = false;
      }
      else
      {
          recurse(width, position + 1, baseString+chars[i]);
      }
    }
    printf("Width: %d Position: %d String: %s\n",width,position,baseString.c_str());
    checkPassword(baseString+chars[i]);
    Sleep(100);
  }
}
void checkPassword(string password) {
   cout << "trying " << password << endl;
  if (password==t) {
    cout << "match [" << password << "]" << endl;
    system("Pause");
    exit(1);
  }
}

This is combustions code but modifyied to try to specify where I want it to start. Problem is the first char specified for start is correct but the second is the same as first and when it goes to flip the chars before the last char in the string they reset back to the beginning instead of going to the next char after the specified char. The idea is to have it so if program exits, it can pick up where it left off instead of starting back at the beginning.

It will help if you understand what combustion's code is doing in the first place.

A previous commenter pointed out that recursion isn't strictly necessary for this problem, and probably slows down the algorithm a great deal. In addition, his recurse() function calls checkPassword() too often: since he's looping over desired password-width outside of the function, he should only be checking the password if the postion == width-1 (he's filled the password to the desired width), the code calls the function for all widths as the recursion unwinds. Finally, another commenter also pointed out an actual error in the code: as written, an input password with a 'z' in it will never be matched.

That said, it's very hard (if not impossible) to restart a recursive process in the middle, if the "program exits" for some reason other than completing its task normally. Then, to be able to "pick up where it left off", the program needs to save its state to some external file which it can read back in when it is started. Otherwise, how would it know that it didn't finish the previous time?

So, I'd recommend (1) understanding what combustion's code is actually doing (printing out the password being tested inside checkPassword() should be sufficient, understanding -why- it works that way is bonus), (2) unrolling the recursion into a simple set of nested loops which accomplishes the same thing, (3) being able to save the current state of testing out to a file and successfully read it back in, (4) save the state periodically (but not on every password attempt, or you'll spend far more time updating the state than you do actually testing the current password -- maybe maintain a counter and save the state after every N checks -- then if you kill the program, it will have to back up to the last save point, but not all the way to the beginning), (5) undersstand how to pick up from a saved state and continue on correctly, and (6) don't forget to take care of the saved state when the program successfully completes so it can start again from the beginning.

While this is probably a silly amount of effort for this sample program, you'll learn a surprising amount of real-world programming skills. Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.