Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: String handling issue with Free Pascal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    I don't want to save all possible strings, just the ones that are matching the hash.

    To avoid further misunderstanding, I will copy my current code here, and maybe you could tell me what's wrong because just when I thought it would work, it doesn't really. It runs, it's trying out everything, but something is bugged...

    Code:
    Program Md5BruteForce;
    
    Uses Md5;
    
    Var
    
    hash,test,tri:string;
    
    Num,Max,j:integer;
    
    Results: array[1..100] of string;
    
    
    Function NextChar(c:char):char; //This is for changing the characters in the string
    
    Begin
    
     if c='z' then NextChar:='0'
     else
     if c='9' then NextChar:='a'
     else
     NextChar:=Succ(c);
    
    End;
    
    Procedure ChangeChar(MaxLength,spot:integer; Var Str:string); //The main procedure
    
    Var
    
     counter:integer;
     i:integer;
    
    Begin
    
     counter:=0;
    
    
     if Str[spot]='9' then 
    
     begin
    
      if length(Str)<=MaxLength then 
    
      begin
    
       for i:=1 to length(Str) do
       begin
    
        if Str[i]='9' then counter:=counter+1; //Count the 9s in the string
    
       end;
    
       if counter=length(Str) then
       begin
    
        for i:=1 to length(Str) do Str[i]:='a'; 
        Str:=Str+'a'; //If every character is a 9, then change all of them back to 'a', and add a plus 'a' to the string
       end
    
       else
       begin
        Str[spot]:='a';
        ChangeChar(MaxLength, spot-1,Str); //The procedure calls itself, until a non-9 character is found
       end;
      end;
     end
     else
    
     Str[spot]:=NextChar(Str[spot]); //If a non-9 character is found then it is simply switched to the next character
    
    
    End;
    
    
    
    Begin
    
    for j:=1 to 100 do Results[j]:='' //Clearing the array, just to make sure...
    
    Writeln('Please Give me the hash');
    Readln(hash);
    
    Writeln('Please give me the estimated maximum length of the original text(characters)');
    Readln(Max);
    
    test:='a';
    
    Num:=1;
    
    Repeat
    
    tri:=MD5Print(Md5String(test));
    
    if tri=hash then
    
    begin
    
     Results[Num]:=test;
     Num:=Num+1; //Storing the matches in an array
    
    end;
    
    Write('Now trying: ',test);
    Write('       ');
    Writeln('Hash: ',tri); //A bit redundant, but it helped me debug the recursion, and it looks cool so I kept it.
    
    ChangeChar(Max,length(test),test); //Go to the next variation
    
    Until length(test)>Max; //Loop ends if the maximum character length is exceeded
    
    if Num>1 then //Checking if there is more than 0 results
    
    begin
    
     for j:=1 to Num do
      begin
    
      Writeln('Result #',j,': ',Results[Num]); //Writing out all the results
      end;
    end
    else
    
    Writeln('Sorry, your hash could not be recovered'); //If the search fails...
    
    Readln;
    
    End.
    I was testing with the string 'c7' at maximum length 2 and 3. (hash is: 4d3a21d8c684c09c19b93be911827fd5)
    The problem is that eventhough it tries every possible permutation now, for some reason it can't list the results. The output is exactly this(except for the long blocks of tries)
    'Result #1:
    Result #2:'
    And that's it. Empty strings. Arrrrgh it's driving me soooo mad, I'm pretty much done with the hard part, and some stupid error is stopping me.

  2. #2
    Nevermind, I'm done with it. It was the stupidest error ever.

    In the for cycle when I'm writing the results out
    Instead of Results[Num] I should use Results[j]

    Now the code is fully functional(except for that it takes forever to decode anything beyond 4 character length) Thanks for the input guys.

    Code:
    Program Md5BruteForce;
    
    Uses Md5;
    
    Var
    
    hash,test,tri:string;
    
    Num,Max,j:integer;
    
    Results:array[1..100] of string;
    
    
    Function NextChar(c:char):char; //This is for changing the characters in the string
    
    Begin
    
     if c='z' then NextChar:='0'
     else
     if c='9' then NextChar:='a'
     else
     NextChar:=Succ(c); //Succ=Next Character in ABC order, next number if the character is a number
    
    End;
    
    Procedure ChangeChar(MaxLength,spot:integer; Var Str:string); //The main procedure
    
    Var
    
     counter:integer;
     i:integer;
    Begin
    
     counter:=0;
    
    
     if Str[spot]='9' then
    
     begin
    
      if length(Str)<=MaxLength then
    
      begin
    
       for i:=1 to length(Str) do
       begin
    
        if Str[i]='9' then counter:=counter+1; //Count the 9s in the string
    
       end;
    
       if counter=length(Str) then
       begin
    
        for i:=1 to length(Str) do Str[i]:='a';
        Str:=Str+'a'; //If every character is a 9, then change all of them back to 'a', and add a plus 'a' to the string
    
       end
    
       else
       begin
        Str[spot]:='a';
        ChangeChar(MaxLength, spot-1,Str); //The procedure calls itself, until a non-9 character is found
       end;
      end;
     end
     else
    
     Str[spot]:=NextChar(Str[spot]); //If a non-9 character is found then it is simply switched to the next character
    
    
    End;
    
    
    
    Begin
    
    for j:=1 to 100 do
    
    begin
     Results[j]:=''; //Clearing the array, just to make sure...
    end;
    
    Writeln('Please Give me the hash');
    Readln(hash);
    
    Writeln('Please give me the estimated maximum length of the original text(characters)');
    Readln(Max);
    
    test:='a';
    
    Num:=1;
    
    Repeat
    
    tri:=MD5Print(Md5String(test));
    
    if tri=hash then
    
    begin
    Results[Num]:=test;
    Num:=Num+1; //Storing the matches in an array
    
    end;
    
    Write('Now trying: ',test);
    Write('       ');
    Writeln('Hash: ',tri); //A bit redundant, but it helped me debug the recursion, and it looks cool so I kept it.
    
    ChangeChar(Max,length(test),test); //Go to the next variation
    
    Until length(test)>Max; //Loop ends if the maximum character length is exceeded
    
    if Num>1 then //Checking if there is more than 0 results
    
    begin
    
     for j:=1 to Num-1 do
      begin
    
      Writeln('Result #',j,': ',Results[j]); //Writing out all the results
    
      end;
    end
    else
    Writeln('Sorry, your hash could not be recovered'); //If the search fails...
    
    Readln;
    
    End.

  3. #3
    Now why not implement multithreading to make everything run faster

  4. #4
    I actually needed to look up how multithreading exactly works in Pascal. And I still don't get half of it, so that will take some time. Keep in mind I've hardly ever used objects in my programs, only simple ones. This TThread class is a mess compaired to them.

    I've deleted the part of the program where it writes out every try, now it just outputs the result, and it is a lot faster now. At 5 character length it still seems unexplainably slow(I haven't tried with more). This multithreading seems like something that could work but I need to study it further before I can implement it in my program.

  5. #5
    If you would want to optimize it a bit, i would first get rid of NextChar() function. You could put all valid characters in a string constant, something like
    Code:
    const validstr = 'abcd....ABCD....XYZ0123456789';
    Then matter of changing character is simply increasing character index by 1.

    Code:
    index:=1;
    validstr[index] is 'a'
    inc(index);
    validstr[index] is now 'b'

  6. #6
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2
    Quote Originally Posted by Sogcelak View Post
    I actually needed to look up how multithreading exactly works in Pascal. And I still don't get half of it, so that will take some time. Keep in mind I've hardly ever used objects in my programs, only simple ones. This TThread class is a mess compaired to them.

    I've deleted the part of the program where it writes out every try, now it just outputs the result, and it is a lot faster now. At 5 character length it still seems unexplainably slow(I haven't tried with more). This multithreading seems like something that could work but I need to study it further before I can implement it in my program.
    I have a tutorial that covers the basics of multi-threading.
    :: AthenaOfDelphi :: My Blog :: My Software ::

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •