function MD5String(M: string): MD5Digest;
function MD5File(N: string): MD5Digest;
function MD5Print(D: MD5Digest): string;
function MD5Match(D1, D2: MD5Digest): Boolean;
function RivestStr(Str: string): string;
//字符串加密函数
function RivestFile(FileName: string): string;
//文件加密函数
var
S: PByte;
T: PDWORD;
I: longword;
begin
S := Source;
T := Target;
for I := 1 to Count div 4 do begin
T^ := S^;
inc(S);
T^ := T^ or (S^ shl 8);
inc(S);
T^ := T^ or (S^ shl 16);
inc(S);
T^ := T^ or (S^ shl 24);
inc(S);
inc(T);
end;
end;
var
S: PDWORD;
T: PByte;
I: longword;
begin
S := Source;
T := Target;
for I := 1 to Count do begin
T^ := S^ and $ff;
inc(T);
T^ := (S^ shr 8) and $ff;
inc(T);
T^ := (S^ shr 16) and $ff;
inc(T);
T^ := (S^ shr 24) and $ff;
inc(T);
inc(S);
end;
end;
//对输入数据作变换
procedure Transform(Buffer: pointer; var State: MD5State);
var
a, b, c, d: DWORD;
Block: MD5Block;
begin
Encode(Buffer, @Block, 64);
a := State[0];
b := State[1];
c := State[2];
d := State[3];
//第一轮
FF (a, b, c, d, Block[ 0], 7, $d76aa478);
FF (d, a, b, c, Block[ 1], 12, $e8c7b756);
FF (c, d, a, b, Block[ 2], 17, $242070db);
FF (b, c, d, a, Block[ 3], 22, $c1bdceee);
FF (a, b, c, d, Block[ 4], 7, $f57c0faf);
FF (d, a, b, c, Block[ 5], 12, $4787c62a);
FF (c, d, a, b, Block[ 6], 17, $a8304613);
FF (b, c, d, a, Block[ 7], 22, $fd469501);
FF (a, b, c, d, Block[ 8], 7, $698098d8);
FF (d, a, b, c, Block[ 9], 12, $8b44f7af);
FF (c, d, a, b, Block[10], 17, $ffff5bb1);
FF (b, c, d, a, Block[11], 22, $895cd7be);
FF (a, b, c, d, Block[12], 7, $6b901122);
FF (d, a, b, c, Block[13], 12, $fd987193);
FF (c, d, a, b, Block[14], 17, $a679438e);
FF (b, c, d, a, Block[15], 22, $49b40821);
//第二轮
GG (a, b, c, d, Block[ 1], 5, $f61e2562);
GG (d, a, b, c, Block[ 6], 9, $c040b340);
GG (c, d, a, b, Block[11], 14, $265e5a51);
GG (b, c, d, a, Block[ 0], 20, $e9b6c7aa);
GG (a, b, c, d, Block[ 5], 5, $d62f105d);
GG (d, a, b, c, Block[10], 9, $2441453);
GG (c, d, a, b, Block[15], 14, $d8a1e681);
GG (b, c, d, a, Block[ 4], 20, $e7d3fbc8);
GG (a, b, c, d, Block[ 9], 5, $21e1cde6);
GG (d, a, b, c, Block[14], 9, $c33707d6);
GG (c, d, a, b, Block[ 3], 14, $f4d50d87);
GG (b, c, d, a, Block[ 8], 20, $455a14ed);
GG (a, b, c, d, Block[13], 5, $a9e3e905);
GG (d, a, b, c, Block[ 2], 9, $fcefa3f8);
GG (c, d, a, b, Block[ 7], 14, $676f02d9);
GG (b, c, d, a, Block[12], 20, $8d2a4c8a);
//第三轮
HH (a, b, c, d, Block[ 5], 4, $fffa3942);
HH (d, a, b, c, Block[ 8], 11, $8771f681);
HH (c, d, a, b, Block[11], 16, $6d9d6122);
HH (b, c, d, a, Block[14], 23, $fde5380c);
HH (a, b, c, d, Block[ 1], 4, $a4beea44);
HH (d, a, b, c, Block[ 4], 11, $4bdecfa9);
HH (c, d, a, b, Block[ 7], 16, $f6bb4b60);
HH (b, c, d, a, Block[10], 23, $bebfbc70);
HH (a, b, c, d, Block[13], 4, $289b7ec6);
HH (d, a, b, c, Block[ 0], 11, $eaa127fa);
HH (c, d, a, b, Block[ 3], 16, $d4ef3085);
HH (b, c, d, a, Block[ 6], 23, $4881d05);
HH (a, b, c, d, Block[ 9], 4, $d9d4d039);
HH (d, a, b, c, Block[12], 11, $e6db99e5);
HH (c, d, a, b, Block[15], 16, $1fa27cf8);
HH (b, c, d, a, Block[ 2], 23, $c4ac5665);
//第四轮
II (a, b, c, d, Block[ 0], 6, $f4292244);
II (d, a, b, c, Block[ 7], 10, $432aff97);
II (c, d, a, b, Block[14], 15, $ab9423a7);
II (b, c, d, a, Block[ 5], 21, $fc93a039);
II (a, b, c, d, Block[12], 6, $655b59c3);
II (d, a, b, c, Block[ 3], 10, $8f0ccc92);
II (c, d, a, b, Block[10], 15, $ffeff47d);
II (b, c, d, a, Block[ 1], 21, $85845dd1);
II (a, b, c, d, Block[ 8], 6, $6fa87e4f);
II (d, a, b, c, Block[15], 10, $fe2ce6e0);
II (c, d, a, b, Block[ 6], 15, $a3014314);
II (b, c, d, a, Block[13], 21, $4e0811a1);
II (a, b, c, d, Block[ 4], 6, $f7537e82);
II (d, a, b, c, Block[11], 10, $bd3af235);
II (c, d, a, b, Block[ 2], 15, $2ad7d2bb);
II (b, c, d, a, Block[ 9], 21, $eb86d391);
//将A,B,C,D分别加上a,b,c,d
inc(State[0], a);
inc(State[1], b);
inc(State[2], c);
inc(State[3], d);
end;
//四个32位变量初始化A,B,C,D(MD5参数)
procedure MD5Init(var Context: MD5Context);
begin
with Context do begin
State[0] := $67452301;
State[1] := $efcdab89;
State[2] := $98badcfe;
State[3] := $10325476;
Count[0] := 0;
Count[1] := 0;
//用0填充64位Buffer
ZeroMemory(@Buffer, SizeOf(MD5Buffer));
end;
end;
//MD5模块更新操作,产生MD5消息摘要
operation, processing another message block, and updating the
context.
procedure MD5Update(var Context: MD5Context; Input: pChar; Length: longword);
var
Index: longword;
PartLen: longword;
I: longword;
begin
with Context do begin
//计算number of bytes mod 64
Index := (Count[0] shr 3) and $3f;
//Update number of bits
inc(Count[0], Length shl 3);
if Count[0] < (Length shl 3) then inc(Count[1]);
inc(Count[1], Length shr 29);
end;
PartLen := 64 - Index;
//变换操作
if Length >= PartLen then begin
//输入Buffer剩余部分
CopyMemory(@Context.Buffer[Index], Input, PartLen);
Transform(@Context.Buffer, Context.State);
I := PartLen;
while I + 63 < Length do begin
Transform(@Input[I], Context.State);
inc(I, 64);
end;
Index := 0;
end else I := 0;
CopyMemory(@Context.Buffer[Index], @Input[I], Length - I);
end;
//MD5算法结束部分. 固定MD5消息摘要操作, 写入消息摘要以及将其余部分填充0操作.
procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);
//输出结果.A,B,C,D连续存放,共16个字节,128位。按十六进制依次输出这个16个字节。
var
Bits: MD5CBits;
Index: longword;
PadLen: longword;
begin
Decode(@Context.Count, @Bits, 2);
//Pad out to 56 mod 64
Index := (Context.Count[0] shr 3) and $3f;
if Index < 56 then PadLen := 56 - Index else PadLen := 120 - Index;
MD5Update(Context, @PADDING, PadLen);
//扩展长度 (填充前)
MD5Update(Context, @Bits, 8);
//将State值(A,B,C,D)存储于摘要中
Decode(@Context.State, @Digest, 4);
//以0填充其余部分
ZeroMemory(@Context, SizeOf(MD5Context));
end;
function MD5String(M: string): MD5Digest;
var
Context: MD5Context;
begin
MD5Init(Context);
MD5Update(Context, pChar(M), length(M));
MD5Final(Context, Result);
end;
function MD5File(N: string): MD5Digest;
var
FileHandle: THandle;
MapHandle: THandle;
ViewPointer: pointer;
Context: MD5Context;
begin
MD5Init(Context);
FileHandle := CreateFile(pChar(N), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
if FileHandle <> INVALID_HANDLE_VALUE then try
MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
if MapHandle <> 0 then try
ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0);
if ViewPointer <> nil then try
MD5Update(Context, ViewPointer, GetFileSize(FileHandle, nil));
finally
UnmapViewOfFile(ViewPointer);
end;
finally
CloseHandle(MapHandle);
end;
finally
CloseHandle(FileHandle);
end;
MD5Final(Context, Result);
end;
function MD5Print(D: MD5Digest): string;
var
I: byte;
const
Digits: array[0..15] of char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
begin
Result := '';
for I := 0 to 15 do Result := Result + Digits[(D[I] shr 4) and $0f] + Digits[D[I] and $0f];
end;
function MD5Match(D1, D2: MD5Digest): boolean;
var
I: byte;
begin
I := 0;
Result := TRUE;
while Result and (I < 16) do begin
Result := D1[I] = D2[I];
inc(I);
end;
end;
function RivestStr(Str: string): string;
begin
Result := MD5Print(MD5String(Str));
end;
function RivestFile(FileName: string): string;
begin
Result := MD5Print(MD5File(FileName));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//function RivestStr(Str: string): string;为字符串加密函数;
if RivestStr(Edit1.Text)=Edit2.Text then
ShowMessage('注册成功!')
else
ShowMessage('注册失败!');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
end.
调试分析,所有标注如下:
用户定义的注释
地址 反汇编 注释
004511B4 push ebp Encode(Source, Target: pointer; Count: longword)调用开始
004511BA mov [ebp-C], ecx I: longword
004511BD mov [ebp-8], edx T: PDWORD
004511C0 mov [ebp-4], eax S: PByte
004511C6 mov [ebp-10], eax S := Source
004511CC mov [ebp-14], eax T := Target
004511D7 jbe short 00451230 for I := 1 to Count div 4 do begin
004511E3 mov eax, [ebp-10] T^ := S^
004511EE inc dword ptr [ebp-10] inc(S)
004511FD or [edx], eax T^ := T^ or (S^ shl 8)
004511FF inc dword ptr [ebp-10] inc(S)
0045120E or [edx], eax T^ := T^ or (S^ shl 16)
00451210 inc dword ptr [ebp-10] inc(S)
0045121F or [edx], eax T^ := T^ or (S^ shl 24)
00451221 inc dword ptr [ebp-10] inc(S)
00451228 inc dword ptr [ebp-18] inc(T)
00451234 push ebp Decode(Source, Target: pointer; Count: longword)调用开始
0045123A mov [ebp-C], ecx I: longword
0045123D mov [ebp-8], edx T: PByte
00451240 mov [ebp-4], eax S: PDWORD
00451254 jbe short 004512B1 for I := 1 to Count do begin
00451265 and al, 0FF T^ := S^ and $ff
0045126C inc dword ptr [ebp-14] inc(T)
00451277 and al, 0FF T^ := (S^ shr 8) and $ff
0045127E inc dword ptr [ebp-14] inc(T)
00451289 and al, 0FF T^ := (S^ shr 16) and $ff
00451290 inc dword ptr [ebp-14] inc(T)
0045129B and al, 0FF T^ := (S^ shr 24) and $ff
004512A2 inc dword ptr [ebp-14] inc(T)
004512A9 inc dword ptr [ebp-18] inc(S)
004512B8 push ebp Transform(Buffer: pointer; var State: MD5State)调用开始
004512CF call 004511B4 Encode(Buffer, @Block, 64)
004512D9 mov [ebp-C], eax a := State[0]
004512E2 mov [ebp-10], eax b := State[1]
004512EB mov [ebp-14], eax c := State[2]
004512F4 mov [ebp-18], eax d := State[3]
004512F7 mov eax, [ebp-18] 第一轮开始FF(a,b,c,d,Mj,s,ti)表示a=b+((a+(F(b,c,d)+Mj+ti)<<<s)
004514C7 mov eax, [ebp-18] 第二轮开始GG(a,b,c,d,Mj,s,ti)表示a=b+((a+(G(b,c,d)+Mj+ti)<<<s)
00451697 mov eax, [ebp-18] 第三轮开始HH(a,b,c,d,Mj,s,ti)表示a=b+((a+(H(b,c,d)+Mj+ti)<<<s)
00451867 mov eax, [ebp-18] 第四轮开始II(a,b,c,d,Mj,s,ti)表示a=b+((a+(I(b,c,d)+Mj+ti)<<<s)
00451A60 push ebp MD5开始初始化
00451A67 mov eax, [ebp-4] 四个32位链接变量初始化
00451A6A mov dword ptr [eax], 67452301 A=0x01234567,State[0]
00451A73 mov dword ptr [eax+4], EFCDAB89 B=0x89abcdef,State[1]
00451A7D mov dword ptr [eax+8], 98BADCFE C=0xfedcba98,State[2]
00451A87 mov dword ptr [eax+C], 10325476 D=0x76543210,State[3]
00451A93 mov [eax+10], edx Count[0]=0
00451A9B mov [eax+14], edx Count[1]=0
00451AA9 call 0040665C ZeroMemory(@Buffer, SizeOf(MD5Buffer)),此处为0012F550开始,buffer=0x40
00451AB4 push ebp MD5Update调用开始
00451AC7 mov eax, [eax+10] Index := (Count[0] shr 3) and $3f
00451AD3 mov eax, [ebp-C] inc(Count[0], Length shl 3)
00451AED jnb short 00451AF5 if Count[0] < (Length shl 3) then inc(Count[1])
00451AF5 mov eax, [ebp-C] inc(Count[1], Length shr 29)
00451B06 sub eax, [ebp-10] PartLen := 64 - Index
00451B12 jb short 00451B65 if Length >= PartLen then begin
00451B24 call 00406654 CopyMemory(@Context.Buffer[Index], Input, PartLen)
00451B32 call 004512B8 Transform(@Context.Buffer, Context.State)
00451B4A call 004512B8 Transform(@Input[I], Context.State)
00451B4F add dword ptr [ebp-18], 40 inc(I, 64)
00451B5C jb short 00451B3F while I + 63 < Length do begin
00451B5E xor eax, eax Index := 0
00451B65 xor eax, eax end else I := 0
00451B82 call 00406654 CopyMemory(@Context.Buffer[Index], Input, PartLen)
00451B8C push ebp MD5Final调用开始
00451BA1 mov ecx, 2 Decode(@Context.Count, @Bits, 2)
00451BA6 call 00451234 算法的主循环,循环的次数是消息中512位消息分组的数目
00451BAE mov eax, [eax+10] Index := (Context.Count[0] shr 3) and $3f
00451BBE jnb short 00451BCD if Index < 56 then PadLen := 56 - Index else PadLen := 120 - Index
00451BE3 call 00451AB4 MD5Update(Context, @PADDING, PadLen)
00451BF3 call 00451AB4 MD5Update(Context, @Bits, 8)
00451BFE mov ecx, 4 Decode(@Context.State, @Digest, 4)
00451C03 call 00451234 算法的主循环,循环的次数是消息中512位消息分组的数目
00451C10 call 0040665C ZeroMemory(@Context, SizeOf(MD5Context))
00451C41 call 00451A60 MD5Init
00451C49 call 004040D0 PartLen: longword
00451C52 call 004042D0 Index: longword
00451C5D call 00451AB4 MD5Update
00451C68 call 00451B8C MD5Final
00451D78 call 00451C1C MD5(name)
00452019 lea eax, [eax] (初始 CPU 选择)
0045201C push ebp 点击确定
00452042 mov eax, [eax+2F8] edtName : N.A.
00452048 call 00430348 TControl.GetText(TControl):TCaption;
00452053 call 00451D50 MD5(name)
0045206F call 00430348 TControl.GetText(TControl):TCaption;
00452085 call 0040421C LStrCmp;
比较后相等就注册成功.