简体   繁体   English

升级到Delphi 2007之后,为什么我的加密不起作用?

[英]Why doesn't my encryption work after I upgrade beyond Delphi 2007?

I recently took some code from Delphi 2007 and upgraded it to Delphi 2009. That may or may not be relevant. 我最近从Delphi 2007中获取了一些代码并将其升级到Delphi 2009.这可能与也可能不相关。

But when I run the code on my computer the decryption of the password is not decrypting correctly. 但是,当我在计算机上运行代码时,密码的解密不能正确解密。 Here is the code. 这是代码。

Seed := GenerateIntFromString('usercode');

// Check if a password already exists
if TableUser.FieldByName('PASSWORD').AsString <> '' then
begin
    EncodedPassword := TableUser.FieldByName('PASSWORD').AsString;
    DecodedPassword := EncryptDecrypt(EncodedPassword, Seed);
//etc.. And the function

function TLogonForm.EncryptDecrypt(Input: string; Seed: integer) : string;
var
i : integer;
Output : string;
begin
    RANDSEED := Seed;
    Output := '';
    for i := 1 to Length(Input) do
        Output := Output + Chr(Ord(Input[i]) XOR (RANDOM(254) + 1));
    Result := Output;
end;

So if my usercode is TD and my password is 'JOEJOE' 所以如果我的用户代码是TD而我的密码是'JOEJOE'

the encrypted password is: ì?Âp? 加密密码是:ì?Âp?

the decrypted passowrd is: JìEJùE 解密的passowrd是:JìEJùE

It should decrypt as JOEJOE obviously. 它应该明显解密为JOEJOE。 The kicker, if I build the code and send the exe to another user it decrypts fine. 踢球者,如果我构建代码并将exe发送给另一个用户,它就会解密。 This leads me to believe its not something wrong with the code rather some anomaly with my computer. 这让我相信代码并没有什么问题,而是我的电脑有些异常。 What could it be? 会是什么呢?


You can disgard this because its probably not related. 你可以对此表示不满,因为它可能与此无关。 I only mention it because it's another case where something works fine on one computer but not the other. 我只提到它,因为它是另一种情况,一台计算机上的东西工作正常而另一台计算机没有。

But there is also one case where when trying to set a filter 但是也有一种情况是在尝试设置过滤器时

TableUser2.Filter := FilterString;

it works fine for me, but the other user gets an error. 它适用于我,但其他用户收到错误。

TableUser2: Error 3106: Unsupported operator found in a record filter expression. TableUser2:错误3106:在记录筛选器表达式中找到不支持的运算符。

Even when we filter by the same name running the same code. 即使我们使用相同的名称过滤相同的代码。 Maybe a database issue? 也许是数据库问题?

Try doing a port from Ansi to Unicode like this: 尝试从Ansi到Unicode的端口,如下所示:

function TLogonForm.EncryptDecrypt(Input: AnsiString; Seed: integer) : AnsiString;
var
i : integer;
Output : AnsiString;
begin
    RANDSEED := Seed;
    Output := '';
    for i := 1 to Length(Input) do
        Output := Output + AnsiChar(Ord(Input[i]) XOR (RANDOM(254) + 1));
    Result := Output;
end;

My best wild guess is that the expected results are different because of the difference between AnsiChar and UnicodeChar. 我最好的猜测是,由于AnsiChar和UnicodeChar之间的区别,预期的结果是不同的。 If you managed to generate some invalid codepoints that can't be stored in your DB's non-unicode data field, you might have some fun errors. 如果您设法生成一些无法存储在数据库的非unicode数据字段中的无效代码点,则可能会出现一些有趣的错误。

Your issue is that Delphi 2009 uses Unicode rather than ANSI for its text. 您的问题是Delphi 2009的文本使用Unicode而不是ANSI。 This was a major breaking change which requires significant porting effort. 这是一个重大的突破性变化,需要大量的移植工作。 Not only do you need to deal with encoding issues in your code, you will need to upgrade any 3rd party components that you use. 您不仅需要处理代码中的编码问题,还需要升级您使用的任何第三方组件。

You can revert to the previous behaviour for this particular function like this: 您可以恢复此特定函数的先前行为,如下所示:

function TLogonForm.EncryptDecrypt(Input: AnsiString; Seed: integer): AnsiString;
var
i : integer;
Output : AnsiString;
begin
    RANDSEED := Seed;
    Output := '';
    for i := 1 to Length(Input) do
        Output := Output + AnsiChar(Ord(Input[i]) XOR (RANDOM(254) + 1));
    Result := Output;
end;

In Delphi 2009 the string data type is a UTF-16 encoded string. 在Delphi 2009中, string数据类型是UTF-16编码的字符串。 The ANSI encoded string that previous versions of Delphi is named AnsiString . 以前版本的Delphi命名为AnsiString的ANSI编码字符串。 Similarly Chr() generates a 16 but WideChar character but you want an AnsiChar , the 8 bit ANSI character type. 类似地, Chr()生成一个16但是WideChar字符,但是你想要一个8位ANSI字符类型的AnsiChar


However, there will surely be a number of other issues to tackle. 但是,肯定还有许多其他问题需要解决。 I suggest you read Marco Cantù's whitepaper on Delphi and Unicode . 我建议你阅读MarcoCantù关于Delphi和Unicode的白皮书。 You really should get on top of the issues detailed in this paper before proceeding any further with the port. 在继续进一步使用端口之前,您真的应该掌握本文中详述的问题。

The first thing I would do is put some logging around the inputs / outputs of your functions. 我要做的第一件事是在函数的输入/输出周围进行一些记录。

It really sounds like maybe the value in TableUser.FieldByName("Password") isn't passing what you expect in both cases. 听起来似乎TableUser.FieldByName(“Password”)中的值可能没有传递您在两种情况下的预期。

Another thing I would pay attention to is the database collation in use with both machines. 我要注意的另一件事是与两台机器一起使用的数据库整理。 I'm assuming that the underlying database is different between your two test cases; 我假设你的两个测试用例之间的底层数据库是不同的; or, at the very least, the connection string info has different values for collation. 或者,至少,连接字符串信息具有不同的整理值。 This could certainly throw off the decryption. 这肯定会摒弃解密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么快捷方式在我的 Delphi 程序中不起作用? - Why the shortcut doesn't work in my Delphi program? Delphi:PopupMenu在我的组件中不起作用 - Delphi: PopupMenu doesn't work in my component 当字符值存储在Delphi XE2 ShortString中时,为什么我的字符值求反算法不起作用? - Why doesn't my algorithm for inverting character values work when they're stored in a Delphi XE2 ShortString? 从Delphi 2007升级到Delphi 2010? - Upgrade from Delphi 2007 to Delphi 2010? 为什么我的光标没有变成Delphi中的FindDialog中的沙漏? - Why doesn't my cursor change to an Hourglass in my FindDialog in Delphi? Delphi 7-为什么VK_RETURN对我不起作用? - Delphi 7 - Why doesn't VK_RETURN work for me? 为什么不通过一致地编辑注册表工作来在Delphi中安装包? - Why doesn't installing packages in Delphi by editing the registry work consistently? 为什么“ParentBackground”在Windows Classic主题中不起作用? (DELPHI) - Why “ParentBackground” doesn't work at Windows Classic theme ? (Delphi) Delphi 2007,Indy10。为什么我不能键入TidBytes缓冲区? - Delphi 2007, Indy 10. Why can't I typecast a TidBytes buffer? 如何修改基于Char的加密代码以与Unicode Delphi一起使用? - How do I modify my Char based encryption code to work with Unicode Delphi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM