Md5算法 ---16位的 ( 积分: 100 )

  • 主题发起人 主题发起人 duhai_lee
  • 开始时间 开始时间
D

duhai_lee

Unregistered / Unconfirmed
GUEST, unregistred user!
关于md5算法的delphi单元随处可见, 我碰到了一个asp的源码。 内容将跟在后面,我不知道,一个同样的原文,例如abc,经过md5加密之后是不是一成不变。 可是我看了我用delphi的md5单元加密后和asp的加密后的结果不一样。 这是为什么?
asp中加密的方法如下:
password=MD5(Trim(Request.Form("txl_password1")))
delphi中加密方法如下:
password := MD5Print(MD5String(Str));
××××××××××××××我的问题是:×××××××××××××
如何用delphi写出一个md5(不管它规范与否), 只要加密的结果和asp一样就行。
×××××××××××××××××××××××××××××××××

下面我分别贴出 delphi和asp的md5源码。
这是asp的:
<%
Private Const BITS_TO_A_BYTE = 8
Private Const BYTES_TO_A_WORD = 4
Private Const BITS_TO_A_WORD = 32
Private m_lOnBits(30)
Private m_l2Power(30)
Private Function LShift(lValue, iShiftBits)
If iShiftBits = 0 then
LShift = lValue
Exit Function
else
If iShiftBits = 31 then
If lValue And 1 then
LShift = &amp;H80000000
else
LShift = 0
End If
Exit Function
else
If iShiftBits < 0 Or iShiftBits > 31 then
Err.Raise 6
End If
If (lValue And m_l2Power(31 - iShiftBits)) then
LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &amp;H80000000
else
LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
End If
End Function
Private Function RShift(lValue, iShiftBits)
If iShiftBits = 0 then
RShift = lValue
Exit Function
else
If iShiftBits = 31 then
If lValue And &amp;H80000000 then
RShift = 1
else
RShift = 0
End If
Exit Function
else
If iShiftBits < 0 Or iShiftBits > 31 then
Err.Raise 6
End If
RShift = (lValue And &amp;H7FFFFFFE) / m_l2Power(iShiftBits)
If (lValue And &amp;H80000000) then
RShift = (RShift Or (&amp;H40000000 / m_l2Power(iShiftBits - 1)))
End If
End Function
Private Function RotateLeft(lValue, iShiftBits)
RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
End Function
Private Function AddUnsigned(lX, lY)
Dim lX4
Dim lY4
Dim lX8
Dim lY8
Dim lResult
lX8 = lX And &amp;H80000000
lY8 = lY And &amp;H80000000
lX4 = lX And &amp;H40000000
lY4 = lY And &amp;H40000000
lResult = (lX And &amp;H3FFFFFFF) + (lY And &amp;H3FFFFFFF)
If lX4 And lY4 then
lResult = lResult Xor &amp;H80000000 Xor lX8 Xor lY8
else
If lX4 Or lY4 then
If lResult And &amp;H40000000 then
lResult = lResult Xor &amp;HC0000000 Xor lX8 Xor lY8
else
lResult = lResult Xor &amp;H40000000 Xor lX8 Xor lY8
End If
else
lResult = lResult Xor lX8 Xor lY8
End If
AddUnsigned = lResult
End Function
Private Function md5_F(x, y, z)
md5_F = (x And y) Or ((Not x) And z)
End Function
Private Function md5_G(x, y, z)
md5_G = (x And z) Or (y And (Not z))
End Function
Private Function md5_H(x, y, z)
md5_H = (x Xor y Xor z)
End Function
Private Function md5_I(x, y, z)
md5_I = (y Xor (x Or (Not z)))
End Function
Private Sub md5_FF(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_GG(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_HH(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_II(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Function ConvertToWordArray(sMessage)
Dim lMessageLength
Dim lNumberOfWords
Dim lWordArray()
Dim lBytePosition
Dim lByteCount
Dim lWordCount
Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448
lMessageLength = Len(sMessage)
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) / BITS_TO_A_BYTE)) / (MODULUS_BITS / BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS / BITS_TO_A_WORD)
ReDim lWordArray(lNumberOfWords - 1)
lBytePosition = 0
lByteCount = 0
Do Until lByteCount >= lMessageLength
lWordCount = lByteCount / BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
lByteCount = lByteCount + 1
Loop
lWordCount = lByteCount / BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&amp;H80, lBytePosition)
lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
ConvertToWordArray = lWordArray
End Function
Private Function WordToHex(lValue)
Dim lByte
Dim lCount
For lCount = 0 To 3
lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
WordToHex = WordToHex &amp;
Right(&quot;0&quot;
&amp;
Hex(lByte), 2)
Next
End Function
Public Function MD5(sMessage)
m_lOnBits(0) = CLng(1)
m_lOnBits(1) = CLng(3)
m_lOnBits(2) = CLng(7)
m_lOnBits(3) = CLng(15)
m_lOnBits(4) = CLng(31)
m_lOnBits(5) = CLng(63)
m_lOnBits(6) = CLng(127)
m_lOnBits(7) = CLng(255)
m_lOnBits(8) = CLng(511)
m_lOnBits(9) = CLng(1023)
m_lOnBits(10) = CLng(2047)
m_lOnBits(11) = CLng(4095)
m_lOnBits(12) = CLng(8191)
m_lOnBits(13) = CLng(16383)
m_lOnBits(14) = CLng(32767)
m_lOnBits(15) = CLng(65535)
m_lOnBits(16) = CLng(131071)
m_lOnBits(17) = CLng(262143)
m_lOnBits(18) = CLng(524287)
m_lOnBits(19) = CLng(1048575)
m_lOnBits(20) = CLng(2097151)
m_lOnBits(21) = CLng(4194303)
m_lOnBits(22) = CLng(8388607)
m_lOnBits(23) = CLng(16777215)
m_lOnBits(24) = CLng(33554431)
m_lOnBits(25) = CLng(67108863)
m_lOnBits(26) = CLng(134217727)
m_lOnBits(27) = CLng(268435455)
m_lOnBits(28) = CLng(536870911)
m_lOnBits(29) = CLng(1073741823)
m_lOnBits(30) = CLng(2147483647)
m_l2Power(0) = CLng(1)
m_l2Power(1) = CLng(2)
m_l2Power(2) = CLng(4)
m_l2Power(3) = CLng(8)
m_l2Power(4) = CLng(16)
m_l2Power(5) = CLng(32)
m_l2Power(6) = CLng(64)
m_l2Power(7) = CLng(128)
m_l2Power(8) = CLng(256)
m_l2Power(9) = CLng(512)
m_l2Power(10) = CLng(1024)
m_l2Power(11) = CLng(2048)
m_l2Power(12) = CLng(4096)
m_l2Power(13) = CLng(8192)
m_l2Power(14) = CLng(16384)
m_l2Power(15) = CLng(32768)
m_l2Power(16) = CLng(65536)
m_l2Power(17) = CLng(131072)
m_l2Power(18) = CLng(262144)
m_l2Power(19) = CLng(524288)
m_l2Power(20) = CLng(1048576)
m_l2Power(21) = CLng(2097152)
m_l2Power(22) = CLng(4194304)
m_l2Power(23) = CLng(8388608)
m_l2Power(24) = CLng(16777216)
m_l2Power(25) = CLng(33554432)
m_l2Power(26) = CLng(67108864)
m_l2Power(27) = CLng(134217728)
m_l2Power(28) = CLng(268435456)
m_l2Power(29) = CLng(536870912)
m_l2Power(30) = CLng(1073741824)

