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

Connect with Us