德尔福5至2010
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否知道,
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:
string
withAnsiString
. This should give you the same results as in D5, of course without Unicode support(It could very well be that your code can't be used with Unicode anyways, due to the 255 consts = ISO8859?)
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.
最小端口,一行更改:
请注意,unicode“String”中的任何 unicode 内容都将丢失,但您之前使用的任何 ANSI(AZ、1、3、4,你知道)代码点,例如“Hello”,应该像以前一样工作。由于这是 CRC32 算法,因此它也可以轻松地对字符串的 UTF8 编码执行 CRC32。
Minimal port, one line change:
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.