Delphi 2010 DCPCrypt2 Sha256 哈希不等于 PHP 哈希

发布于 2024-12-27 21:39:42 字数 1842 浏览 2 评论 0原文

我正在编写一个 Delphi 2010 应用程序,它将连接到 PHP Web 应用程序以获取特定数据。服务器上的网页接受 3 个 GET 参数:

  1. cityid // Web 应用程序返回特定数据
  2. 盐所需的值 // 当前时间戳:年月日时分秒毫秒
  3. 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:

  1. cityid // Value needed for web application to return specific data
  2. salt // Current time stamp: Year Month Date Hour Minute Second Millisecond
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文