德尔福5至2010

发布于 2024-08-25 16:08:49 字数 889 浏览 15 评论 0原文

我在 Delphi 5 和 2010 中使用了相同的函数( OneWayEncrypt(edit1.Text) )。
为什么结果不同? (或者我怎样才能从 Delphi 2010 中得到相同的结果?)

uses Sysutils, Windows, Dialogs, classes;

function OneWayEncrypt(AStr: string): string;
PROCEDURE CalcCRC32 (p:  pointer; ByteCount:  DWORD; VAR CRCvalue:  DWORD);

implementation

const 
  table:  ARRAY[0..255] OF DWORD = 
  (
    //table consts are here
  );

PROCEDURE CalcCRC32(p: pointer; ByteCount: DWORD; VAR CRCvalue: DWORD);
VAR
  i: DWORD;
  q: ^Byte;
BEGIN
  q := p;
  FOR i := 0 TO ByteCount - 1 DO
  BEGIN
    CRCvalue := (CRCvalue SHR 8) XOR table[q^ XOR (CRCvalue AND $000000FF)];
    INC(q);
  END
END;

function OneWayEncrypt(AStr: string): string;
var
  dwCrc: DWORD;
  s: string;
begin
  dwCrc := $FFFFFFFF; 
  s := 'X' + AStr + '7F';
  CalcCRC32(Addr(s[1]), Length(s), dwCrc);
  result := IntToHex(dwCrc, 8);
end;

I used same function ( OneWayEncrypt(edit1.Text) ) in Delphi 5 and 2010.
Why the results are different? (Or how can I give the same results from Delphi 2010?)

uses Sysutils, Windows, Dialogs, classes;

function OneWayEncrypt(AStr: string): string;
PROCEDURE CalcCRC32 (p:  pointer; ByteCount:  DWORD; VAR CRCvalue:  DWORD);

implementation

const 
  table:  ARRAY[0..255] OF DWORD = 
  (
    //table consts are here
  );

PROCEDURE CalcCRC32(p: pointer; ByteCount: DWORD; VAR CRCvalue: DWORD);
VAR
  i: DWORD;
  q: ^Byte;
BEGIN
  q := p;
  FOR i := 0 TO ByteCount - 1 DO
  BEGIN
    CRCvalue := (CRCvalue SHR 8) XOR table[q^ XOR (CRCvalue AND $000000FF)];
    INC(q);
  END
END;

function OneWayEncrypt(AStr: string): string;
var
  dwCrc: DWORD;
  s: string;
begin
  dwCrc := $FFFFFFFF; 
  s := 'X' + AStr + '7F';
  CalcCRC32(Addr(s[1]), Length(s), dwCrc);
  result := IntToHex(dwCrc, 8);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你好,陌生人 2024-09-01 16:08:49

您是否知道,string 在 D2010 中指的是 Unicode 字符串,而在 版本中指的是 AnsiString。 D2009?这应该是你问题的根源。

所以你有两个选择:

  • 你可以用 AnsiString 替换所有出现的 string 。这应该会给你与 D5 相同的结果,当然没有 Unicode 支持
  • 你可以重构你的代码。我想指针——“黑客攻击”是这里的关键部分。但我必须承认,我没有花时间完全理解代码;-)
    (很可能你的代码无论如何都不能与 Unicode 一起使用,因为 255 个 consts = ISO8859?)

Are you aware that string refers to a Unicode string in D2010, while it refers to AnsiString in versions < D2009? That should be the source of your problem.

So you have two choices:

  • You could replace all appearances of string with AnsiString. This should give you the same results as in D5, of course without Unicode support
  • You could refactor your code. I guess that the pointer-"hacking" is the crucial part here. But I have to admit, I didn't take the time to fully understand the code ;-)
    (It could very well be that your code can't be used with Unicode anyways, due to the 255 consts = ISO8859?)
江心雾 2024-09-01 16:08:49

D2010(和 D2009)使用 Unicode 字符串(宽字符串),因此字符大小不同(字节)。尝试将所有字符串引用切换为 AnsiString。

D2010 (and D2009) use Unicode strings (widestrings), so the character size is different (bytes). Try switching all references of string to AnsiString.

风铃鹿 2024-09-01 16:08:49

最小端口,一行更改:

  // old code:
  CalcCRC32(Addr(s[1]), Length(s), dwCrc);

  // delphi 2010 code:
  CalcCRC32( PAnsiChar(AnsiString(s)), Length(s), dwCrc);

请注意,unicode“String”中的任何 unicode 内容都将丢失,但您之前使用的任何 ANSI(AZ、1、3、4,你知道)代码点,例如“Hello”,应该像以前一样工作。由于这是 CRC32 算法,因此它也可以轻松地对字符串的 UTF8 编码执行 CRC32。

Minimal port, one line change:

  // old code:
  CalcCRC32(Addr(s[1]), Length(s), dwCrc);

  // delphi 2010 code:
  CalcCRC32( PAnsiChar(AnsiString(s)), Length(s), dwCrc);

Please be aware that any unicode content in the unicode "String" will be lost, but any ANSI (A-Z, 1,3,4, you know) codepoints you used before, for example "Hello", should work just like before. Since this is a CRC32 algorithm, it could do a CRC32 on a UTF8 encoding of the string too, easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文