Why does my encryption not work after upgrading to Delphi 2007?

I recently took some code from Delphi 2007 and upgraded it to Delphi 2009. This may or may not be relevant.

But when I run the code on my computer, decrypting the password does not decrypt 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 user code is TD and my password is "JOEJOE"

encrypted password: ì? Âp?

decrypted passowrd: JìEJùE

It should decrypt as JOEJOE, obviously. Kicker, if I create the code and send the exe to another user, it decrypts everything. This makes me believe that this is not something wrong with the code, but rather some kind of anomaly with my computer. What could it be?


You can refuse this because it is probably not related. I only mention this because it is a different case when something works fine on one computer, but not on another.

But there is one case when trying to set a filter

 TableUser2.Filter := FilterString; 

It works fine for me, but another user gets an error.

TableUser2: Error 3106: An unsupported statement was found in a write filter expression.

Even when we filter the same name with the same code. Perhaps a database problem?

+4
source share
3 answers

Try making a port from Ansi to Unicode as follows:

 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 differ from each other due to the difference between AnsiChar and UnicodeChar. If you manage to create some invalid code points that cannot be stored in a non-Unicode data field, you may get some funny errors.

+7
source

Your problem is that Delphi 2009 uses Unicode, not ANSI for its text. This was a significant change that required significant migration efforts. Not only do you need to deal with coding problems in your code, you will need to update any third-party components that you use.

You can return to the previous behavior for this particular function as follows:

 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. ANSI encoded string that previous versions of Delphi called AnsiString . Similarly, Chr() generates a 16 character, but WideChar , but you want AnsiChar , an 8-bit ANSI character type.


However, there will certainly be a number of other problems to solve. I suggest you read the technical paper by Marco Canty in Delphi and Unicode . You really should go into the details described in detail in this article before continuing with the port.

+1
source

The first thing I would like to do is put some records of the inputs / outputs of your functions.

It really seems that the value in TableUser.FieldByName ("Password") does not convey what you expect in both cases.

Another thing that I would like to pay attention to is the sorting of the database used with both machines. I assume that the underlying database is different between your two test cases; or at least the connection string information has different meanings for sorting. This, of course, may discard decryption.

0
source

Source: https://habr.com/ru/post/1386354/


All Articles