Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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'

the encrypted password is: ì?Âp?

the decrypted passowrd is: JìEJùE

It should decrypt as JOEJOE obviously. The kicker, if I build the code and send the exe to another user it decrypts fine. 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.

Even when we filter by the same name running the same code. Maybe a database issue?

like image 714
Trevor Avatar asked Feb 22 '23 03:02

Trevor


1 Answers

Try doing a port from Ansi to Unicode 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;

My best wild guess is that the expected results are different because of the difference between AnsiChar and 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.

like image 118
Warren P Avatar answered May 10 '23 06:05

Warren P