Dim x
Dim k
Dim AA
Dim BB
Dim CC
Dim DD
Dim a
Dim b
Dim c
Dim d
Const S11 = 7
Const S12 = 12
Const S13 = 17
Const S14 = 22
Const S21 = 5
Const S22 = 9
Const S23 = 14
Const S24 = 20
Const S31 = 4
Const S32 = 11
Const S33 = 16
Const S34 = 23
Const S41 = 6
Const S42 = 10
Const S43 = 15
Const S44 = 21
x = ConvertToWordArray(sMessage)
a = &amp;H67452301
b = &amp;HEFCDAB89
c = &amp;H98BADCFE
d = &amp;H10325476
For k = 0 To UBound(x) Step 16
AA = a
BB = b
CC = c
DD = d
md5_FF a, b, c, d, x(k + 0), S11, &amp;HD76AA478
md5_FF d, a, b, c, x(k + 1), S12, &amp;HE8C7B756
md5_FF c, d, a, b, x(k + 2), S13, &amp;H242070DB
md5_FF b, c, d, a, x(k + 3), S14, &amp;HC1BDCEEE
md5_FF a, b, c, d, x(k + 4), S11, &amp;HF57C0FAF
md5_FF d, a, b, c, x(k + 5), S12, &amp;H4787C62A
md5_FF c, d, a, b, x(k + 6), S13, &amp;HA8304613
md5_FF b, c, d, a, x(k + 7), S14, &amp;HFD469501
md5_FF a, b, c, d, x(k + 8), S11, &amp;H698098D8
md5_FF d, a, b, c, x(k + 9), S12, &amp;H8B44F7AF
md5_FF c, d, a, b, x(k + 10), S13, &amp;HFFFF5BB1
md5_FF b, c, d, a, x(k + 11), S14, &amp;H895CD7BE
md5_FF a, b, c, d, x(k + 12), S11, &amp;H6B901122
md5_FF d, a, b, c, x(k + 13), S12, &amp;HFD987193
md5_FF c, d, a, b, x(k + 14), S13, &amp;HA679438E
md5_FF b, c, d, a, x(k + 15), S14, &amp;H49B40821
md5_GG a, b, c, d, x(k + 1), S21, &amp;HF61E2562
md5_GG d, a, b, c, x(k + 6), S22, &amp;HC040B340
md5_GG c, d, a, b, x(k + 11), S23, &amp;H265E5A51
md5_GG b, c, d, a, x(k + 0), S24, &amp;HE9B6C7AA
md5_GG a, b, c, d, x(k + 5), S21, &amp;HD62F105D
md5_GG d, a, b, c, x(k + 10), S22, &amp;H2441453
md5_GG c, d, a, b, x(k + 15), S23, &amp;HD8A1E681
md5_GG b, c, d, a, x(k + 4), S24, &amp;HE7D3FBC8
md5_GG a, b, c, d, x(k + 9), S21, &amp;H21E1CDE6
md5_GG d, a, b, c, x(k + 14), S22, &amp;HC33707D6
md5_GG c, d, a, b, x(k + 3), S23, &amp;HF4D50D87
md5_GG b, c, d, a, x(k + 8), S24, &amp;H455A14ED
md5_GG a, b, c, d, x(k + 13), S21, &amp;HA9E3E905
md5_GG d, a, b, c, x(k + 2), S22, &amp;HFCEFA3F8
md5_GG c, d, a, b, x(k + 7), S23, &amp;H676F02D9
md5_GG b, c, d, a, x(k + 12), S24, &amp;H8D2A4C8A
md5_HH a, b, c, d, x(k + 5), S31, &amp;HFFFA3942
md5_HH d, a, b, c, x(k + 8), S32, &amp;H8771F681
md5_HH c, d, a, b, x(k + 11), S33, &amp;H6D9D6122
md5_HH b, c, d, a, x(k + 14), S34, &amp;HFDE5380C
md5_HH a, b, c, d, x(k + 1), S31, &amp;HA4BEEA44
md5_HH d, a, b, c, x(k + 4), S32, &amp;H4BDECFA9
md5_HH c, d, a, b, x(k + 7), S33, &amp;HF6BB4B60
md5_HH b, c, d, a, x(k + 10), S34, &amp;HBEBFBC70
md5_HH a, b, c, d, x(k + 13), S31, &amp;H289B7EC6
md5_HH d, a, b, c, x(k + 0), S32, &amp;HEAA127FA
md5_HH c, d, a, b, x(k + 3), S33, &amp;HD4EF3085
md5_HH b, c, d, a, x(k + 6), S34, &amp;H4881D05
md5_HH a, b, c, d, x(k + 9), S31, &amp;HD9D4D039
md5_HH d, a, b, c, x(k + 12), S32, &amp;HE6DB99E5
md5_HH c, d, a, b, x(k + 15), S33, &amp;H1FA27CF8
md5_HH b, c, d, a, x(k + 2), S34, &amp;HC4AC5665
md5_II a, b, c, d, x(k + 0), S41, &amp;HF4292244
md5_II d, a, b, c, x(k + 7), S42, &amp;H432AFF97
md5_II c, d, a, b, x(k + 14), S43, &amp;HAB9423A7
md5_II b, c, d, a, x(k + 5), S44, &amp;HFC93A039
md5_II a, b, c, d, x(k + 12), S41, &amp;H655B59C3
md5_II d, a, b, c, x(k + 3), S42, &amp;H8F0CCC92
md5_II c, d, a, b, x(k + 10), S43, &amp;HFFEFF47D
md5_II b, c, d, a, x(k + 1), S44, &amp;H85845DD1
md5_II a, b, c, d, x(k + 8), S41, &amp;H6FA87E4F
md5_II d, a, b, c, x(k + 15), S42, &amp;HFE2CE6E0
md5_II c, d, a, b, x(k + 6), S43, &amp;HA3014314
md5_II b, c, d, a, x(k + 13), S44, &amp;H4E0811A1
md5_II a, b, c, d, x(k + 4), S41, &amp;HF7537E82
md5_II d, a, b, c, x(k + 11), S42, &amp;HBD3AF235
md5_II c, d, a, b, x(k + 2), S43, &amp;H2AD7D2BB
md5_II b, c, d, a, x(k + 9), S44, &amp;HEB86D391
a = AddUnsigned(a, AA)
b = AddUnsigned(b, BB)
c = AddUnsigned(c, CC)
d = AddUnsigned(d, DD)
Next
'MD5 = LCase(WordToHex(a) &amp;
WordToHex(b) &amp;
WordToHex(c) &amp;
WordToHex(d))
MD5=LCase(WordToHex(b) &amp;
WordToHex(c)) 'I crop this to fit 16byte database password :D
End Function
%>
这是delphi的:
// tabs = 2
// -----------------------------------------------------------------------------------------------
//
// MD5 Message-Digest for Delphi 4
//
// Delphi 4 Unit implementing the
// RSA Data Security, Inc. MD5 Message-Digest Algorithm
//
// Implementation of Ronald L. Rivest's RFC 1321
//
// Copyright ?1997-1999 Medienagentur Fichtner &amp;
Meyer
// Written by Matthias Fichtner
//
// -----------------------------------------------------------------------------------------------
// See RFC 1321 for RSA Data Security's copyright and license notice!
// -----------------------------------------------------------------------------------------------
//
// 14-Jun-97 mf Implemented MD5 according to RFC 1321 RFC 1321
// 16-Jun-97 mf Initial release of the compiled unit (no source code) RFC 1321
// 28-Feb-99 mf Added MD5Match function for comparing two digests RFC 1321
// 13-Sep-99 mf Reworked the entire unit RFC 1321
// 17-Sep-99 mf Reworked the &quot;Test Driver&quot;
project RFC 1321
// 19-Sep-99 mf Release of sources for MD5 unit and &quot;Test Driver&quot;
project RFC 1321
//
// -----------------------------------------------------------------------------------------------
// The latest release of md5.pas will always be available from
// the distribution site at: http://www.fichtner.net/delphi/md5/
// -----------------------------------------------------------------------------------------------
// Please send questions, bug reports and suggestions
// regarding this code to: mfichtner@fichtner-meyer.com
// -----------------------------------------------------------------------------------------------
// This code is provided &quot;as is&quot;
without express or
// implied warranty of any kind. Use it at your own risk.
// -----------------------------------------------------------------------------------------------
unit md5;
// -----------------------------------------------------------------------------------------------
INTERFACE
// -----------------------------------------------------------------------------------------------
uses
Windows;
type
MD5Count = array[0..1] of DWORD;
MD5State = array[0..3] of DWORD;
MD5Block = array[0..15] of DWORD;
MD5CBits = array[0..7] of byte;
MD5Digest = array[0..15] of byte;
MD5Buffer = array[0..63] of byte;
MD5Context = record
State: MD5State;
Count: MD5Count;
Buffer: MD5Buffer;
end;

