summaryrefslogtreecommitdiff
path: root/plugins/!Deprecated/Dbx_mmap_SA/Cryptors/Athena/UAthena.pas
blob: 15a9674e0c07155f708bb8a9e0543957deaf6075 (plain)
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
170
171
172
173
174
175
176
unit UAthena;
{
  Athena: cryptor module for Miranda SecuredMMAP Database driver
  Copyright 2007-2008 Klyde

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
}


interface
uses
   md5_unit, windows;
Type
   Arr  = array of byte;
   PArr = ^Arr;
   
   Function MD5_Mod(const s: AnsiString; block_count: byte): AnsiString;
   Procedure MakeKey(key: PArr; len: word; const pwd: AnsiString);
   Procedure EncryptData(key: PArr; data: PByte; size: LongWord);
   Procedure DecryptData(key: PArr; data: PByte; size: LongWord);

implementation
//==============================================================================
Function str_back(const s: AnsiString): AnsiString;
Var
   i: integer;
Begin
   result := '';
   for i := Length(s) downto 1 do result := result + s[i];
end;
//==============================================================================
Function MD5_Mod(const s: AnsiString; block_count: byte): AnsiString;
Var
   s1, s2, sb : AnsiString;
   k          : word;
Begin
   sb := str_back(s);
   s2 := '';
   For k := 1 to block_count do
   Begin
      s1 := md5(s + sb);
      s2 := str_back(s2 + md5(s1+sb+s2));
   End;
   result := s2;
end;
//==============================================================================
Procedure MakeKey(key: PArr; len: word; const pwd: AnsiString);
Var
   s : AnsiString;
   i : word;
   dummy: integer;
Begin
   if len > 64 then Len := ((Len div 16) + 1)*16 else Len := 64;
   SetLength(key^, Len);
   s := MD5_mod(pwd, len div 16);
   for i := 1 to length(s) div 2 do
   begin
     val('$' + copy(s, i*2 - 1, 2),key^[i-1],dummy);
   end;
end;
//==============================================================================
Procedure GetNENum(key: arr; var n1, n2: LongWord);
Var
   i: LongWord;
Begin
   n1 := 0;
   n2 := 0;
   for i := 0 to Length(key) - 1 do
   Begin
      n1 := n1 + key[i] + (i + 1)*(n1+1);
      n2 := n2 + key[i] - (i + 1)*(n2+1);
   end;
   n1 := n1*2 + 1;
   n2 := n2*2 + 3;
end;

//==============================================================================
Procedure SimGamm(key: PArr; data: PByte; size: LongWord);
Var
   kg : Arr;
   i, n1, n2 : LongWord;
   lk, k1, k2 : word;
Begin
   lk := Length(key^);
   SetLength(kg, lk);
   for i := 0 to lk - 1 do kg[i] := key^[i];
   GetNENum(kg, n1, n2);
   For i := 1 to size - 1 do
   Begin
      if (i mod lk) = 0 then GetNENum(kg, n1, n2);
      k1 := (i+n1+7)*n2 mod lk;
      k2 := (i+n2+3)*n1 mod lk;

      PByte(uint_ptr(data)+i)^ := PByte(uint_ptr(data)+i)^ xor kg[k1] xor kg[k2];

      kg[k1] := kg[k1]*k1 + kg[k2] + i*k2;
      kg[k2] := kg[k2]*k2 + kg[k1] + i*k1;
   end;
end;
//==============================================================================
Procedure Left(key: PArr; data: PByte; size: LongWord);
Var
   k : Arr;
   i, n1, n2 : LongWord;
   lk, k1, k2 : word;

Begin
   lk := Length(key^);

   SetLength(k, lk);
   for i := 0 to lk - 1 do k[i] := key^[i];
   GetNENum(k, n1, n2);
   //---------------------------------------------------------------------------
   k1 := (n2 + lk)*n1 mod lk;
   k2 := (n1 + lk)*n2 mod lk;
   data^ := data^ xor k[k1] xor k[k2];

   //---------------------------------------------------------------------------
   For i := 1 to size - 1 do
   Begin
      k1 := (i+n1)*n2 mod lk;
      k2 := (i+n2)*n1 mod lk;

      PByte(uint_ptr(data)+i)^ := PByte(uint_ptr(data)+i)^ xor ((PByte(uint_ptr(data)+i-1)^ xor k[k1]) xor k[k2]);
   end;
end;
//==============================================================================
Procedure Right(key: PArr; data: PByte; size: LongWord);
Var
   k : Arr;
   i, n1, n2 : LongWord;
   lk, k1, k2 : word;
Begin
   lk := Length(key^);
   SetLength(k, lk);
   for i := 0 to lk - 1 do k[i] := key^[i];
   GetNENum(k, n1, n2);
   //---------------------------------------------------------------------------
   For i := size - 1 downto 1 do
   Begin
      k1 := (i+n1)*n2 mod lk;
      k2 := (i+n2)*n1 mod lk;
      PByte(uint_ptr(data) + i)^ := PByte(uint_ptr(data)+i)^ xor ((PByte(uint_ptr(data) + i - 1)^ xor k[k1]) xor k[k2]);
   end;
   //---------------------------------------------------------------------------
   k1 := (n2 + lk)*n1 mod lk;
   k2 := (n1 + lk)*n2 mod lk;
   data^ := data^ xor k[k1] xor k[k2];
end;
//==============================================================================
Procedure EncryptData(key: PArr; data: PByte; size: LongWord);
Begin
   Left(key, data, size);
   SimGamm(key, data, size);
end;
//==============================================================================
Procedure DecryptData(key: PArr; data: PByte; size: LongWord);
Begin
   SimGamm(key, data, size);
   Right(key, data, size);
end;
//==============================================================================
end.