- UID
- 27100
注册时间2007-2-18
阅读权限20
最后登录1970-1-1
以武会友
该用户从未签到
|
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, jpeg, ExtCtrls;
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Button1: TButton;
- Image1: TImage;
- Label1: TLabel;
- Label2: TLabel;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- type LongWordArray =array of LongWord;
- type PArrayByte =array of Byte;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- function md5_f ( x, y, z : LongWord ) : LongWord ;
- begin
- Result := ( ( x and y) or ( ( not x) and z ) );
- end;
- function md5_g ( x, y, z : LongWord ) : LongWord ;
- begin
- Result := ( ( x and z) or ( ( not z) and y ) );
- end;
- function md5_h ( x, y, z : LongWord ) : LongWord ;
- begin
- Result := ( x xor y xor z );
- end;
- function md5_i ( x, y, z : LongWord ) : LongWord ;
- begin
- Result := ( y xor (x or (not z)));
- end;
- function md5_RotateLeft ( x, s: LongWord ) : LongWord ;
- begin
- Result := ( (x shl s) or (x shr (32- s)));
- end;
- procedure md5_ff ( var a:LongWord ;b,c,d,mj,s,ti :LongWord );
- begin
- a:=a+ md5_f(b,c,d)+mj+ti;
- a:=md5_RotateLeft(a,s);
- a:=a+b;
- end;
- procedure md5_gg ( var a:LongWord ;b,c,d,mj,s,ti :LongWord );
- begin
- a:=a+ md5_g(b,c,d)+mj+ti;
- a:=md5_RotateLeft(a,s);
- a:=a+b;
- end;
- procedure md5_hh ( var a:LongWord ;b,c,d,mj,s,ti :LongWord );
- begin
- a:=a+ md5_h(b,c,d)+mj+ti;
- a:=md5_RotateLeft(a,s);
- a:=a+b;
- end;
- procedure md5_ii ( var a:LongWord ;b,c,d,mj,s,ti :LongWord );
- begin
- a:=a+ md5_i(b,c,d)+mj+ti;
- a:=md5_RotateLeft(a,s);
- a:=a+b;
- end;
- procedure md5_MakeOneTurn ( var a,b,c,d:LongWord ; messageGroup:LongWordArray);
- begin
- md5_ff(a, b, c, d, messageGroup[ 0], 7, $d76aa478);
- md5_ff(d, a, b, c, messageGroup[ 1], 12, $e8c7b756);
- md5_ff(c, d, a, b, messageGroup[ 2], 17, $242070db);
- md5_ff(b, c, d, a, messageGroup[ 3], 22, $c1bdceee);
- md5_ff(a, b, c, d, messageGroup[ 4], 7, $f57c0faf);
- md5_ff(d, a, b, c, messageGroup[ 5], 12, $4787c62a);
- md5_ff(c, d, a, b, messageGroup[ 6], 17, $a8304613);
- md5_ff(b, c, d, a, messageGroup[ 7], 22, $fd469501);
- md5_ff(a, b, c, d, messageGroup[ 8], 7, $698098d8);
- md5_ff(d, a, b, c, messageGroup[ 9], 12, $8b44f7af);
- md5_ff(c, d, a, b, messageGroup[10], 17, $ffff5bb1);
- md5_ff(b, c, d, a, messageGroup[11], 22, $895cd7be);
- md5_ff(a, b, c, d, messageGroup[12], 7, $6b901122);
- md5_ff(d, a, b, c, messageGroup[13], 12, $fd987193);
- md5_ff(c, d, a, b, messageGroup[14], 17, $a679438e);
- md5_ff(b, c, d, a, messageGroup[15], 22, $49b40821);
- md5_gg(a, b, c, d, messageGroup[ 1], 5, $f61e2562);
- md5_gg(d, a, b, c, messageGroup[ 6], 9, $c040b340);
- md5_gg(c, d, a, b, messageGroup[11], 14, $265e5a51);
- md5_gg(b, c, d, a, messageGroup[ 0], 20, $e9b6c7aa);
- md5_gg(a, b, c, d, messageGroup[ 5], 5, $d62f105d);
- md5_gg(d, a, b, c, messageGroup[10], 9, $02441453);
- md5_gg(c, d, a, b, messageGroup[15], 14, $d8a1e681);
- md5_gg(b, c, d, a, messageGroup[ 4], 20, $e7d3fbc8);
- md5_gg(a, b, c, d, messageGroup[ 9], 5, $21e1cde6);
- md5_gg(d, a, b, c, messageGroup[14], 9, $c33707d6);
- md5_gg(c, d, a, b, messageGroup[ 3], 14, $f4d50d87);
- md5_gg(b, c, d, a, messageGroup[ 8], 20, $455a14ed);
- md5_gg(a, b, c, d, messageGroup[13], 5, $a9e3e905);
- md5_gg(d, a, b, c, messageGroup[ 2], 9, $fcefa3f8);
- md5_gg(c, d, a, b, messageGroup[ 7], 14, $676f02d9);
- md5_gg(b, c, d, a, messageGroup[12], 20, $8d2a4c8a);
-
- md5_hh(a, b, c, d, messageGroup[ 5], 4, $fffa3942);
- md5_hh(d, a, b, c, messageGroup[ 8], 11, $8771f681);
- md5_hh(c, d, a, b, messageGroup[11], 16, $6d9d6122);
- md5_hh(b, c, d, a, messageGroup[14], 23, $fde5380c);
- md5_hh(a, b, c, d, messageGroup[ 1], 4, $a4beea44);
- md5_hh(d, a, b, c, messageGroup[ 4], 11, $4bdecfa9);
- md5_hh(c, d, a, b, messageGroup[ 7], 16, $f6bb4b60);
- md5_hh(b, c, d, a, messageGroup[10], 23, $bebfbc70);
- md5_hh(a, b, c, d, messageGroup[13], 4, $289b7ec6);
- md5_hh(d, a, b, c, messageGroup[ 0], 11, $eaa127fa);
- md5_hh(c, d, a, b, messageGroup[ 3], 16, $d4ef3085);
- md5_hh(b, c, d, a, messageGroup[ 6], 23, $04881d05);
- md5_hh(a, b, c, d, messageGroup[ 9], 4, $d9d4d039);
- md5_hh(d, a, b, c, messageGroup[12], 11, $e6db99e5);
- md5_hh(c, d, a, b, messageGroup[15], 16, $1fa27cf8);
- md5_hh(b, c, d, a, messageGroup[ 2], 23, $c4ac5665);
-
- md5_ii(a, b, c, d, messageGroup[ 0], 6, $f4292244);
- md5_ii(d, a, b, c, messageGroup[ 7], 10, $432aff97);
- md5_ii(c, d, a, b, messageGroup[14], 15, $ab9423a7);
- md5_ii(b, c, d, a, messageGroup[ 5], 21, $fc93a039);
- md5_ii(a, b, c, d, messageGroup[12], 6, $655b59c3);
- md5_ii(d, a, b, c, messageGroup[ 3], 10, $8f0ccc92);
- md5_ii(c, d, a, b, messageGroup[10], 15, $ffeff47d);
- md5_ii(b, c, d, a, messageGroup[ 1], 21, $85845dd1);
- md5_ii(a, b, c, d, messageGroup[ 8], 6, $6fa87e4f);
- md5_ii(d, a, b, c, messageGroup[15], 10, $fe2ce6e0);
- md5_ii(c, d, a, b, messageGroup[ 6], 15, $a3014314);
- md5_ii(b, c, d, a, messageGroup[13], 21, $4e0811a1);
- md5_ii(a, b, c, d, messageGroup[ 4], 6, $f7537e82);
- md5_ii(d, a, b, c, messageGroup[11], 10, $bd3af235);
- md5_ii(c, d, a, b, messageGroup[ 2], 15, $2ad7d2bb);
- md5_ii(b, c, d, a, messageGroup[ 9], 21, $eb86d391);
- end;
- function md5_TurnType (PByte:PArrayByte):string;
- var
- i:LongWord;
- temp_num:Byte;
- temp: string ;
- begin
- Result :='';
- for i := 0 to 15 do
- begin
- temp_num :=pByte[i];
- temp := Format('%02x',[temp_num]);
- Result:=Result+temp;
- end;
- end;
- function md5_Digest ( var message :string ;len : LongWord ) : string ;
- type
- PointerToChar = ^char;
- PointerToByte = ^Byte;
- var
- thepointer,a,b,c,d,blen,groupLen,i:LongWord;
- save :array[1..4] of LongWord;
- Group :array[1..64] of char;
- p:array of char;
- pByte:PArrayByte;
- begin
- save[1]:=$67452301;
- save[2]:=$efcdab89;
- save[3]:=$98badcfe;
- save[4]:=$10325476;
- i:=1;
- thepointer:=0;
- while thepointer <= len do
- begin
- groupLen := len - thepointer;
- if(groupLen > 64) then groupLen := 64;
- for i := 1 to groupLen do
- Group[i] := message[thepointer + i];
- thepointer:=thepointer+groupLen;
- if groupLen < 64 then
- begin
- if (groupLen > 0) or (len = 0) then
- begin
- Group[i] := Chr($80);
- i:=i+1;
- end;
- while i<=64 do
- begin
- Group[i] := chr(0);
- i:=i+1;
- end;
- if groupLen < 56 then
- begin
- blen := len * 8;
- //执行下面这句后save第4个元素不知道为什么加1了
- p := @blen;
- //搞不明白为什么会出现这种错误,,所以在这里-1,希望高手指点下
- save[4]:=save[4]-1;
- for i := 57 to 60 do
- Group[i] := p[i - 57];
- thepointer := len + 1;
- end;
- end;
- a := save[1];
- b := save[2];
- c := save[3];
- d := save[4];
- md5_MakeOneTurn(a, b, c, d,@Group);
- save[1] := save[1] +a;
- save[2] := save[2] +b;
- save[3] := save[3] +c;
- save[4] := save[4] +d;
- end;
- Result:=md5_TurnType(@save);
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- text:string;
- len:LongWord;
- md5_text:string;
- begin
- text:=Edit1.Text;
- len :=Length(text);
- md5_text:=md5_Digest(text,len);
- Edit2.Text:=md5_text;
- end;
- end.
复制代码 上面代码有个地方有错,,注释有说明,希望会的指点下,,,
[ 本帖最后由 deletex 于 2007-12-22 17:55 编辑 ] |
|