SRL Archive - Bugs

rc2_encrypt or rc2_decrypt bugs


The Mayor

2015-01-29 05:21:45

This was originally posted as a bug in the SRL-6 section as Rafiki can encrypt your army. The problem is when you decrypt the army string, only 1 player will show up even if you had more than 1 player in the army. The reason it does this is because any text longer than 189 characters will get cut off. I'm not sure if this is during encryption or decryption. It's not lape as I get the same result using PS.

Here is an example, you can see the last few characters of the string get lost:

program template;

var
data: String;

begin
data := '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 Opps it broke!';
writeLn('Before: ' + data);
writeLn(length(data));

rc2_encrypt('pw', htSHA512, data);
writeLn('After encrypt: ' + data);

rc2_decrypt('pw', htSHA512, data);
writeLn('After decrypt: ' + data);
writeLn(length(data));
end.



Before: 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 Opps it broke!
194
After encrypt: tFMIdlsx26/NvmiwBsodE+tueb62AhWv5R1ChG4VqN3EPZ9yd9/aME39Wk7LFackXhgL0zfj+Q5NXVPM2mfQ35QbWW/Y8YBUkKaq3/miHKw+rk6zVCg+I3OpGKx/4Bs9hXulMGQCleTcWCEcsJTbdBmY6aDLR341UIBka4V1Z53ghlaBPzWzpME50JmOCPPEYa0AB5OSHt9petF1PWOxiD4l2+AucWfFbjQ5EhhnkgcviQv9q+ZFh1fHMEGURWu
After decrypt: 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 Opps it b
189

Olly

2015-01-29 15:51:31

Just use htSHA256

Brandon

2015-01-29 18:38:07

@The Mayor; TEMPORARY SOLUTION:


Since we know that Base64 does not use all ASCII characters, we can split the string, encrypt the chunk and prepend each chunk with '~' (tilde).. For decryption, we split with `~` (tilde), decrypt each chunk and join the chunks.. as shown below:


Function generateData(l: Integer): String;
var
I: Integer;
begin
for I := 0 to l - 4 do
Result := Result + Chr(RandomRange(65, 127));
Result := Result + 'EOF';
end;

Function generateKey(l: Integer): String;
var
I: Integer;
begin
SetLength(Result, l);
for I := 1 To l do
Result[I] := Chr(Random(256));
end;



Function Split(Data: String; Chunk: Integer): TStringArray;
var
I, J, L: Integer;
Begin
J := 0;
L := Length(Data);
SetLength(Result, ((L - 1) div Chunk) + 1);

For I := 1 To L - Chunk + 1 Do
Begin
Result[J] := Copy(Data, I, Chunk);
I := I + Chunk - 1;
J := J + 1;
End;

If (L mod Chunk <> 0) Then
Result[J] := Copy(Data, L - L mod Chunk + 1, L mod Chunk);
End;

Procedure RC2Encrypt(Key: String; HashType: THashType; var Data: String);
var
I: Integer;
Arr: TStringArray;
Res: String;
Begin
Arr := Split(Data, 189);
For I := 0 To Length(Arr) - 1 Do
Begin
rc2_encrypt(Key, HashType, Arr[I]);
Res := Res + '~' + Arr[I];
End;
Data := Res;
End;

Procedure RC2Decrypt(Key: String; HashType: THashType; var Data: String);
var
I, L: Integer;
T: TStringList;
Res, Arr: String;
Begin
{$IFDEF LAPE}
T.Init();
SplitRegExpr('~', Data, T);
L := T.getCount() - 1;
{$ELSE}
T := TStringList.Create();
SplitRegExpr('~', Data, T);
L := T.Count - 1;
{$ENDIF}


For I := 1 To L Do
Begin
{$IFDEF LAPE}
Arr := T.getStrings(I);
{$ELSE}
Arr := T.Strings[I];
{$ENDIF}

rc2_decrypt(Key, HashType, Arr);
Res := Res + Arr;
End;
T.Free();
Data := Res;
End;




var
key, data: String;
begin
key := generateKey(64);
data := generateData(200); //200 / 8 = 25..

writeln(Length(Data));

writeln('Before Encryption: ' + data);
writeln('');
writeln('');


rc2encrypt(key, htMD5, data);
rc2decrypt(key, htMD5, data);


writeLn('After Decryption: ' + data);
writeLn(length(data));
end.


RESULT:


Compiled successfully in 15 ms.
Length: 200

