代码如下: end.unit uMD5; interface uses SysUtils,Windows; function MD5(const Input: String): String; implementation const ADVAPI32 = 'advapi32.dll'; function CryptAcquireContext(phProv: PULONG; pszContainer: PAnsiChar; pszProvider: PAnsiChar; dwProvType: DWORD; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptAcquireContextA'; function CryptCreateHash(hProv: ULONG; Algid: ULONG; hKey: ULONG; dwFlags: DWORD; phHash: PULONG): BOOL; stdcall; external ADVAPI32 name 'CryptCreateHash'; function CryptHashData(hHash: ULONG; const pbData: PBYTE; dwDataLen: DWORD; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptHashData'; function CryptGetHashParam(hHash: ULONG; dwParam: DWORD; pbData: PBYTE; pdwDataLen: PDWORD; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptGetHashParam'; function CryptDestroyHash(hHash: ULONG): BOOL; stdcall; external ADVAPI32 name 'CryptDestroyHash'; function CryptReleaseContext(hProv: ULONG; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptReleaseContext'; function MD5(const Input: String): String; const HP_HASHVAL = $0002; PROV_RSA_FULL = 1; CRYPT_VERIFYCONTEXT = $F0000000; CRYPT_MACHINE_KEYSET = $00000020; ALG_CLASS_HASH = (4 SHL 13); ALG_TYPE_ANY = 0; ALG_SID_MD5 = 3; CALG_MD5 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5); var hCryptProvider : ULONG; hHash : ULONG; bHash : Array[0..$7F] Of Byte; dwHashLen : DWORD; pbContent : PByte; I : Integer; begin dwHashLen := 16; pbContent := Pointer(PChar(Input)); Result := ''; If CryptAcquireContext(@hCryptProvider,nil,nil,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT Or CRYPT_MACHINE_KEYSET) Then begin If CryptCreateHash(hCryptProvider,CALG_MD5,0,0,@hHash) Then begin If CryptHashData(hHash,pbContent,Length(Input),0) Then begin If CryptGetHashParam(hHash,HP_HASHVAL,@bHash[0],@dwHashLen,0) Then begin For I := 0 To dwHashLen - 1 Do begin Result := Result + Format('%.2x',[bHash[I]]); end; end; end; CryptDestroyHash(hHash); end; CryptReleaseContext(hCryptProvider, 0); end; Result := AnsiLowerCase(Result); end; end. 注:转载自 http://hi.baidu.com/0xcea4/blog/item/fecbaa7f711b4a300dd7da19.html