Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: strange string behaviour


This question is answered.


Permlink Replies: 4 - Last Post: Sep 6, 2015 1:33 AM Last Post By: Rolf Fankhauser
Rolf Fankhauser

Posts: 22
Registered: 9/3/01
strange string behaviour  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 4, 2015 11:44 AM
Hi all,

I made a test program to encrypt and decrypt strings with xtea algorithm.
To test conversion I write some dates and their encryption to a memo (see code below).
The strange thing is that sCode is not displayed in the memo lines.
If I reverse the order of the strings (line := sCode + #9#9 + sDateTime + ' ' + sTest;) then all is fine.
So, it seems that something strange happens on concat !!!!
I gess that something goes wrong in HexToStr where a copy of the input string is read and truncated.
When I simplify the code without the encryption and decryption like:
sCode := StrToHex(sDateTime);
sTest := HexToStr(sCode);


output is also ok. Really very strange!!

Thanks, Rolf


procedure TForm1.Button2Click(Sender: TObject);
var
   DateTime: TDatetime;
   i: integer;
   sDateTime, sCode, sTest, line: ShortString;
begin
   DateTime := Date;
   for i:=1 to 100 do
    begin
     DateTime := DateTime + 1.0;
     sDateTime := DateTimeToStr(DateTime);
     sCode := StrToHex(XTeaCryptStr(sDateTime, password));
     sTest := XTeaDecryptStr(HexToStr(sCode), password);
     line := sDateTime + ' ' + sTest + #9#9 + sCode;
     Memo1.Lines.Append(line);
    end;
end;


unit xtea;
{-------------------------------------------------------------------------
(c) 2002 Irnis Haliullin
www.irnis.net
irnis@irnis.net
--------------------------------------------------------------------------}
interface
 
type
  TTeaMsgBlock = array[0..1] of LongWord;
  TTeaKeyBlock = array[0..3] of LongWord;
 
procedure XTeaCrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
procedure XTeaDecrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
function XTeaCryptStr(const Msg, Pwd: ShortString): ShortString;
function XTeaDecryptStr(const Msg, Pwd: ShortString): ShortString;
 
implementation
 
uses SysUtils;
{ ========================================================================= }
const
  DELTA = $9e3779b9;
  N = 32;
 
procedure XTeaCrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
var
  I: LongWord;
  S: Int64;
begin
  S := 0;
  for I := 0 to N - 1 do begin
    Inc(V[0], (V[1] shl 4 xor V[1] shr 5) + (V[1] xor S) + K[S and 3]);
    Inc(S, DELTA);
    Inc(V[1], (V[0] shl 4 xor V[0] shr 5) + (V[0] xor S) + K[S shr 11 and 3]);
  end;
end;
 
procedure XTeaDecrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
var
  I: LongWord;
  S: Int64;
begin
  S := DELTA * Int64(N);
  for I := 0 to N - 1 do begin
    Dec(V[1], (V[0] shl 4 xor V[0] shr 5) + (V[0] xor S) + K[S shr 11 and 3]);
    Dec(S, DELTA);
    Dec(V[0], (V[1] shl 4 xor V[1] shr 5) + (V[1] xor S) + K[S and 3]);
  end;
end;
 
function XTeaCryptStr(const Msg, Pwd: ShortString): ShortString;
var
  V: TTeaMsgBlock;
  K: TTeaKeyBlock;
  I, L, N: Integer;
begin
  L := Length(Pwd); if L > SizeOf(K) then L := SizeOf(K);
  K[0] := 0; K[1] := 0; K[2] := 0; K[3] := 0; Move(Pwd[1], K[0], L);
 
  I := 1; L := Length(Msg);
  if L > 0 then SetLength(Result, ((L - 1) div SizeOf(V) + 1) * SizeOf(V))
           else SetLength(Result, 0);
  while I <= L do begin
    V[0] := 0; V[1] := 0;
    N := L - I + 1; if N > SizeOf(V) then N := SizeOf(V);
    Move(Msg[I], V[0], N);
    XTeaCrypt(V, K);
    Move(V[0], Result[I], SizeOf(V));
    Inc(I, SizeOf(V))
  end;
end;
 
function XTeaDecryptStr(const Msg, Pwd: ShortString): ShortString;
var
  V: TTeaMsgBlock;
  K: TTeaKeyBlock;
  I, L, N: Integer;
begin
  L := Length(Pwd); if L > SizeOf(K) then L := SizeOf(K);
  K[0] := 0; K[1] := 0; K[2] := 0; K[3] := 0; Move(Pwd[1], K[0], L);
 
  I := 1; L := Length(Msg);
  if L > 0 then SetLength(Result, ((L - 1) div SizeOf(V) + 1) * SizeOf(V))
           else SetLength(Result, 0);
  while I <= L do begin
    V[0] := 0; V[1] := 0;
    N := L - I + 1; if N > SizeOf(V) then N := SizeOf(V);
    Move(Msg[I], V[0], N);
    XTeaDecrypt(V, K);
    Move(V[0], Result[I], SizeOf(V));
    Inc(I, SizeOf(V))    
  end;
end;
 
{ ========================================================================= }
 
function StrToHex(const stringIn: ShortString) : ShortString;
 var
   cnt: Integer;
   C: byte;
 begin
   result := '';
   for cnt := 1 to Length(stringIn) do
     begin
       C := Ord(stringIn[cnt]);
       result := result + Format('%2.2x', [C]);
     end;
 end;
 
function HexToStr(stringIn: ShortString) : ShortString;
 var
   C: byte;
   tmp, subString: ShortString;
 
 begin
   result := '';
   tmp := stringIn;
   repeat
    subString := Copy(tmp, 1, 2);
    C := StrToInt('$' + subString);
    result := result + AnsiChar(C);
    tmp := Copy(tmp, 3, Length(tmp) - 2);
   until
    tmp = '';
 end;
 
end.


Edited by: Rolf Fankhauser on Sep 5, 2015 3:29 AM
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: strange string behaviour
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2015 12:19 AM   in response to: Rolf Fankhauser in response to: Rolf Fankhauser
Rolf Fankhauser wrote:
Hi all,

I made a test program to encrypt and decrypt strings with xtea algorithm.

Ran your code password = 'password' in XE8 FMX

worked fine ... best guess is the #9s are scrolling the sCode off the viewable memo screen
so try one of these
1) show scrollbars on memo
2) make memo wider
3) remove #9s (or at least 1)

--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"

Rolf Fankhauser

Posts: 22
Registered: 9/3/01
Re: strange string behaviour  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2015 2:19 AM   in response to: Linden ROTH in response to: Linden ROTH
Hi Linden

Thanks a lot for testing. I already tried all your suggestions but without success. I use XE5 VCL.
I could try it with FireMonkey to see if it's a problem of VCL.

[Here is a sreen shot if sCode is added to the end.|https://drive.google.com/file/d/0B7Xm9sXQbqBlTThuMXNYMlZWSEU/view?usp=sharing]
[and here if I add sCode first.|https://drive.google.com/file/d/0B7Xm9sXQbqBlZldHeG1rU3NSZk0/view?usp=sharing]
And it's the same result if I remove the tab's and add a space. Really very strange!
[Here is source code and exe.|https://drive.google.com/file/d/0B7Xm9sXQbqBlT2xBcENwYmpWLUk/view?usp=sharing]

Rolf

Linden ROTH wrote:
Rolf Fankhauser wrote:
Hi all,

I made a test program to encrypt and decrypt strings with xtea algorithm.

Ran your code password = 'password' in XE8 FMX

worked fine ... best guess is the #9s are scrolling the sCode off the viewable memo screen
so try one of these
1) show scrollbars on memo
2) make memo wider
3) remove #9s (or at least 1)

--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"

Linden ROTH

Posts: 467
Registered: 11/3/11
Re: strange string behaviour
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2015 3:29 PM   in response to: Rolf Fankhauser in response to: Rolf Fankhauser
Rolf Fankhauser wrote:
Hi Linden

Thanks a lot for testing. I already tried all your suggestions but without success. I use XE5 VCL.
I could try it with FireMonkey to see if it's a problem of VCL.

Its your problem not VCL as sTest is returned with trailing #0s so the "line" is truncated
use
     line := sDateTime + ' ' + trim( sTest ) + #9#9 + sCode;


--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"
Rolf Fankhauser

Posts: 22
Registered: 9/3/01
Re: strange string behaviour  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 6, 2015 1:33 AM   in response to: Linden ROTH in response to: Linden ROTH
Thanks, you are right! XTeaDecryptStr returns these trailing #0s, so I will trim the result to prevent this.

Linden ROTH wrote:
Rolf Fankhauser wrote:
Hi Linden

Thanks a lot for testing. I already tried all your suggestions but without success. I use XE5 VCL.
I could try it with FireMonkey to see if it's a problem of VCL.

Its your problem not VCL as sTest is returned with trailing #0s so the "line" is truncated
use
     line := sDateTime + ' ' + trim( sTest ) + #9#9 + sCode;


--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02