The Tiny Encryption Algorithm (TEA)
Not working in Pascal/delphi on 6 windows xp ?
First I made a pointer version in pascal just like the c version then
I made this one with only variables, still not working.
I suspect it might have something to do with shl and shr ?
or something else is going on... I really have no idea...
The code is very short.
*** begin of c code ***
void encipher(unsigned long *const v,unsigned long *const w,
const unsigned long *const k)
{
register unsigned long y=v[0],z=v[1],sum=0,delta=0x9E3779B9,
a=k[0],b=k[1],c=k[2],d=k[3],n=32;
while(n-->0)
{
sum += delta;
y += (z<<4)+a ^ z+sum ^ (z>>5)+b;
z += (y<<4)+c ^ y+sum ^ (y>>5)+d;
}
w[0]=y; w[1]=z;
Quote:
}
void decipher(unsigned long *const v,unsigned long *const w,
const unsigned long *const k)
{
register unsigned long y=v[0],z=v[1],sum=0xC6EF3720,
delta=0x9E3779B9,a=k[0],b=k[1],c=k[2],
d=k[3],n=32;
/* sum = delta<<5, in general sum = delta * n */
while(n-->0)
{
z -= (y<<4)+c ^ y+sum ^ (y>>5)+d;
y -= (z<<4)+a ^ z+sum ^ (z>>5)+b;
sum -= delta;
}
w[0]=y; w[1]=z;
Quote:
}
// c test program by skybuck
int main(int argc, char* argv[])
{
unsigned long a[2];
unsigned long b[2];
unsigned long c[2];
unsigned long key[4];
printf("program start\n");
a[0] = 324534;
a[1] = 5743234;
b[0] = 0;
b[1] = 0;
c[0] = 0;
c[1] = 0;
key[0] = 345345;
key[1] = 6456743;
key[2] = 1486534;
key[3] = 254645;
printf("data : %lu%lu \n", a[0], a[1] );
encipher( a, b, key );
printf("encrypted: %lu%lu \n", b[0], b[1] );
decipher( b, c, key );
printf("decrypted: %lu%lu \n", c[0], c[1] );
printf("program end\n");
return 0;
Quote:
}
*** end of c code
*** begin of pascal code
unit unit_tea_64_bit;
interface
procedure tea_encipher_64_bit( const in1, in2 : longword;
var out1, out2 : longword;
const key1, key2, key3, key4 : longword );
procedure tea_decipher_64_bit( const in1, in2 : longword;
var out1, out2 : longword;
const key1, key2, key3, key4 : longword );
(*
/************************************************
David Wheeler and Roger Needham of the
Cambridge Computer Laboratory
implementation
procedure tea_encipher_64_bit( const in1, in2 : longword;
var out1, out2 : longword;
const key1, key2, key3, key4 : longword );
var
y,z,_sum,delta,a,b,c,d,n : longword;
begin
// y := input_64[0];
// z := input_64[1];
y := in1;
z := in2;
_sum := 0;
delta := $9E3779B9;
a := key1;
b := key2;
c := key3;
d := key4;
n := 32;
while (n>0) do
begin
_sum := _sum + delta;
y := y + (z shl 4) + (a xor z) + (_sum xor (z shr 5)) + b;
z := z + (y shl 4) + (c xor y) + (_sum xor (y shr 5)) + d;
n := n - 1;
end;
out1 := y;
out2 := z;
end;
procedure tea_decipher_64_bit( const in1, in2 : longword;
var out1, out2 : longword;
const key1, key2, key3, key4 : longword );
var
y,z,_sum,delta,a,b,c,d,n : longword;
begin
y := in1;
z := in2;
_sum := $C6EF3720;
delta := $9E3779B9;
a := key1;
b := key2;
c := key3;
d := key4;
n := 32;
// _sum := delta shr 5;
// sum = delta<<5, in general sum = delta * n
while (n>0) do
begin
z := z - (y shl 4) + (c xor y) + (_sum xor (y shr 5)) + d;
y := y - (z shl 4) + (a xor z) + (_sum xor (z shr 5)) + b;
_sum := _sum - delta;
n := n - 1;
end;
out1 := y;
out2 := z;
end;
end.
// *** end of pascal code ***
// *** start of pascal example code ***
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
data_a : array[0..1] of longword;
data_b : array[0..1] of longword;
data_c : array[0..1] of longword;
key : array[0..3] of longword;
end;
var
Form1: TForm1;
implementation
uses unit_tea_64_bit;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
(*
randomize;
data_a[0] := random(maxint);
data_a[1] := random(maxint);
key[0] := random(maxint);
key[1] := random(maxint);
key[2] := random(maxint);
key[3] := random(maxint);
*)
data_a[0] := 23;
data_a[1] := 34;
data_b[0] := 0;
data_b[1] := 0;
data_c[0] := 0;
data_c[1] := 0;
key[0] := 123;
key[1] := 54;
key[2] := 36;
key[3] := 34;
Memo1.Lines.Add( 'data a before encryption:');
Memo1.Lines.Add( IntToStr(data_a[0]) );
Memo1.Lines.Add( IntToStr(data_a[1]) );
tea_encipher_64_bit( data_a[0], data_a[1],
data_b[0], data_b[1],
key[0], key[1], key[2], key[3] );
Memo1.Lines.Add( 'data b after encryption:');
Memo1.Lines.Add( IntToStr(data_b[0]) );
Memo1.Lines.Add( IntToStr(data_b[1]) );
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
tea_decipher_64_bit(
data_b[0], data_b[1],
data_c[0], data_c[1],
key[0], key[1], key[2], key[3] );
Memo1.Lines.Add( 'data c after decryption:');
Memo1.Lines.Add( IntToStr(data_c[0]) );
Memo1.Lines.Add( IntToStr(data_c[1]) );
end;
end.
*** // end of pascal example code ***