Einzelnen Beitrag anzeigen

gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#11

AW: MD5 Algorithmus - Wo ist mein Fehler?

  Alt 20. Jan 2015, 15:32
Der Rest sind Little-endian-Probleme! Hier eine Version mit meinen Änderungen (//*WE), die die richtigen Hashwerte für '', 'abc' und 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern' liefert:

Delphi-Quellcode:
procedure PrepareText;
var l,j: Int64;
var i: integer;
begin
  l:= Length(KlarText)*8;
  KlarText:= Klartext+ Chr(128);
  repeat
    KlarText:= KlarText+Chr(0);
  until Length(KlarText) mod 64 = 56;
  //*WE: Bitlaene als little-endian
  j := l;
  for i:=0 to 7 do begin
    KlarText:= KlarText+Chr(j and $ff);
    j := j shr 8;
  end;
end;

procedure HashMD5;
var A,B,C,D,temp,RotWert,FWert: LongWord;
var M: array[0..15] of LongWord;
var TempText,Hash: AnsiString;
var parts,p,j,g: integer; //Anzahl der 512 bit Stücke im KlarText
begin

  //Variablen auf Standard setzen:
  a0:= StrToInt('$67452301');
  b0:= StrToInt('$EFCDAB89');
  c0:= StrToInt('$98BADCFE');
  d0:= StrToInt('$10325476');

  PrepareText;

  parts:= Length(KlarText) div 64;

  for p:=1 to parts do begin
    TempText:= Copy(KlarText, ((p-1)*64+1), 64);
    for j:= 0 to 15 do begin
      //*WE: Block als little-endian
      M[j]:= ( ((Ord(TempText[4])) shl 24) or ((Ord(TempText[3])) shl 16) or
              ((Ord(TempText[2])) shl 8) or (Ord(TempText[1])) );
      Delete(TempText, 1, 4);
    end;

    A:=a0;
    B:=b0;
    C:=c0;
    D:=d0;

    for j:=0 to 63 do begin
      Case j of
        0..15: begin
          g:= j;
          FWert:= F1(B,C,D);
          temp:= D;
          D:= C;
          C:= B;
          RotWert:= (((A+FWert+K[j]+M[g]) shl s[j])
                      or ((A+FWert+K[j]+M[g]) shr (32-s[j])));
          B:= B+RotWert;
          A:= temp;
        end;

        16..31: begin
          g:= (5*j +1) mod 16;
          FWert:= F2(B,C,D);
          temp:= D;
          D:= C;
          C:= B;
          RotWert:= (((A+FWert+K[j]+M[g]) shl s[j])
                      or ((A+FWert+K[j]+M[g]) shr (32-s[j])));
          B:= B+RotWert;
          A:= temp;
        end;

        32..47: begin
          g:= (3*j +5) mod 16;
          FWert:= F3(B,C,D);
          temp:= D;
          D:= C;
          C:= B;
          RotWert:= (((A+FWert+K[j]+M[g]) shl s[j])
                      or ((A+FWert+K[j]+M[g]) shr (32-s[j])));
          B:= B+RotWert;
          A:= temp;
        end;

        48..63: begin
          g:= (7*j) mod 16;
          FWert:= F4(B,C,D);
          temp:= D;
          D:= C;
          C:= B;
          RotWert:= (((A+FWert+K[j]+M[g]) shl s[j])
                      or ((A+FWert+K[j]+M[g]) shr (32-s[j])));
          B:= B+RotWert;
          A:= temp;
        end;
      end;
    end;
    a0:= a0+A;
    b0:= b0+B;
    c0:= c0+C;
    d0:= d0+D;
  end;

  //*WE: Hash als little-endian
  Hash := IntToHex(a0 and $ff,2) +
          IntToHex((a0 shr 8) and $ff,2) +
          IntToHex((a0 shr 16) and $ff,2) +
          IntToHex((a0 shr 24) and $ff,2) +
          IntToHex(b0 and $ff,2) +
          IntToHex((b0 shr 8) and $ff,2) +
          IntToHex((b0 shr 16) and $ff,2) +
          IntToHex((b0 shr 24) and $ff,2) +
          IntToHex(c0 and $ff,2) +
          IntToHex((c0 shr 8) and $ff,2) +
          IntToHex((c0 shr 16) and $ff,2) +
          IntToHex((c0 shr 24) and $ff,2) +
          IntToHex(d0 and $ff,2) +
          IntToHex((d0 shr 8) and $ff,2) +
          IntToHex((d0 shr 16) and $ff,2) +
          IntToHex((d0 shr 24) and $ff,2) ;
  Form1.Label1.Caption:= Hash;

end;
  Mit Zitat antworten Zitat