Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Thursday, September 9, 2010

Designing Simple Brute-Force Algorithm

I decided to write a brute-force algorithm and as a start, it will be a simple one. This algorithm lists all possible character combinations of all lengths starting from 4 through 22. You can also extend its functionality to include space and non-alphanumeric characters.
I also decided to give this code the ability to save its stop point and  to restore it on next start. In other words, if your program stops at the combination 002, the next time it starts it will resume at 003 and so on. This property is implemented in most of password recovery programs which use brute-force attacks.

Here is the source code

This console application runs for 20 seconds generating the in-order combinations. Just before exit, it saves its stop point.

N.B. You can freely change the code to meet your needs.

N.B. The code is only for demonstration.

Thursday, August 26, 2010

Simple Random Number Generator

Today i decided to write my own rand function to generate a random number without any  use of the time APIs. Unlike the famous "rand" function, the "myrand" function doesn't require any srand function calls.

My function depends on an x-86 instruction called RDTSC. This instruction reads the processor time stamp i.e. the number of clock cycles since the last reset. The time stamp is read into the  EAX:EDX registers.

The "myrand" function takes only one parameter called X e.g. if X is 5, the generated random number will range from 0 to 4.

The code is so simple that it can be implemented in few lines.


int myrand(int X)
{
          int a;
         __asm
       {
                pushad
                rdtsc
               mov a,eax
               popad
       }
       return a%X;
}

The cons. for this function is that there is no guarantee that the target processor supports it, but i see it as a good step towards writing my self-implemented function.

Any ideas or comments are very welcome.

Sunday, August 15, 2010

Simple Permutations Algorithm

Today i decided to write my own algorithm for generating permutations for a given set of integers. One of the ideas that hit my mind was using array cyclic shift.

The algorithm is as follows:_

for 1 2 3 4
it would be like this
1 2 3 4 --->2341-->3412-->4123

For each set of the above 4 sets, the element with index 0 is fixed and the rest of the array is shifted. In this manner:

1234-->1342-->1423

For each one of the above 3 sets, the elements with indexes 0 and 1 are fixed  and the rest of the array is shifted.
In this manner:

1234-->1243

Here is the code for my algorithm

void permutation(int X[],int size,int level)
{
//----------------------------
if(level==size-1) return;
//----------------------------
int* A=(int*)malloc(size*sizeof(int));
memcpy(A,X,size*sizeof(int));
//----------------------------
int i=0;
while(i<(size-level)) { shift(&A[level],size-level,i); if(!level) print(A,size); else if(i) print(A,size); permutation(A,size,level+1); i++; memcpy(A,X,size*sizeof(int)); } free(A); return; }

Any suggestions are very welcome.  You can find an example here.