Delphi 2010 DCPCrypt2 Sha256 哈希不等于 PHP 哈希
我正在编写一个 Delphi 2010 应用程序,它将连接到 PHP Web 应用程序以获取特定数据。服务器上的网页接受 3 个 GET 参数:
- cityid // Web 应用程序返回特定数据
- 盐所需的值 // 当前时间戳:年月日时分秒毫秒
- token // SHA-256 哈希使用安全密钥进行保护
问题在于令牌。它是在 Delphi 中使用以下函数生成的:
function DigestToStr(Digest: array of byte): string;
var
i: Integer;
begin
Result := '';
for i := 0 to Length(Digest) do
Result := Result + LowerCase(IntToHex(Digest[i], 2));
end;
function GetStringHash(Source: string): string;
var
Hash: TDCP_sha256;
Digest: array[0..31] of Byte;
begin
Hash := TDCP_sha256.Create(nil);
Hash.Init;
Hash.UpdateStr(Source);
Hash.Final(Digest);
Hash.Free;
Result := DigestToStr(Digest);
end;
procedure TMain.Button3Click(Sendere: TObject);
var
TheToken,Sha256:string;
begin
salt:=FormatDateTime('yyyymmddhhnnsszzz',Now);
TheToken:=salt+'THE_SECRET_KEY'+MCity;
Sha256:=GetStringHash(TheToken);
end;
Delphi 为 token 提供此字符串: ec4338bb3e7819a1148ffc01713f6d164f4f5940c5758d896b0a3d53f30f8c24f0
PHP 的 hash 函数给出完全不同的字符串:
<?php
hash('sha256','20120119115458592THE_SECRET_KEY1');
?>
8b4cff3b937bbcfae8fa7a67ce407b6185451a52e994ea2ef92a760676717490
我试图在网上寻找类似的问题,但发现只讨论了对称加密/描述问题,我不需要令牌的对称加密
更新 我已经想通了!!!问题出在错误的变量类型上!
我使用了string,当我用AnsiString替换它时,它成功了!
function GetStringHash(Source: AnsiString): string;
var
...
除此之外,我
for i := 0 to Length(Digest) do
替换
for i := 0 to Length(Digest)-1 do
在 DigestToStr 函数中
为 抱歉造成干扰,但我希望这会对某人有所帮助!
I'm writing a Delphi 2010 application that will connect to the PHP Web application to get specific data. Web page on the server accepts 3 GET parameters:
- cityid // Value needed for web application to return specific data
- salt // Current time stamp: Year Month Date Hour Minute Second Millisecond
- token // SHA-256 hash with security key for protection
The problem is with token. It's generated in Delphi using these functions:
function DigestToStr(Digest: array of byte): string;
var
i: Integer;
begin
Result := '';
for i := 0 to Length(Digest) do
Result := Result + LowerCase(IntToHex(Digest[i], 2));
end;
function GetStringHash(Source: string): string;
var
Hash: TDCP_sha256;
Digest: array[0..31] of Byte;
begin
Hash := TDCP_sha256.Create(nil);
Hash.Init;
Hash.UpdateStr(Source);
Hash.Final(Digest);
Hash.Free;
Result := DigestToStr(Digest);
end;
procedure TMain.Button3Click(Sendere: TObject);
var
TheToken,Sha256:string;
begin
salt:=FormatDateTime('yyyymmddhhnnsszzz',Now);
TheToken:=salt+'THE_SECRET_KEY'+MCity;
Sha256:=GetStringHash(TheToken);
end;
Delphi gives this string for a token: ec4338bb3e7819a1148ffc01713f6d164f4f5940c5758d896b0a3d53f30f8c24f0
PHP's hash function gives totally different string:
<?php
hash('sha256','20120119115458592THE_SECRET_KEY1');
?>
8b4cff3b937bbcfae8fa7a67ce407b6185451a52e994ea2ef92a760676717490
I tried to look for similar questions on the Web, but found only symmetric encryption/description problems discussed, I don't need symmetric encryption for the token
UPDATE
I've figured out!!! The problem was in the wrong variable type!
I used string, and when I replaced it with AnsiString it worked out!
function GetStringHash(Source: AnsiString): string;
var
...
Besides that I replaced
for i := 0 to Length(Digest) do
with
for i := 0 to Length(Digest)-1 do
in DigestToStr function
Sorry for causing disturbance, but I hope this will help somebody!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论