Before Encryption: kfkMD\SoR`J{D]^m\GhKR\hsOaKG`MDfOuMxfFTAM[eRu{dkY\gFBOD_lx`lxMjMrjCP|KpzSBFHMBs~v|~f{IEXvBaKndct^L{Qpz\NrBy~ByUAFNrMr}|JZvQsRBdjR}TlfyCruUyvSka~zJIHxEQpSxOYUYj{h|WVNNk_X~StSW`h{^XdhZDVNqiBgYQySdSw`EOF


After Decryption: kfkMD\SoR`J{D]^m\GhKR\hsOaKG`MDfOuMxfFTAM[eRu{dkY\gFBOD_lx`lxMjMrjCP|KpzSBFHMBs~v|~f{IEXvBaKndct^L{Qpz\NrBy~ByUAFNrMr}|JZvQsRBdjR}TlfyCruUyvSka~zJIHxEQpSxOYUYj{h|WVNNk_X~StSW`h{^XdhZDVNqiBgYQySdSw`EOF

Length: 200
Successfully executed.

Olly

2015-01-29 20:42:44

iirc Bart originally had it in 512 blocks to encrypt/decrypt.. I guess this is why.

Dgby714

2015-01-29 21:36:54

Just going to note, the actual code that does the encryption and decryption is a unit called DCPCrypt, and is not something the Simba devs wrote.

Also, Simba only exposes the DCPCrypt methods to the script, Simba doesn't do any padding or anything, that is up to the scripter.

I'm guessing the 189 limit is a bug/limitation of DCPCrypt, someone should look into it.

Simba's Crypto.inc: https://github.com/MerlijnWajer/Simba/blob/master/Units/MMLAddon/PSInc/Wrappers/crypto.inc
DCPCrypt Files: https://github.com/MerlijnWajer/Simba/tree/master/Units/Misc/DCPCrypt
DCPCrypt Info: http://wiki.lazarus.freepascal.org/DCPcrypt

Brandon

2015-01-29 22:38:46

From: Dgby714
Just going to note, the actual code that does the encryption and decryption is a unit called DCPCrypt, and is not something the Simba devs wrote.

Also, Simba only exposes the DCPCrypt methods to the script, Simba doesn't do any padding or anything, that is up to the scripter.

I'm guessing the 189 limit is a bug/limitation of DCPCrypt, someone should look into it.

Simba's Crypto.inc: https://github.com/MerlijnWajer/Simba/blob/master/Units/MMLAddon/PSInc/Wrappers/crypto.inc
DCPCrypt Files: https://github.com/MerlijnWajer/Simba/tree/master/Units/Misc/DCPCrypt
DCPCrypt Info: http://wiki.lazarus.freepascal.org/DCPcrypt




I don't know what's wrong with the current Simba but I recompiled Simba myself and it works without changing anything at all.. That means something is wrong with the current released exe.. All I did was a git submodule update and compiled it..

Here's the working exe (nothing in the source changed): http://www.mediafire.com/download/jtuiltql3hw3ycp/Simba.zip Can just replace Simba.exe with it.

Virus Total Scan: https://www.virustotal.com/en/url/480b935c4a7913684e3196c4965430401568850a49cab228d261937482a03efd/analysis/1422589214/

Virus Total Downloaded Scan: https://www.virustotal.com/en/file/8101d2eb634653e4f7366051eb6047c678ce058250b920f8baefaa922782dc38/analysis/1422589218/

Someone needs to recompile the distributed Simba for the mass public..

Results: http://i.imgur.com/ssavgEn.png

Dgby714

2015-01-29 22:58:51

@Wizzup?;

Could be an issue with the buildbot, the currently mass distributed Simba packages are compile in Linux.
Guessing it's an issue with FPC, Lazarus, or DCPCrypt in the build environment.

Dgby714

2015-01-30 01:19:53

@Wizzup?;

Editing posts doesn't actually mention..

The Mayor

2015-01-30 02:33:01

From: Brandon
I don't know what's wrong with the current Simba but I recompiled Simba myself and it works without changing anything at all.. That means something is wrong with the current released exe.. All I did was a git submodule update and compiled it..

Here's the working exe (nothing in the source changed): http://www.mediafire.com/download/jtuiltql3hw3ycp/Simba.zip Can just replace Simba.exe with it.

Virus Total Scan: https://www.virustotal.com/en/url/480b935c4a7913684e3196c4965430401568850a49cab228d261937482a03efd/analysis/1422589214/

Virus Total Downloaded Scan: https://www.virustotal.com/en/file/8101d2eb634653e4f7366051eb6047c678ce058250b920f8baefaa922782dc38/analysis/1422589218/

Someone needs to recompile the distributed Simba for the mass public..

Results: http://i.imgur.com/ssavgEn.png


Confirmed that this build works fine.

Wizzup?

2015-02-14 11:20:19

@Dgby714; it depends on the compiler version then I presume. Have people tried it with the latest master? That is using a much newer compiler.