-
Notifications
You must be signed in to change notification settings - Fork 14
/
XXTEA.pas
169 lines (152 loc) · 4.07 KB
/
XXTEA.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
/**********************************************************\
| |
| XXTEA.pas |
| |
| XXTEA encryption algorithm library for Delphi/FreePascal |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <[email protected]> |
| LastModified: Dec 17, 2020 |
| |
\**********************************************************/
}
unit XXTEA;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses SysUtils;
function Encrypt(const Data, Key: TBytes): TBytes;
function Decrypt(const Data, Key: TBytes): TBytes;
implementation
type
TUint32 = Cardinal;
TUint32Array = array of TUint32;
const
Delta: TUint32 = $9e3779b9;
function ToUint32Array(const Data: TBytes; IncludeLength: Boolean): TUint32Array;
var
N, I, L: TUint32;
begin
Result:= nil; // <-- If not, it all doesn't work under FPC
L := Length(Data);
if ((L and 3) = 0) then N := L shr 2 else N := (L shr 2) + 1;
if (IncludeLength) then begin
SetLength(Result, N + 1);
Result[N] := L;
end
else SetLength(Result, N);
for I := 0 to L - 1 do
begin
Result[I shr 2] := Result[I shr 2] or (($000000ff and Data[I]) shl ((I and 3) shl 3));
end;
end;
function ToBytes(const Data: TUint32Array; IncludeLength: Boolean): TBytes;
var
N, M, I: TUint32;
begin
N := Length(Data) shl 2;
if (IncludeLength) then
begin
M := Data[Length(Data) - 1];
Dec(N, 4);
if ((M < N - 3) or (M > N)) then
begin
Result := nil;
Exit;
end
else N := M;
end;
SetLength(Result, N);
for I := 0 to N - 1 do
Result[I] := Byte((Data[I shr 2] shr ((I and 3) shl 3)) and $ff);
end;
function MX(Sum, Y, Z, P, E: TUint32; const K: TUint32Array): TUint32;
begin
Result := (((Z shr 5) xor (Y shl 2)) + ((Y shr 3) xor (Z shl 4))) xor ((Sum xor Y) + (K[P and 3 xor E] xor Z));
end;
function XXTEAEncrypt(var V, K: TUint32Array): TUint32Array;
var
N, Z, Y, Sum, E, P, Q: TUint32;
begin
N := Length(V) - 1;
if (N < 1) then
begin
Result := V;
Exit;
end;
if Length(K) < 4 then SetLength(K, 4);
Z := V[N];
Sum := 0;
Q := 6 + 52 div (N + 1);
repeat
Inc(Sum, Delta);
E := (Sum shr 2) and 3;
for P := 0 to N - 1 do
begin
Y := V[P + 1];
inc(V[P], MX(Sum, Y, Z, P, E, K));
Z := V[P];
end;
P := N;
Y := V[0];
inc(V[P], MX(Sum, Y, Z, P, E, K));
Z := V[P];
Dec(Q);
until Q = 0;
Result := V;
end;
function XXTEADecrypt(var V, K: TUint32Array): TUint32Array;
var
N, Z, Y, Sum, E, P, Q: TUint32;
begin
N := Length(V) - 1;
if (N < 1) then
begin
Result := V;
Exit;
end;
if Length(K) < 4 then SetLength(K, 4);
Y := V[0];
Q := 6 + 52 div (N + 1);
Sum := Q * Delta;
while (Sum <> 0) do
begin
E := (Sum shr 2) and 3;
for P := N downto 1 do
begin
Z := V[P - 1];
Dec(V[P], MX(Sum, Y, Z, P, E, K));
Y := V[P];
end;
P := 0;
Z := V[N];
Dec(V[0], MX(Sum, Y, Z, P, E, K));
Y := V[0];
Dec(Sum, Delta);
end;
Result := V;
end;
function Encrypt(const Data, Key: TBytes): TBytes;
var
V, K: TUint32Array;
begin
if (Length(Data) = 0) then Exit;
V := ToUint32Array(Data, True);
K := ToUint32Array(Key, False);
Result := ToBytes(XXTEAEncrypt(V, K), False);
end;
function Decrypt(const Data, Key: TBytes): TBytes;
var
V, K: TUint32Array;
begin
if (Length(Data) = 0) then exit;
V := ToUint32Array(Data, False);
K := ToUint32Array(Key, False);
Result := ToBytes(XXTEADecrypt(V, K), True);
end;
end.