procedure MD5Init(var Context: MD5Context);
procedure MD5Update(var Context: MD5Context;
Input: pChar;
Length: longword);
procedure MD5Final(var Context: MD5Context;
var Digest: MD5Digest);
function MD5String(M: string): MD5Digest;
function MD5File(N: string): MD5Digest;
function MD5Print(D: MD5Digest): string;
function MD5Match(D1, D2: MD5Digest): boolean;
// -----------------------------------------------------------------------------------------------
IMPLEMENTATION
// -----------------------------------------------------------------------------------------------
var
PADDING: MD5Buffer = (
$80, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00
);
function F(x, y, z: DWORD): DWORD;
begin
Result := (x and y) or ((not x) and z);
end;

function G(x, y, z: DWORD): DWORD;
begin
Result := (x and z) or (y and (not z));
end;

function H(x, y, z: DWORD): DWORD;
begin
Result := x xor y xor z;
end;

function I(x, y, z: DWORD): DWORD;
begin
Result := y xor (x or (not z));
end;

procedure rot(var x: DWORD;
n: BYTE);
begin
x := (x shl n) or (x shr (32 - n));
end;

procedure FF(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, F(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

procedure GG(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, G(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

procedure HH(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, H(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

procedure II(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, I(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

// -----------------------------------------------------------------------------------------------
// Encode Count bytes at Source into (Count / 4) DWORDs at Target
procedure Encode(Source, Target: pointer;
Count: longword);
var
S: PByte;
T: PDWORD;
I: longword;
begin
S := Source;
T := Target;
for I := 1 to Count div 4do
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;

// Decode Count DWORDs at Source into (Count * 4) Bytes at Target
procedure Decode(Source, Target: pointer;
Count: longword);
var
S: PDWORD;
T: PByte;
I: longword;
begin
S := Source;
T := Target;
for I := 1 to Countdo
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;

// Transform State according to first 64 bytes at Buffer
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);
inc(State[0], a);
inc(State[1], b);
inc(State[2], c);
inc(State[3], d);
end;

// -----------------------------------------------------------------------------------------------
// Initialize given Context
procedure MD5Init(var Context: MD5Context);
begin
with Contextdo
begin
State[0] := $67452301;
State[1] := $efcdab89;
State[2] := $98badcfe;
State[3] := $10325476;
Count[0] := 0;
Count[1] := 0;
ZeroMemory(@Buffer, SizeOf(MD5Buffer));
end;
end;

// Update given Context to include Length bytes of Input
procedure MD5Update(var Context: MD5Context;
Input: pChar;
Length: longword);
var
Index: longword;
PartLen: longword;
I: longword;
begin
with Contextdo
begin
Index := (Count[0] shr 3) and $3f;
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
CopyMemory(@Context.Buffer[Index], Input, PartLen);
Transform(@Context.Buffer, Context.State);
I := PartLen;
while I + 63 < Lengthdo
begin
Transform(@Input, Context.State);
inc(I, 64);
end;
Index := 0;
end else
I := 0;
CopyMemory(@Context.Buffer[Index], @Input, Length - I);
end;

// Finalize given Context, create Digest and zeroize Context
procedure MD5Final(var Context: MD5Context;
var Digest: MD5Digest);
var
Bits: MD5CBits;
Index: longword;
PadLen: longword;
begin
Decode(@Context.Count, @Bits, 2);
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);
Decode(@Context.State, @Digest, 4);
ZeroMemory(@Context, SizeOf(MD5Context));
end;

// -----------------------------------------------------------------------------------------------
// Create digest of given Message
function MD5String(M: string): MD5Digest;
var
Context: MD5Context;
begin
MD5Init(Context);
MD5Update(Context, pChar(M), length(M));
MD5Final(Context, Result);
end;

// Create digest of file with given Name
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;

// Create hex representation of given Digest
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 15do
Result := Result + Digits[(D shr 4) and $0f] + Digits[D and $0f];
end;

// -----------------------------------------------------------------------------------------------
// Compare two Digests
function MD5Match(D1, D2: MD5Digest): boolean;
var
I: byte;
begin
I := 0;
Result := TRUE;
while Result and (I < 16)do
begin
Result := D1 = D2;
inc(I);
end;
end;

end.
 
关于md5算法的delphi单元随处可见, 我碰到了一个asp的源码。 内容将跟在后面,我不知道,一个同样的原文,例如abc,经过md5加密之后是不是一成不变。 可是我看了我用delphi的md5单元加密后和asp的加密后的结果不一样。 这是为什么?
asp中加密的方法如下:
password=MD5(Trim(Request.Form(&quot;txl_password1&quot;)))
delphi中加密方法如下:
password := MD5Print(MD5String(Str));
××××××××××××××我的问题是:×××××××××××××
如何用delphi写出一个md5(不管它规范与否), 只要加密的结果和asp一样就行。
×××××××××××××××××××××××××××××××××

下面我分别贴出 delphi和asp的md5源码。
这是asp的:
<%
Private Const BITS_TO_A_BYTE = 8
Private Const BYTES_TO_A_WORD = 4
Private Const BITS_TO_A_WORD = 32
Private m_lOnBits(30)
Private m_l2Power(30)
Private Function LShift(lValue, iShiftBits)
If iShiftBits = 0 then
LShift = lValue
Exit Function
else
If iShiftBits = 31 then
If lValue And 1 then
LShift = &amp;H80000000
else
LShift = 0
End If
Exit Function
else
If iShiftBits < 0 Or iShiftBits > 31 then
Err.Raise 6
End If
If (lValue And m_l2Power(31 - iShiftBits)) then
LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &amp;H80000000
else
LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
End If
End Function
Private Function RShift(lValue, iShiftBits)
If iShiftBits = 0 then
RShift = lValue
Exit Function
else
If iShiftBits = 31 then
If lValue And &amp;H80000000 then
RShift = 1
else
RShift = 0
End If
Exit Function
else
If iShiftBits < 0 Or iShiftBits > 31 then
Err.Raise 6
End If
RShift = (lValue And &amp;H7FFFFFFE) / m_l2Power(iShiftBits)
If (lValue And &amp;H80000000) then
RShift = (RShift Or (&amp;H40000000 / m_l2Power(iShiftBits - 1)))
End If
End Function
Private Function RotateLeft(lValue, iShiftBits)
RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
End Function
Private Function AddUnsigned(lX, lY)
Dim lX4
Dim lY4
Dim lX8
Dim lY8
Dim lResult
lX8 = lX And &amp;H80000000
lY8 = lY And &amp;H80000000
lX4 = lX And &amp;H40000000
lY4 = lY And &amp;H40000000
lResult = (lX And &amp;H3FFFFFFF) + (lY And &amp;H3FFFFFFF)
If lX4 And lY4 then
lResult = lResult Xor &amp;H80000000 Xor lX8 Xor lY8
else
If lX4 Or lY4 then
If lResult And &amp;H40000000 then
lResult = lResult Xor &amp;HC0000000 Xor lX8 Xor lY8
else
lResult = lResult Xor &amp;H40000000 Xor lX8 Xor lY8
End If
else
lResult = lResult Xor lX8 Xor lY8
End If
AddUnsigned = lResult
End Function
Private Function md5_F(x, y, z)
md5_F = (x And y) Or ((Not x) And z)
End Function
Private Function md5_G(x, y, z)
md5_G = (x And z) Or (y And (Not z))
End Function
Private Function md5_H(x, y, z)
md5_H = (x Xor y Xor z)
End Function
Private Function md5_I(x, y, z)
md5_I = (y Xor (x Or (Not z)))
End Function
Private Sub md5_FF(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_GG(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_HH(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_II(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Function ConvertToWordArray(sMessage)
Dim lMessageLength
Dim lNumberOfWords
Dim lWordArray()
Dim lBytePosition
Dim lByteCount
Dim lWordCount
Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448
lMessageLength = Len(sMessage)
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) / BITS_TO_A_BYTE)) / (MODULUS_BITS / BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS / BITS_TO_A_WORD)
ReDim lWordArray(lNumberOfWords - 1)
lBytePosition = 0
lByteCount = 0
Do Until lByteCount >= lMessageLength
lWordCount = lByteCount / BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
lByteCount = lByteCount + 1
Loop
lWordCount = lByteCount / BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&amp;H80, lBytePosition)
lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
ConvertToWordArray = lWordArray
End Function
Private Function WordToHex(lValue)
Dim lByte
Dim lCount
For lCount = 0 To 3
lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
WordToHex = WordToHex &amp;
Right(&quot;0&quot;
&amp;
Hex(lByte), 2)
Next
End Function
Public Function MD5(sMessage)
m_lOnBits(0) = CLng(1)
m_lOnBits(1) = CLng(3)
m_lOnBits(2) = CLng(7)
m_lOnBits(3) = CLng(15)
m_lOnBits(4) = CLng(31)
m_lOnBits(5) = CLng(63)
m_lOnBits(6) = CLng(127)
m_lOnBits(7) = CLng(255)
m_lOnBits(8) = CLng(511)
m_lOnBits(9) = CLng(1023)
m_lOnBits(10) = CLng(2047)
m_lOnBits(11) = CLng(4095)
m_lOnBits(12) = CLng(8191)
m_lOnBits(13) = CLng(16383)
m_lOnBits(14) = CLng(32767)
m_lOnBits(15) = CLng(65535)
m_lOnBits(16) = CLng(131071)
m_lOnBits(17) = CLng(262143)
m_lOnBits(18) = CLng(524287)
m_lOnBits(19) = CLng(1048575)
m_lOnBits(20) = CLng(2097151)
m_lOnBits(21) = CLng(4194303)
m_lOnBits(22) = CLng(8388607)
m_lOnBits(23) = CLng(16777215)
m_lOnBits(24) = CLng(33554431)
m_lOnBits(25) = CLng(67108863)
m_lOnBits(26) = CLng(134217727)
m_lOnBits(27) = CLng(268435455)
m_lOnBits(28) = CLng(536870911)
m_lOnBits(29) = CLng(1073741823)
m_lOnBits(30) = CLng(2147483647)
m_l2Power(0) = CLng(1)
m_l2Power(1) = CLng(2)
m_l2Power(2) = CLng(4)
m_l2Power(3) = CLng(8)
m_l2Power(4) = CLng(16)
m_l2Power(5) = CLng(32)
m_l2Power(6) = CLng(64)
m_l2Power(7) = CLng(128)
m_l2Power(8) = CLng(256)
m_l2Power(9) = CLng(512)
m_l2Power(10) = CLng(1024)
m_l2Power(11) = CLng(2048)
m_l2Power(12) = CLng(4096)
m_l2Power(13) = CLng(8192)
m_l2Power(14) = CLng(16384)
m_l2Power(15) = CLng(32768)
m_l2Power(16) = CLng(65536)
m_l2Power(17) = CLng(131072)
m_l2Power(18) = CLng(262144)
m_l2Power(19) = CLng(524288)
m_l2Power(20) = CLng(1048576)
m_l2Power(21) = CLng(2097152)
m_l2Power(22) = CLng(4194304)
m_l2Power(23) = CLng(8388608)
m_l2Power(24) = CLng(16777216)
m_l2Power(25) = CLng(33554432)
m_l2Power(26) = CLng(67108864)
m_l2Power(27) = CLng(134217728)
m_l2Power(28) = CLng(268435456)
m_l2Power(29) = CLng(536870912)
m_l2Power(30) = CLng(1073741824)

Dim x
Dim k
Dim AA
Dim BB
Dim CC
Dim DD
Dim a
Dim b
Dim c
Dim d
Const S11 = 7
Const S12 = 12
Const S13 = 17
Const S14 = 22
Const S21 = 5
Const S22 = 9
Const S23 = 14
Const S24 = 20
Const S31 = 4
Const S32 = 11
Const S33 = 16
Const S34 = 23
Const S41 = 6
Const S42 = 10
Const S43 = 15
Const S44 = 21
x = ConvertToWordArray(sMessage)
a = &amp;H67452301
b = &amp;HEFCDAB89
c = &amp;H98BADCFE
d = &amp;H10325476
For k = 0 To UBound(x) Step 16
AA = a
BB = b
CC = c
DD = d
md5_FF a, b, c, d, x(k + 0), S11, &amp;HD76AA478
md5_FF d, a, b, c, x(k + 1), S12, &amp;HE8C7B756
md5_FF c, d, a, b, x(k + 2), S13, &amp;H242070DB
md5_FF b, c, d, a, x(k + 3), S14, &amp;HC1BDCEEE
md5_FF a, b, c, d, x(k + 4), S11, &amp;HF57C0FAF
md5_FF d, a, b, c, x(k + 5), S12, &amp;H4787C62A
md5_FF c, d, a, b, x(k + 6), S13, &amp;HA8304613
md5_FF b, c, d, a, x(k + 7), S14, &amp;HFD469501
md5_FF a, b, c, d, x(k + 8), S11, &amp;H698098D8
md5_FF d, a, b, c, x(k + 9), S12, &amp;H8B44F7AF
md5_FF c, d, a, b, x(k + 10), S13, &amp;HFFFF5BB1
md5_FF b, c, d, a, x(k + 11), S14, &amp;H895CD7BE
md5_FF a, b, c, d, x(k + 12), S11, &amp;H6B901122
md5_FF d, a, b, c, x(k + 13), S12, &amp;HFD987193
md5_FF c, d, a, b, x(k + 14), S13, &amp;HA679438E
md5_FF b, c, d, a, x(k + 15), S14, &amp;H49B40821
md5_GG a, b, c, d, x(k + 1), S21, &amp;HF61E2562
md5_GG d, a, b, c, x(k + 6), S22, &amp;HC040B340
md5_GG c, d, a, b, x(k + 11), S23, &amp;H265E5A51
md5_GG b, c, d, a, x(k + 0), S24, &amp;HE9B6C7AA
md5_GG a, b, c, d, x(k + 5), S21, &amp;HD62F105D
md5_GG d, a, b, c, x(k + 10), S22, &amp;H2441453
md5_GG c, d, a, b, x(k + 15), S23, &amp;HD8A1E681
md5_GG b, c, d, a, x(k + 4), S24, &amp;HE7D3FBC8
md5_GG a, b, c, d, x(k + 9), S21, &amp;H21E1CDE6
md5_GG d, a, b, c, x(k + 14), S22, &amp;HC33707D6
md5_GG c, d, a, b, x(k + 3), S23, &amp;HF4D50D87
md5_GG b, c, d, a, x(k + 8), S24, &amp;H455A14ED
md5_GG a, b, c, d, x(k + 13), S21, &amp;HA9E3E905
md5_GG d, a, b, c, x(k + 2), S22, &amp;HFCEFA3F8
md5_GG c, d, a, b, x(k + 7), S23, &amp;H676F02D9
md5_GG b, c, d, a, x(k + 12), S24, &amp;H8D2A4C8A
md5_HH a, b, c, d, x(k + 5), S31, &amp;HFFFA3942
md5_HH d, a, b, c, x(k + 8), S32, &amp;H8771F681
md5_HH c, d, a, b, x(k + 11), S33, &amp;H6D9D6122
md5_HH b, c, d, a, x(k + 14), S34, &amp;HFDE5380C
md5_HH a, b, c, d, x(k + 1), S31, &amp;HA4BEEA44
md5_HH d, a, b, c, x(k + 4), S32, &amp;H4BDECFA9
md5_HH c, d, a, b, x(k + 7), S33, &amp;HF6BB4B60
md5_HH b, c, d, a, x(k + 10), S34, &amp;HBEBFBC70
md5_HH a, b, c, d, x(k + 13), S31, &amp;H289B7EC6
md5_HH d, a, b, c, x(k + 0), S32, &amp;HEAA127FA
md5_HH c, d, a, b, x(k + 3), S33, &amp;HD4EF3085
md5_HH b, c, d, a, x(k + 6), S34, &amp;H4881D05
md5_HH a, b, c, d, x(k + 9), S31, &amp;HD9D4D039
md5_HH d, a, b, c, x(k + 12), S32, &amp;HE6DB99E5
md5_HH c, d, a, b, x(k + 15), S33, &amp;H1FA27CF8
md5_HH b, c, d, a, x(k + 2), S34, &amp;HC4AC5665
md5_II a, b, c, d, x(k + 0), S41, &amp;HF4292244
md5_II d, a, b, c, x(k + 7), S42, &amp;H432AFF97
md5_II c, d, a, b, x(k + 14), S43, &amp;HAB9423A7
md5_II b, c, d, a, x(k + 5), S44, &amp;HFC93A039
md5_II a, b, c, d, x(k + 12), S41, &amp;H655B59C3
md5_II d, a, b, c, x(k + 3), S42, &amp;H8F0CCC92
md5_II c, d, a, b, x(k + 10), S43, &amp;HFFEFF47D
md5_II b, c, d, a, x(k + 1), S44, &amp;H85845DD1
md5_II a, b, c, d, x(k + 8), S41, &amp;H6FA87E4F
md5_II d, a, b, c, x(k + 15), S42, &amp;HFE2CE6E0
md5_II c, d, a, b, x(k + 6), S43, &amp;HA3014314
md5_II b, c, d, a, x(k + 13), S44, &amp;H4E0811A1
md5_II a, b, c, d, x(k + 4), S41, &amp;HF7537E82
md5_II d, a, b, c, x(k + 11), S42, &amp;HBD3AF235
md5_II c, d, a, b, x(k + 2), S43, &amp;H2AD7D2BB
md5_II b, c, d, a, x(k + 9), S44, &amp;HEB86D391
a = AddUnsigned(a, AA)
b = AddUnsigned(b, BB)
c = AddUnsigned(c, CC)
d = AddUnsigned(d, DD)
Next
'MD5 = LCase(WordToHex(a) &amp;
WordToHex(b) &amp;
WordToHex(c) &amp;
WordToHex(d))
MD5=LCase(WordToHex(b) &amp;
WordToHex(c)) 'I crop this to fit 16byte database password :D
End Function
%>
这是delphi的:
// tabs = 2
// -----------------------------------------------------------------------------------------------
//
// MD5 Message-Digest for Delphi 4
//
// Delphi 4 Unit implementing the
// RSA Data Security, Inc. MD5 Message-Digest Algorithm
//
// Implementation of Ronald L. Rivest's RFC 1321
//
// Copyright ?1997-1999 Medienagentur Fichtner &amp;
Meyer
// Written by Matthias Fichtner
//
// -----------------------------------------------------------------------------------------------
// See RFC 1321 for RSA Data Security's copyright and license notice!
// -----------------------------------------------------------------------------------------------
//
// 14-Jun-97 mf Implemented MD5 according to RFC 1321 RFC 1321
// 16-Jun-97 mf Initial release of the compiled unit (no source code) RFC 1321
// 28-Feb-99 mf Added MD5Match function for comparing two digests RFC 1321
// 13-Sep-99 mf Reworked the entire unit RFC 1321
// 17-Sep-99 mf Reworked the &quot;Test Driver&quot;
project RFC 1321
// 19-Sep-99 mf Release of sources for MD5 unit and &quot;Test Driver&quot;
project RFC 1321
//
// -----------------------------------------------------------------------------------------------
// The latest release of md5.pas will always be available from
// the distribution site at: http://www.fichtner.net/delphi/md5/
// -----------------------------------------------------------------------------------------------
// Please send questions, bug reports and suggestions
// regarding this code to: mfichtner@fichtner-meyer.com
// -----------------------------------------------------------------------------------------------
// This code is provided &quot;as is&quot;
without express or
// implied warranty of any kind. Use it at your own risk.
// -----------------------------------------------------------------------------------------------
unit md5;
// -----------------------------------------------------------------------------------------------
INTERFACE
// -----------------------------------------------------------------------------------------------
uses
Windows;
type
MD5Count = array[0..1] of DWORD;
MD5State = array[0..3] of DWORD;
MD5Block = array[0..15] of DWORD;
MD5CBits = array[0..7] of byte;
MD5Digest = array[0..15] of byte;
MD5Buffer = array[0..63] of byte;
MD5Context = record
State: MD5State;
Count: MD5Count;
Buffer: MD5Buffer;
end;

procedure MD5Init(var Context: MD5Context);
procedure MD5Update(var Context: MD5Context;
Input: pChar;
Length: longword);
procedure MD5Final(var Context: MD5Context;
var Digest: MD5Digest);
function MD5String(M: string): MD5Digest;
function MD5File(N: string): MD5Digest;
function MD5Print(D: MD5Digest): string;
function MD5Match(D1, D2: MD5Digest): boolean;
// -----------------------------------------------------------------------------------------------
IMPLEMENTATION
// -----------------------------------------------------------------------------------------------
var
PADDING: MD5Buffer = (
$80, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00
);
function F(x, y, z: DWORD): DWORD;
begin
Result := (x and y) or ((not x) and z);
end;

function G(x, y, z: DWORD): DWORD;
begin
Result := (x and z) or (y and (not z));
end;

function H(x, y, z: DWORD): DWORD;
begin
Result := x xor y xor z;
end;

function I(x, y, z: DWORD): DWORD;
begin
Result := y xor (x or (not z));
end;

procedure rot(var x: DWORD;
n: BYTE);
begin
x := (x shl n) or (x shr (32 - n));
end;

procedure FF(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, F(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

procedure GG(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, G(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

procedure HH(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, H(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

procedure II(var a: DWORD;
b, c, d, x: DWORD;
s: BYTE;
ac: DWORD);
begin
inc(a, I(b, c, d) + x + ac);
rot(a, s);
inc(a, b);
end;

// -----------------------------------------------------------------------------------------------
// Encode Count bytes at Source into (Count / 4) DWORDs at Target
procedure Encode(Source, Target: pointer;
Count: longword);
var
S: PByte;
T: PDWORD;
I: longword;
begin
S := Source;
T := Target;
for I := 1 to Count div 4do
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;

// Decode Count DWORDs at Source into (Count * 4) Bytes at Target
procedure Decode(Source, Target: pointer;
Count: longword);
var
S: PDWORD;
T: PByte;
I: longword;
begin
S := Source;
T := Target;
for I := 1 to Countdo
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;

// Transform State according to first 64 bytes at Buffer
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);
inc(State[0], a);
inc(State[1], b);
inc(State[2], c);
inc(State[3], d);
end;

// -----------------------------------------------------------------------------------------------
// Initialize given Context
procedure MD5Init(var Context: MD5Context);
begin
with Contextdo
begin
State[0] := $67452301;
State[1] := $efcdab89;
State[2] := $98badcfe;
State[3] := $10325476;
Count[0] := 0;
Count[1] := 0;
ZeroMemory(@Buffer, SizeOf(MD5Buffer));
end;
end;

// Update given Context to include Length bytes of Input
procedure MD5Update(var Context: MD5Context;
Input: pChar;
Length: longword);
var
Index: longword;
PartLen: longword;
I: longword;
begin
with Contextdo
begin
Index := (Count[0] shr 3) and $3f;
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
CopyMemory(@Context.Buffer[Index], Input, PartLen);
Transform(@Context.Buffer, Context.State);
I := PartLen;
while I + 63 < Lengthdo
begin
Transform(@Input, Context.State);
inc(I, 64);
end;
Index := 0;
end else
I := 0;
CopyMemory(@Context.Buffer[Index], @Input, Length - I);
end;

// Finalize given Context, create Digest and zeroize Context
procedure MD5Final(var Context: MD5Context;
var Digest: MD5Digest);
var
Bits: MD5CBits;
Index: longword;
PadLen: longword;
begin
Decode(@Context.Count, @Bits, 2);
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);
Decode(@Context.State, @Digest, 4);
ZeroMemory(@Context, SizeOf(MD5Context));
end;

// -----------------------------------------------------------------------------------------------
// Create digest of given Message
function MD5String(M: string): MD5Digest;
var
Context: MD5Context;
begin
MD5Init(Context);
MD5Update(Context, pChar(M), length(M));
MD5Final(Context, Result);
end;

// Create digest of file with given Name
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;

// Create hex representation of given Digest
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 15do
Result := Result + Digits[(D shr 4) and $0f] + Digits[D and $0f];
end;

// -----------------------------------------------------------------------------------------------
// Compare two Digests
function MD5Match(D1, D2: MD5Digest): boolean;
var
I: byte;
begin
I := 0;
Result := TRUE;
while Result and (I < 16)do
begin
Result := D1 = D2;
inc(I);
end;
end;

end.
 
假如这是我贴的asp的和delphi的md5算法不完全一样, 那么我该怎么做才能用delphi加密出和asp加密一样的效果。 谢谢大家关注。 不知道没关系,扔块砖也行。
 
要是有规律那不就很容易破了?结果不一样是很好的结果。
 
MD5_32('abc') = '900150983CD24FB0D6963F7D28E17F72'
MD5_16('abc') = '3CD24FB0D6963F7D'
 
我的意思是 如何把这个delphi的 md5算法修改后 , 加密出和asp加密出的效果完全一样????????
我不在乎delphi的是错误的或者asp的md5有问题, 我只关心如何让delphi加密出和这个asp加密出一样的效果。
 
--你应该确认到底是哪个算法有问题
不管是Delphi还是ASP,经过MD5加密后结果应该一样
就拿'abc'来说,如果结果不是'900150983CD24FB0D6963F7D28E17F72',说明算法有问题
 
没有弄过ASP
估计是编码的问题吧
 
不, 我们假设asp这个是正确的, 我现在想用delphi 写出和asp 加密一样的效果。。。
 
是长度不一样还是别的?
 
首先长度是不一样。 您可以试试。 内容更是不一样, 长度好像是delphi哪个比asp的长了一倍。
 
这就是了
据说:
现在在一般Asp下使用的MD5算法,使用的是老外写的一个vbScript算法,他内部使用的是16位算法。
而delphi下面的是32位的。
没有任何办法可以让他们所出的结果一致。
不过有个变通的法子。原理是在ms平台上,所有的脚本语言,包括vbScript,JavaScript和JScript,都是使用同一个脚本处理器,名称是ScriptHost,在微软官方下载站搜索Script56就可以找到。对这个ScriptHost,微软推出了一个Com组件,这个组件专门经过处理,可以供.net的运行库通过它可以调用并控制这个ScriptHost。
人家已经在.net上实现了,但是到了delphi。。。。。。
 
to : 不会到了delphi这里 只能把asp的代码翻译成delphi的把????
糟糕。 没有别的法子嘛?
 
to xianguo:
您能不能提供哪个16位的md5算法。? 谢谢。。 我试试这个效果看一样不??
 
谁有16位的md5 算法, delphi版的, 麻烦发一份。 谢谢
 
找了半天,没有找到合适的16位的代码,不过ASP的东西最大痛苦是居然类型可以不用定义,给你一个建议就是用VB做一个DLL,然后让Delphi来调用,要不真的很痛苦,另外一个建议看看id那个系列的控件有没有比较全的版本,好像叫TIdCoderMD5,我是D6,好像没有16位和32位的分别,看看有没有成熟控件

下面的代码好像也是32位的,给你做个参考吧,调用方法如下
procedure TForm1.Button1Click(Sender: TObject);
var
res : THashDigest;
md5 := TMD5;
begin
md5 := TMD5.Create;
md5.InputType := itString;
md5.InputString := str;
md5.POutputArray := @res;
md5.HashCalc;
MD5.Free;
//结果在res内。
end;

以下转自网络
//------------------------------------------------------------------------------
unit MD5;
interface
uses SysUtils;
type
PLong = ^Longword;
TInputType = (itString, itFile, itByteArray);
PHashDigest = ^THashDigest;
THashDigest = record
A,
B,
C,
D: Longword;
end;

TMD5 = class
private
FInputType: TInputType;
FInputString: string;
FInputFilePath: string;
FPInputArray: PByteArray;
FInputLength: Longword;
FOutputDigest: PHashDigest;
FActiveBlock: array[0..15] of Longword;
FA, FB, FC, FD, FAA, FBB, FCC, FDD: Longword;
FpA, FpB, FpC, FpD: PLong;
procedure FF(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
procedure GG(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
procedure HH(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
procedure II(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
public
procedure HashInit;
procedure HashTransform;
procedure HashFinish;
procedure HashBytes;
procedure HashFile;
procedure HashCalc;
property InputType: TInputType read FInputType write FInputType;
property InputString: string read FInputString write FInputString;
property InputFilePath: string read FInputFilePath write FInputFilePath;
property PInputArray: PByteArray read FPInputArray write FPInputArray;
property InputLength: Longword read FInputLength write FInputLength;
property POutputArray: PHashDigest read FOutputDigest write FOutputDigest;
end;

const
S11 = 7;
S12 = 12;
S13 = 17;
S14 = 22;
S21 = 5;
S22 = 9;
S23 = 14;
S24 = 20;
S31 = 4;
S32 = 11;
S33 = 16;
S34 = 23;
S41 = 6;
S42 = 10;
S43 = 15;
S44 = 21;
implementation
function ROL(val: Longword;
shift: Byte): LongWord;
assembler;
asm
MOV EAX, val;
MOV CL, shift;
ROL EAX, CL;
end;

procedure TMD5.HashInit;
var
a, b, c, d: Longword;
begin
a := $67452301;
b := $efcdab89;
c := $98badcfe;
d := $10325476;
Move(a, FA, 4);
FpA := @FA;
Move(b, FB, 4);
FpB := @FB;
Move(c, FC, 4);
FpC := @FC;
Move(d, FD, 4);
FpD := @FD;
end;

{Purpose: Round 1 of the Transform.
Equivalent to a = b + ((a + F(b,c,d) + x + ac) <<< s)
Where F(b,c,d) = b And c Or Not(b) And d}
procedure TMD5.FF(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
var
Fret: Longword;
begin
Fret := ((PLong(b)^) and (PLong(c)^)) or ((not(PLong(b)^)) and (PLong(d)^));
PLong(a)^ := PLong(a)^ + Fret + PLong(x)^ + ac;
Longword(a^) := ROL(Longword(a^), s);
Inc(PLong(a)^, PLong(b)^);
end;

{Purpose: Round 2 of the Transform.
Equivalent to a = b + ((a + G(b,c,d) + x + ac) <<< s)
Where G(b,c,d) = b And d Or c Not d}
procedure TMD5.GG(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
var
Gret: Longword;
begin
Gret := (PLong(b)^ and PLong(d)^) or ( PLong(c)^ and (not PLong(d)^));
PLong(a)^ := PLong(a)^ + Gret + PLong(x)^ + ac;
Longword(a^) := ROL(Longword(a^), s);
Inc(PLong(a)^, PLong(b)^);
end;

{Purpose: Round 3 of the Transform.
Equivalent to a = b + ((a + H(b,c,d) + x + ac) <<< s)
Where H(b,c,d) = b Xor c Xor d}
procedure TMD5.HH(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
var
Hret: Longword;
begin
Hret := PLong(b)^ xor PLong(c)^ xor PLong(d)^;
PLong(a)^ := PLong(a)^ + Hret + PLong(x)^ + ac;
Longword(a^) := ROL(Longword(a^), s);
PLong(a)^ := PLong(b)^ + PLong(a)^;
end;

{Purpose: Round 4 of the Transform.
Equivalent to a = b + ((a + I(b,c,d) + x + ac) <<< s)
Where I(b,c,d) = C Xor (b Or Not(d))}
procedure TMD5.II(a, b, c, d, x: Pointer;
s: Byte;
ac: Longword);
var
Iret: Longword;
begin
Iret := (PLong(c)^ xor (PLong(b)^ or (not PLong(d)^)));
PLong(a)^ := PLong(a)^ + Iret + PLong(x)^ + ac;
Longword(a^) := ROL(PLong(a)^, s );
PLong(a)^ := PLong(b)^ + PLong(a)^;
end;

{Purpose: Perform Step 4 of the algorithm. This is where all the important
stuff happens. This performs the rounds on a 64Byte Block. This
procedure should be called in a loop until all input data has been
transformed.}
procedure TMD5.HashTransform;
begin
FAA := FA;
FBB := FB;
FCC := FC;
FDD := FD;
{ Round 1 }
FF(FpA, FpB, FpC, FpD, @FActiveBlock[ 0], S11, $d76aa478);
{ 1 }
FF(FpD, FpA, FpB, FpC, @FActiveBlock[ 1], S12, $e8c7b756);
{ 2 }
FF(FpC, FpD, FpA, FpB, @FActiveBlock[ 2], S13, $242070db);
{ 3 }
FF(FpB, FpC, FpD, FpA, @FActiveBlock[ 3], S14, $c1bdceee);
{ 4 }
FF(FpA, FpB, FpC, FpD, @FActiveBlock[ 4], S11, $f57c0faf);
{ 5 }
FF(FpD, FpA, FpB, FpC, @FActiveBlock[ 5], S12, $4787c62a);
{ 6 }
FF(FpC, FpD, FpA, FpB, @FActiveBlock[ 6], S13, $a8304613);
{ 7 }
FF(FpB, FpC, FpD, FpA, @FActiveBlock[ 7], S14, $fd469501);
{ 8 }
FF(FpA, FpB, FpC, FpD, @FActiveBlock[ 8], S11, $698098d8);
{ 9 }
FF(FpD, FpA, FpB, FpC, @FActiveBlock[ 9], S12, $8b44f7af);
{ 10 }
FF(FpC, FpD, FpA, FpB, @FActiveBlock[10], S13, $ffff5bb1);
{ 11 }
FF(FpB, FpC, FpD, FpA, @FActiveBlock[11], S14, $895cd7be);
{ 12 }
FF(FpA, FpB, FpC, FpD, @FActiveBlock[12], S11, $6b901122);
{ 13 }
FF(FpD, FpA, FpB, FpC, @FActiveBlock[13], S12, $fd987193);
{ 14 }
FF(FpC, FpD, FpA, FpB, @FActiveBlock[14], S13, $a679438e);
{ 15 }
FF(FpB, FpC, FpD, FpA, @FActiveBlock[15], S14, $49b40821);
{ 16 }
{ Round 2 }
GG(FpA, FpB, FpC, FpD, @FActiveBlock[ 1], S21, $f61e2562);
{ 17 }
GG(FpD, FpA, FpB, FpC, @FActiveBlock[ 6], S22, $c040b340);
{ 18 }
GG(FpC, FpD, FpA, FpB, @FActiveBlock[11], S23, $265e5a51);
{ 19 }
GG(FpB, FpC, FpD, FpA, @FActiveBlock[ 0], S24, $e9b6c7aa);
{ 20 }
GG(FpA, FpB, FpC, FpD, @FActiveBlock[ 5], S21, $d62f105d);
{ 21 }
GG(FpD, FpA, FpB, FpC, @FActiveBlock[10], S22, $02441453);
{ 22 }
GG(FpC, FpD, FpA, FpB, @FActiveBlock[15], S23, $d8a1e681);
{ 23 }
GG(FpB, FpC, FpD, FpA, @FActiveBlock[ 4], S24, $e7d3fbc8);
{ 24 }
GG(FpA, FpB, FpC, FpD, @FActiveBlock[ 9], S21, $21e1cde6);
{ 25 }
GG(FpD, FpA, FpB, FpC, @FActiveBlock[14], S22, $c33707d6);
{ 26 }
GG(FpC, FpD, FpA, FpB, @FActiveBlock[ 3], S23, $f4d50d87);
{ 27 }
GG(FpB, FpC, FpD, FpA, @FActiveBlock[ 8], S24, $455a14ed);
{ 28 }
GG(FpA, FpB, FpC, FpD, @FActiveBlock[13], S21, $a9e3e905);
{ 29 }
GG(FpD, FpA, FpB, FpC, @FActiveBlock[ 2], S22, $fcefa3f8);
{ 30 }
GG(FpC, FpD, FpA, FpB, @FActiveBlock[ 7], S23, $676f02d9);
{ 31 }
GG(FpB, FpC, FpD, FpA, @FActiveBlock[12], S24, $8d2a4c8a);
{ 32 }
{ Round 3 }
HH(FpA, FpB, FpC, FpD, @FActiveBlock[ 5], S31, $fffa3942);
{ 33 }
HH(FpD, FpA, FpB, FpC, @FActiveBlock[ 8], S32, $8771f681);
{ 34 }
HH(FpC, FpD, FpA, FpB, @FActiveBlock[11], S33, $6d9d6122);
{ 35 }
HH(FpB, FpC, FpD, FpA, @FActiveBlock[14], S34, $fde5380c);
{ 36 }
HH(FpA, FpB, FpC, FpD, @FActiveBlock[ 1], S31, $a4beea44);
{ 37 }
HH(FpD, FpA, FpB, FpC, @FActiveBlock[ 4], S32, $4bdecfa9);
{ 38 }
HH(FpC, FpD, FpA, FpB, @FActiveBlock[ 7], S33, $f6bb4b60);
{ 39 }
HH(FpB, FpC, FpD, FpA, @FActiveBlock[10], S34, $bebfbc70);
{ 40 }
HH(FpA, FpB, FpC, FpD, @FActiveBlock[13], S31, $289b7ec6);
{ 41 }
HH(FpD, FpA, FpB, FpC, @FActiveBlock[ 0], S32, $eaa127fa);
{ 42 }
HH(FpC, FpD, FpA, FpB, @FActiveBlock[ 3], S33, $d4ef3085);
{ 43 }
HH(FpB, FpC, FpD, FpA, @FActiveBlock[ 6], S34, $04881d05);
{ 44 }
HH(FpA, FpB, FpC, FpD, @FActiveBlock[ 9], S31, $d9d4d039);
{ 45 }
HH(FpD, FpA, FpB, FpC, @FActiveBlock[12], S32, $e6db99e5);
{ 46 }
HH(FpC, FpD, FpA, FpB, @FActiveBlock[15], S33, $1fa27cf8);
{ 47 }
HH(FpB, FpC, FpD, FpA, @FActiveBlock[ 2], S34, $c4ac5665);
{ 48 }
{ Round 4 }
II(FpA, FpB, FpC, FpD, @FActiveBlock[ 0], S41, $f4292244);
{ 49 }
II(FpD, FpA, FpB, FpC, @FActiveBlock[ 7], S42, $432aff97);
{ 50 }
II(FpC, FpD, FpA, FpB, @FActiveBlock[14], S43, $ab9423a7);
{ 51 }
II(FpB, FpC, FpD, FpA, @FActiveBlock[ 5], S44, $fc93a039);
{ 52 }
II(FpA, FpB, FpC, FpD, @FActiveBlock[12], S41, $655b59c3);
{ 53 }
II(FpD, FpA, FpB, FpC, @FActiveBlock[ 3], S42, $8f0ccc92);
{ 54 }
II(FpC, FpD, FpA, FpB, @FActiveBlock[10], S43, $ffeff47d);
{ 55 }
II(FpB, FpC, FpD, FpA, @FActiveBlock[ 1], S44, $85845dd1);
{ 56 }
II(FpA, FpB, FpC, FpD, @FActiveBlock[ 8], S41, $6fa87e4f);
{ 57 }
II(FpD, FpA, FpB, FpC, @FActiveBlock[15], S42, $fe2ce6e0);
{ 58 }
II(FpC, FpD, FpA, FpB, @FActiveBlock[ 6], S43, $a3014314);
{ 59 }
II(FpB, FpC, FpD, FpA, @FActiveBlock[13], S44, $4e0811a1);
{ 60 }
II(FpA, FpB, FpC, FpD, @FActiveBlock[ 4], S41, $f7537e82);
{ 61 }
II(FpD, FpA, FpB, FpC, @FActiveBlock[11], S42, $bd3af235);
{ 62 }
II(FpC, FpD, FpA, FpB, @FActiveBlock[ 2], S43, $2ad7d2bb);
{ 63 }
II(FpB, FpC, FpD, FpA, @FActiveBlock[ 9], S44, $eb86d391);
{ 64 }
Inc(FA, FAA);
Inc(FB, FBB);
Inc(FC, FCC);
Inc(FD, FDD);
FillChar(FActiveBlock, SizeOf(FActiveBlock), #0);
end;

procedure TMD5.HashCalc;
var
PStr: PChar;
begin
HashInit;
case FInputType of
itFile:
HashFile;
itByteArray:
HashBytes;
itString:
begin
PStr := StrAlloc(Length(FInputString) + 1);
try
StrPCopy(PStr, FInputString);
FInputLength := Length(FInputString);
FPInputArray := Pointer(PStr);
HashBytes;
finally
StrDispose(PStr);
end;
end;
end;
HashFinish;
end;

procedure TMD5.HashBytes;
var
Buffer: array[0..4159] of Byte;
Count64: Comp;
Index: Longword;
begin
Move(FPInputArray^, Buffer, FInputLength);
Count64 := FInputLength * 8;
Buffer[FInputLength] := $80;
Inc(FInputLength);
while (FInputLength mod 64) <> 56do
begin
Buffer[FInputLength] := 0;
Inc(FInputLength);
end;
Move(Count64, Buffer[FInputLength], SizeOf(Count64));
Index := 0;
Inc(FInputLength, 8);
repeat
Move(Buffer[Index], FActiveBlock, 64);
HashTransform;
Inc(Index, 64);
until Index = FInputLength;
end;

procedure TMD5.HashFile;
var
do
neFile: Boolean;
InputFile: file;
Count64: Comp;
Index, NumRead: Integer;
Buffer: array[0..4159] of Byte;
begin
do
neFile := False;
AssignFile(InputFile, FInputFilePath);
Reset(InputFile, 1);
Count64 := 0;
repeat
BlockRead(InputFile, Buffer, 4096, NumRead);
Count64 := Count64 + NumRead;
if NumRead <> 4096 then
begin
Buffer[NumRead] := $80;
Inc(NumRead);
while (NumRead mod 64) <> 56do
begin
Buffer[NumRead] := 0;
Inc(NumRead);
end;
Count64 := Count64 * 8;
Move(Count64, Buffer[NumRead], 8);
Inc(NumRead, 8);
do
neFile := True;
end;
Index := 0;
repeat
Move(Buffer[Index], FActiveBlock, 64);
HashTransform;
Inc(Index, 64);
until Index = NumRead;
untildo
neFile;
CloseFile(InputFile);
end;

procedure TMD5.HashFinish;
begin
FOutputDigest^.A := Longword(FpA^);
FOutputDigest^.B := Longword(FpB^);
FOutputDigest^.C := Longword(FpC^);
FOutputDigest^.D := Longword(FpD^);
end;
end.
 
感谢楼上建议, 我马上去试。 立即放分
 
d7 没有哪个控件啊。。
 
有时间研究一下,,谢谢楼主
 
后退
顶部