PDA

View Full Version : BruteForcing with Qt



silur
23rd August 2012, 14:21
Hi i'm creating a BruteForce function in Qt and altough this code works in my Python version the C++ seems to be a little buggy:


#include <QtCore/QCoreApplication>
#include <QDebug>
void BruteForce(int min, int max, QString charset);
void recurse(int width, int pos, QString s, QString charset);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

BruteForce(1,3,"abcdef");

return a.exec();
}

void BruteForce(int min, int max, QString charset)
{
for (int i=min; i<=max; i++)
{
qDebug() << "Checking with" << i << "char " << endl;
recurse(i,0,"",charset);
}
}

void recurse(int width, int pos, QString s, QString charset)
{
QChar c;
for (int i=0;i<=charset.length();i++)
{
if (pos < width - 1)
{
recurse(width, pos+1 , s + c , charset);

}
c=charset[i];

qDebug() << s+c;
}
}


and my output:
....
"aed"
"aee"
"aef"
"ae
"af"
"afa"
"afb"
"afc"
"afd"
"afe"
"aff"
"af
"a
"b"
"b
"b
"b
"b
"b
"b
"b
"ba"
"baa"
"bab"
"bac"
"bad"
"bae"
"baf"
"ba
"bb"
......
as you can see it goes trough aaa to aaf and check again ? aa ?
after a 3 character check it prints the actual item in the cahrset in [charset lenght] times O.o

any guesses?

d_stranz
24th August 2012, 00:56
The first time through the for loop in the recurse() function, the variable "c" has been declared but is unassigned, probably contains garbage. Who knows what appending it to "s" does in that case.

Aside from the fact that you're using QString and QChar, what does this have to do with Qt? It's a basic C++ programming error problem.

By the way, "BruteForce" and "recurse" are very clever names for functions. I am sure when you look at this code a year from now, you'll be able to tell from the names exactly what those functions are supposed to do, assuming you ever get them to do what you intend for them to do.