-
Notifications
You must be signed in to change notification settings - Fork 2
/
uCrypt.pas
122 lines (120 loc) · 2.96 KB
/
uCrypt.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
unit uCrypt;
interface
uses
Windows, SysUtils;
const
C1 = 52845;
C2 = 22719;
CryptKey = 72957;
function Encrypt(Source: array of Byte; var Dest: array of Byte; Len: Cardinal): BOOL; overload;
function Decrypt(Source: array of Byte; var Dest: array of Byte; Len: Cardinal): BOOL; overload;
function Encrypt(Source: String; var Dest: string): BOOL; overload;
function Decrypt(Source: String; var Dest: string): BOOL; overload;
function Encrypt(Source: Pchar; Dest: PChar; Len: Cardinal): BOOL; overload;
function Decrypt(Source: Pchar; Dest: PChar; Len: Cardinal): BOOL; overload;
implementation
function Encrypt(Source: Pchar; Dest: PChar; Len: Cardinal): BOOL; overload;
var
i: Integer;
Key: Integer;
begin
Key:=CryptKey;
//判断数据是否正常
if (not Assigned(Source)) or (not Assigned(Dest)) or (Len <=0) then
begin
Result:=False;
Exit;
end;
//循环加密每一个字节
for i:=0 to Len - 1 do
begin
Dest[i]:=Char(Byte(Source[i]) xor (Key shr 8));
Key:=(Byte(Dest[i]) + Key) * C1 + C2;
end;
Result:=True;
end;
function Decrypt(Source: Pchar; Dest: PChar; Len: Cardinal): BOOL; overload;
var
i: Integer;
Key: Integer;
PrevBlock: Byte;
begin
Key:=CryptKey;
//判断数据是否正常
if (not Assigned(Source)) or (not Assigned(Dest)) or (Len <=0) then
begin
Result:=False;
Exit;
end;
//循环加密每一个字节
for i:=0 to Len - 1 do
begin
PrevBlock:=Byte(Source[i]);
Dest[i]:=Char(Byte(Source[i]) xor (Key shr 8));
Key:=(Byte(PrevBlock) + Key) * C1 + C2;
end;
Result:=True;
end;
function Encrypt(Source: String; var Dest: string): BOOL;
begin
Result:=False;
if Length(Source) > 0 then
begin
SetLength(Dest, Length(Source));
Encrypt(PChar(Source), PChar(Dest), Length(Source));
Result:=True;
end;
end;
function Decrypt(Source: String; var Dest: string): BOOL;
begin
Result:=False;
if Length(Source) > 0 then
begin
SetLength(Dest, Length(Source));
Decrypt(PChar(Source), PChar(Dest), Length(Source));
Result:=True;
end;
end;
function Encrypt(Source: array of Byte; var Dest: array of Byte; Len: Cardinal): BOOL;
var
i: Integer;
Key: Integer;
begin
Key:=CryptKey;
//判断数据是否正常
if Len <= 0 then
begin
Result:=False;
Exit;
end;
//循环加密每一个字节
for i:=0 to Len - 1 do
begin
Dest[i]:=Source[i] xor (Key shr 8);
Key:=(Dest[i] + Key) * C1 + C2;
end;
Result:=True;
end;
function Decrypt(Source: array of Byte; var Dest: array of Byte; Len: Cardinal): BOOL;
var
i: Integer;
PrevBlock: Byte;
Key: Integer;
begin
Key:=CryptKey;
//判断数据是否正常
if (Len <= 0) then
begin
Result:=False;
Exit;
end;
//循环解密每一个字节
for i:=0 to Len - 1 do
begin
PrevBlock:=Source[i];
Dest[i]:=Source[i] xor (Key shr 8);
Key:=(PrevBlock + Key) * C1 + C2;
end;
Result:=True;
end;
end.