各位兄弟姐妹有没有delphi的MD5算法的denmo,小弟在项目中遇到棘手就是客户那边的要求用MD5加密,而且要用密钥的,但是我网上找了很多资料都没见到带密钥参数的算法
解决方案 »
- 关于delphi字符串一个很怪诞的问题!
- 如何使用Delphi捕捉其他程序的热键?
- 急救?怎样实现不同网段之间的广播?
- 给50分谁来答!调用报表的时候出错!
- ado和bde的探讨!~~~
- 求救SQL语句!返回无重复记录!在线等待!!!!!!!!
- 哪里有Delphi中常用的关键字解释说明资料?
- 急!急!急!在线,关于类型转换的问题。
- 高手请进,在线等待...急,急,急,急,急,急,急,急,急,急,急,急,急,急,急!
- 想写一个类似word的文字处理软件,有什么好方法。 100分哦!
- 您打算干到什么时候?有感于“搞编程的,晒晒你的年龄,我是不是挺老的了”
- 求类似java的hmac-sha1算法的delphi代码例子。
那么用MD5加密前把 明文+密钥 “ABCDEFGABC123” 后再用 MD5加密。
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
const
MaxBufSize = 16384;
MD5Version = 102;
CopyRight : String = 'MD5 Message-Digest (c) 97-98 F. Piette V1.02';
type
TMD5Context = record
State: array[0..3] of LongInt;
Count: array[0..1] of LongInt;
case Integer of
0: (BufChar: array[0..63] of Byte);
1: (BufLong: array[0..15] of LongInt);
end;
TMD5Digest = array[0..15] of Char; PMD5Buffer = ^TMD5Buffer;
TMD5Buffer = array[0..(MaxBufSize - 1)] of Char; TForm4 = class(TForm)
BitBtn1: TBitBtn;
Edit1: TEdit;
Edit2: TEdit;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; procedure MD5Init(var MD5Context: TMD5Context);
procedure MD5Update(var MD5Context: TMD5Context;
const Data;
Len: Integer);
procedure MD5Transform(var Buf: array of LongInt;
const Data: array of LongInt);
procedure MD5UpdateBuffer(var MD5Context: TMD5Context;
Buffer: Pointer;
BufSize: Integer);
procedure MD5Final(var Digest: TMD5Digest; var MD5Context: TMD5Context); function GetMD5(Buffer: Pointer; BufSize: Integer): string;
function StrMD5(Buffer : String): string;var
Form4: TForm4;
implementation{$R *.dfm}{ MD5 initialization. Begins an MD5 operation, writing a new context. }
procedure MD5Init(var MD5Context: TMD5Context);
begin
FillChar(MD5Context, SizeOf(TMD5Context), #0);
with MD5Context do begin
{ Load magic initialization constants. }
State[0] := LongInt($67452301);
State[1] := LongInt($EFCDAB89);
State[2] := LongInt($98BADCFE);
State[3] := LongInt($10325476);
end
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ MD5 block update operation. Continues an MD5 message-digest operation, }
{ processing another message block, and updating the context. }
procedure MD5Update(
var MD5Context: TMD5Context; { Context }
const Data; { Input block }
Len: Integer); { Length of input block }
type
TByteArray = array[0..0] of Byte;
var
Index: Word;
T: LongInt;
begin
with MD5Context do begin
T := Count[0];
Inc(Count[0], LongInt(Len) shl 3);
if Count[0] < T then
Inc(Count[1]);
Inc(Count[1], Len shr 29);
T := (T shr 3) and $3F;
Index := 0;
if T <> 0 then begin
Index := T;
T := 64 - T;
if Len < T then begin
Move(Data, BufChar[Index], Len);
Exit;
end;
Move(Data, BufChar[Index], T);
MD5Transform(State, BufLong);
Dec(Len, T);
Index := T; { Wolfgang Klein, 05/06/99 }
end;
while Len >= 64 do begin
Move(TByteArray(Data)[Index], BufChar, 64);
MD5Transform(State, BufLong);
Inc(Index, 64);
Dec(Len, 64);
end;
Move(TByteArray(Data)[Index], BufChar, Len);
end
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ MD5 finalization. Ends an MD5 message-digest operation, writing the message }
{ digest and zeroizing the context. }
procedure MD5Final(var Digest: TMD5Digest; var MD5Context: TMD5Context);
var
Cnt : Word;
P : Byte;
begin
with MD5Context do begin
Cnt := (Count[0] shr 3) and $3F;
P := Cnt;
BufChar[P] := $80;
Inc(P);
Cnt := 64 - 1 - Cnt;
if Cnt < 8 then begin
FillChar(BufChar[P], Cnt, #0);
MD5Transform(State, BufLong);
FillChar(BufChar, 56, #0);
end
else
FillChar(BufChar[P], Cnt - 8, #0);
BufLong[14] := Count[0];
BufLong[15] := Count[1];
MD5Transform(State, BufLong);
Move(State, Digest, 16)
end;
FillChar(MD5Context, SizeOf(TMD5Context), #0)
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ MD5 basic transformation. Transforms state based on block. }
procedure MD5Transform(
var Buf: array of LongInt;
const Data: array of LongInt);
var
A, B, C, D: LongInt; procedure Round1(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
begin
Inc(W, (Z xor (X and (Y xor Z))) + Data);
W := (W shl S) or (W shr (32 - S));
Inc(W, X)
end; procedure Round2(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
begin
Inc(W, (Y xor (Z and (X xor Y))) + Data);
W := (W shl S) or (W shr (32 - S));
Inc(W, X)
end; procedure Round3(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
begin
Inc(W, (X xor Y xor Z) + Data);
W := (W shl S) or (W shr (32 - S));
Inc(W, X)
end; procedure Round4(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
begin
Inc(W, (Y xor (X or not Z)) + Data);
W := (W shl S) or (W shr (32 - S));
Inc(W, X)
end;
begin
A := Buf[0];
B := Buf[1];
C := Buf[2];
D := Buf[3]; Round1(A, B, C, D, Data[ 0] + LongInt($d76aa478), 7);
Round1(D, A, B, C, Data[ 1] + LongInt($e8c7b756), 12);
Round1(C, D, A, B, Data[ 2] + LongInt($242070db), 17);
Round1(B, C, D, A, Data[ 3] + LongInt($c1bdceee), 22);
Round1(A, B, C, D, Data[ 4] + LongInt($f57c0faf), 7);
Round1(D, A, B, C, Data[ 5] + LongInt($4787c62a), 12);
Round1(C, D, A, B, Data[ 6] + LongInt($a8304613), 17);
Round1(B, C, D, A, Data[ 7] + LongInt($fd469501), 22);
Round1(A, B, C, D, Data[ 8] + LongInt($698098d8), 7);
Round1(D, A, B, C, Data[ 9] + LongInt($8b44f7af), 12);
Round1(C, D, A, B, Data[10] + LongInt($ffff5bb1), 17);
Round1(B, C, D, A, Data[11] + LongInt($895cd7be), 22);
Round1(A, B, C, D, Data[12] + LongInt($6b901122), 7);
Round1(D, A, B, C, Data[13] + LongInt($fd987193), 12);
Round1(C, D, A, B, Data[14] + LongInt($a679438e), 17);
Round1(B, C, D, A, Data[15] + LongInt($49b40821), 22); Round2(A, B, C, D, Data[ 1] + LongInt($f61e2562), 5);
Round2(D, A, B, C, Data[ 6] + LongInt($c040b340), 9);
Round2(C, D, A, B, Data[11] + LongInt($265e5a51), 14);
Round2(B, C, D, A, Data[ 0] + LongInt($e9b6c7aa), 20);
Round2(A, B, C, D, Data[ 5] + LongInt($d62f105d), 5);
Round2(D, A, B, C, Data[10] + LongInt($02441453), 9);
Round2(C, D, A, B, Data[15] + LongInt($d8a1e681), 14);
Round2(B, C, D, A, Data[ 4] + LongInt($e7d3fbc8), 20);
Round2(A, B, C, D, Data[ 9] + LongInt($21e1cde6), 5);
Round2(D, A, B, C, Data[14] + LongInt($c33707d6), 9);
Round2(C, D, A, B, Data[ 3] + LongInt($f4d50d87), 14);
Round2(B, C, D, A, Data[ 8] + LongInt($455a14ed), 20);
Round2(A, B, C, D, Data[13] + LongInt($a9e3e905), 5);
Round2(D, A, B, C, Data[ 2] + LongInt($fcefa3f8), 9);
Round2(C, D, A, B, Data[ 7] + LongInt($676f02d9), 14);
Round2(B, C, D, A, Data[12] + LongInt($8d2a4c8a), 20); Round3(A, B, C, D, Data[ 5] + LongInt($fffa3942), 4);
Round3(D, A, B, C, Data[ 8] + LongInt($8771f681), 11);
Round3(C, D, A, B, Data[11] + LongInt($6d9d6122), 16);
Round3(B, C, D, A, Data[14] + LongInt($fde5380c), 23);
Round3(A, B, C, D, Data[ 1] + LongInt($a4beea44), 4);
Round3(D, A, B, C, Data[ 4] + LongInt($4bdecfa9), 11);
Round3(C, D, A, B, Data[ 7] + LongInt($f6bb4b60), 16);
Round3(B, C, D, A, Data[10] + LongInt($bebfbc70), 23);
Round3(A, B, C, D, Data[13] + LongInt($289b7ec6), 4);
Round3(D, A, B, C, Data[ 0] + LongInt($eaa127fa), 11);
Round3(C, D, A, B, Data[ 3] + LongInt($d4ef3085), 16);
Round3(B, C, D, A, Data[ 6] + LongInt($04881d05), 23);
Round3(A, B, C, D, Data[ 9] + LongInt($d9d4d039), 4);
Round3(D, A, B, C, Data[12] + LongInt($e6db99e5), 11);
Round3(C, D, A, B, Data[15] + LongInt($1fa27cf8), 16);
Round3(B, C, D, A, Data[ 2] + LongInt($c4ac5665), 23); Round4(A, B, C, D, Data[ 0] + LongInt($f4292244), 6);
Round4(D, A, B, C, Data[ 7] + LongInt($432aff97), 10);
Round4(C, D, A, B, Data[14] + LongInt($ab9423a7), 15);
Round4(B, C, D, A, Data[ 5] + LongInt($fc93a039), 21);
Round4(A, B, C, D, Data[12] + LongInt($655b59c3), 6);
Round4(D, A, B, C, Data[ 3] + LongInt($8f0ccc92), 10);
Round4(C, D, A, B, Data[10] + LongInt($ffeff47d), 15);
Round4(B, C, D, A, Data[ 1] + LongInt($85845dd1), 21);
Round4(A, B, C, D, Data[ 8] + LongInt($6fa87e4f), 6);
Round4(D, A, B, C, Data[15] + LongInt($fe2ce6e0), 10);
Round4(C, D, A, B, Data[ 6] + LongInt($a3014314), 15);
Round4(B, C, D, A, Data[13] + LongInt($4e0811a1), 21);
Round4(A, B, C, D, Data[ 4] + LongInt($f7537e82), 6);
Round4(D, A, B, C, Data[11] + LongInt($bd3af235), 10);
Round4(C, D, A, B, Data[ 2] + LongInt($2ad7d2bb), 15);
Round4(B, C, D, A, Data[ 9] + LongInt($eb86d391), 21); Inc(Buf[0], A);
Inc(Buf[1], B);
Inc(Buf[2], C);
Inc(Buf[3], D);
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;const
MaxBufSize = 16384;
MD5Version = 102;
CopyRight : String = 'MD5 Message-Digest (c) 97-98 F. Piette V1.02';type
TMD5Context = record
State: array[0..3] of LongInt;
Count: array[0..1] of LongInt;
case Integer of
0: (BufChar: array[0..63] of Byte);
1: (BufLong: array[0..15] of LongInt);
end;
TMD5Digest = array[0..15] of Char; PMD5Buffer = ^TMD5Buffer;
TMD5Buffer = array[0..(MaxBufSize - 1)] of Char; TForm4 = class(TForm)
BitBtn1: TBitBtn;
Edit1: TEdit;
Edit2: TEdit;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; procedure MD5Init(var MD5Context: TMD5Context);
procedure MD5Update(var MD5Context: TMD5Context;
const Data;
Len: Integer);
procedure MD5Transform(var Buf: array of LongInt;
const Data: array of LongInt);
procedure MD5UpdateBuffer(var MD5Context: TMD5Context;
Buffer: Pointer;
BufSize: Integer);
procedure MD5Final(var Digest: TMD5Digest; var MD5Context: TMD5Context); function GetMD5(Buffer: Pointer; BufSize: Integer): string;
function StrMD5(Buffer : String): string;var
Form4: TForm4;implementation{$R *.dfm}{ MD5 initialization. Begins an MD5 operation, writing a new context. }
procedure MD5Init(var MD5Context: TMD5Context);
begin
FillChar(MD5Context, SizeOf(TMD5Context), #0);
with MD5Context do begin
{ Load magic initialization constants. }
State[0] := LongInt($67452301);
State[1] := LongInt($EFCDAB89);
State[2] := LongInt($98BADCFE);
State[3] := LongInt($10325476);
end
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ MD5 block update operation. Continues an MD5 message-digest operation, }
{ processing another message block, and updating the context. }
procedure MD5Update(
var MD5Context: TMD5Context; { Context }
const Data; { Input block }
Len: Integer); { Length of input block }
type
TByteArray = array[0..0] of Byte;
var
Index: Word;
T: LongInt;
begin
with MD5Context do begin
T := Count[0];
Inc(Count[0], LongInt(Len) shl 3);
if Count[0] < T then
Inc(Count[1]);
Inc(Count[1], Len shr 29);
T := (T shr 3) and $3F;
Index := 0;
if T <> 0 then begin
Index := T;
T := 64 - T;
if Len < T then begin
Move(Data, BufChar[Index], Len);
Exit;
end;
Move(Data, BufChar[Index], T);
MD5Transform(State, BufLong);
Dec(Len, T);
Index := T; { Wolfgang Klein, 05/06/99 }
end;
while Len >= 64 do begin
Move(TByteArray(Data)[Index], BufChar, 64);
MD5Transform(State, BufLong);
Inc(Index, 64);
Dec(Len, 64);
end;
Move(TByteArray(Data)[Index], BufChar, Len);
end
end;
procedure MD5UpdateBuffer(
var MD5Context: TMD5Context;
Buffer: Pointer;
BufSize: Integer);
var
BufTmp : PMD5Buffer;
BufPtr : PChar;
Bytes : Word;
begin
New(BufTmp);
BufPtr := Buffer;
try
repeat
if BufSize > MaxBufSize then
Bytes := MaxBufSize
else
Bytes := BufSize;
Move(BufPtr^, BufTmp^, Bytes);
Inc(BufPtr, Bytes);
Dec(BufSize, Bytes);
if Bytes > 0 then
MD5Update(MD5Context, BufTmp^, Bytes);
until Bytes < MaxBufSize;
finally
Dispose(BufTmp);
end;
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function GetMD5(Buffer: Pointer; BufSize: Integer): string;
var
I : Integer;
MD5Digest : TMD5Digest;
MD5Context : TMD5Context;
begin
for I := 0 to 15 do
Byte(MD5Digest[I]) := I + 1;
MD5Init(MD5Context);
MD5UpdateBuffer(MD5Context, Buffer, BufSize);
MD5Final(MD5Digest, MD5Context);
Result := '';
for I := 0 to 15 do
Result := Result + IntToHex(Byte(MD5Digest[I]), 2);
end;{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function StrMD5(Buffer : String): string;
begin
Result := GetMD5(@Buffer[1], Length(Buffer));
end;
procedure TForm4.BitBtn1Click(Sender: TObject);
var
vKey : string;
begin
vKey := '123456';
Edit2.Text :=StrMd5(Edit1.Text +vKey);
end;end.
举个例子:明文:1051006212012050200063281110510062120120502000632801105100621201205020006327911051006212012050200063278210510062120120502000632771
密钥:Picc37mu63ht38mw
客户那边MD5加密后得到密文是:4577c0b6b3364fb097ecd16293f157d6
不知道他那边是怎么算出这个密文的
* 生成签名
* @param method HTTP请求方法 "get" / "post" //参数1:请求方式的字符串值
* @param url_path CGI名字, //参数2:api的相对URL的字符串值
* @param params URL请求参数 //参数3:由request的请求名和值,构成的hashmap数据类型值
* @param secret 密钥 //参数4:用户网站的appkey的字符串值
* @return 签名值 //返回值,对请求值进行hmac-sha1加密算法的计算返回值。
* @throws OpensnsException 不支持指定编码以及不支持指定的加密方法时抛出异常。
*/
private String makeSign(String method, String url_path, HashMap<String, String> params, String secret) throws OpenApiException {
String sig = "";
try {
Mac mac = Mac.getInstance("HmacSHA1"); //1 得到一个是hmacsha1的mac对象
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(charset), mac.getAlgorithm()); //2 创建secretKey对象
mac.init(secretKey); //3 用secretkey对象,初始化mac对象。
String mk = makeSource(method, url_path, params); //4 输入method、url_path,parms参数,得到源名码
System.out.println(mk);
byte[] hash = mac.doFinal(mk.getBytes(charset)); //5 用mac对源名码加密,得到byte数组
sig = new String(Base64Coder.encode(hash)); //6 encode编码,然后转化为string输出值
// sig = encodeUrl(sig);
} catch (Exception e) {
throw new OpenApiException(OpenApiException.MAKE_SIGNATURE_ERROR, e);
}
return sig;
}输入post/get 的string值、相对api的url string值、hashmap方式的request值、appkey的string值,经过hmac算法,得到计算的密码值。
源名码=源明码 上面是拍拍网的api访问的java的demo,有否delphi版的算法例子。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons,
IdHashMessageDigest, HTTPApp, IdHMACSHA1, IdCoderMIME, DateUtils;type
TForm3 = class(TForm)
BitBtn1: TBitBtn;
Memo1: TMemo;
procedure BitBtn1Click(Sender: TObject);
procedure Memo1Change(Sender: TObject); private
{ Private declarations }
function Base64Encode(const Input: TBytes): string;
function EncryptHMACSha1(Input,AKey:AnsiString): TBytes;
public
{ Public declarations }
end;var
Form3: TForm3;implementation{$R *.dfm}procedure TForm3.BitBtn1Click(Sender: TObject);var sHttpMethod: AnsiString; sRequestURL: AnsiString; sRequestParameter: AnsiString; sAppSecret: AnsiString; sSignatureBaseString: AnsiString; sMD5: TIdHashMessageDigest5;begin sMD5 := TIdHashMessageDigest5.Create; sAppSecret := '4b50186d12987f405b17bc38c9b99b0a&'; //密钥 sHttpMethod := 'GET'; sRequestURL := 'http://open.t.qq.com/cgi-bin/request_token'; sRequestParameter := 'oauth_callback=null&oauth_consumer_key=8*******9&oauth_nonce='; sRequestParameter := sRequestParameter + sMD5.HashStringAsHex(IntToStr(Random(10000)), nil); //随机32位数 sRequestParameter := sRequestParameter + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp='; sRequestParameter := sRequestParameter + FloatToStr(DateTimeToUnix(Now()-1/3)); //时间戳 sRequestParameter := sRequestParameter + '&oauth_version=1.0'; sSignatureBaseString := HTTPEncode(sHttpMethod) + '&' + HTTPEncode(sRequestURL) + '&' + HTTPEncode(sRequestParameter); Memo1.Text := Base64Encode(EncryptHMACSha1(sSignatureBaseString, sAppSecret)); sMD5.Free;end;//两个函数 Base64Encode,EncryptHMACSha1;
//HMACSha1算法
function TForm3.EncryptHMACSha1(Input,AKey:AnsiString): TBytes;
var Key: TBytes;
begin with TIdHMACSHA1.Create do
try
Key := BytesOf(AKey);
Result := HashValue(BytesOf(Input)); finally
Free;
end;end;procedure TForm3.Memo1Change(Sender: TObject);
beginend;//Base64编码function TForm3.Base64Encode(const Input: TBytes): string;
begin Result := TIdEncoderMIME.EncodeBytes(Input);
end;
end.
我将MD5算法,在delphi7.0下编译,将某字符串加密,得到的32位的密文是A,将同样的程序,放到delphi xe3下编译,将同样的字符串加密,得到的是字符串B。
同样的md5算法,因为编译的语言不同,得到的结果也是不同的。
你不知道你D7是ANSI字符,XE3是Unicode编码吗?这是基础知识,请好好补习一下。你在sql server中的varchar和nvarchar编码后的值也是不一样的。
比如很多网站的后台数据api的rest方式,就是用md5和hmac-sha1算法的。
多看java代码,很清楚的。
delphi低版本的关于网络的算法,都要依靠第三方控件或代码,
有些甚至要通过调用dll来处理。
这就是delphi的特点。