-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encryp.pas
160 lines (141 loc) · 3.98 KB
/
Encryp.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
unit Encryp;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
// Mr. Franco Paolo Brescianini's Sample provide
// String Encryption/Decryption Algorithm
//
// Freeware Non-Visual Component for Delphi 2.0x
// Written By Tom Lee , From Taiwan - Republic Of China
// Version 1.01 Release date OCT 20 1996
//
interface
uses
{$IFnDEF FPC}
WinTypes, WinProcs,
{$ELSE}
LCLIntf, LCLType, //LMessages,
{$ENDIF}
SysUtils, Classes, // Messages, Graphics, Controls,
Forms, Dialogs;
type
TActionType = (atEncryption,atDecryption);
TTomEncryption = class(TComponent)
private
{ Private declarations }
FInputString:string;
FOutputString:string;
// FHexInputString:string;
// FHexOutputString:string;
FKeyString:string;
FAction:TActionType;
procedure SetInputString(input:string);
procedure SetOutputString(input:string);
procedure SetKeyString(input:string);
Function EncryptionEngine (Src:String; Key : String; Encrypt : Boolean):String;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
Procedure Execute;
published
{ Published declarations }
property Input: String read FInputString write SetInputString;
property Output: String read FOutputString write SetOutputString;
property Key: String read FKeyString write SetKeyString;
property Action: TActionType read FAction write FAction default atEncryption;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('AddEu', [TTomEncryption]);
end;
constructor TTomEncryption.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Action:=atEncryption;
end;
Procedure TTomEncryption.SetOutputString(input:string);
begin
if input<> FOutputString then
FOutputString:=input;
end;
Procedure TTomEncryption.SetKeyString(input:string);
begin
if input <> FKeyString Then
FKeyString:=input;
end;
Procedure TTomEncryption.SetInputString(input:string);
begin
if input <> FInputString Then
FInputString:=input;
end;
Function TTomEncryption.EncryptionEngine (Src:String; Key:String; Encrypt : Boolean):string;
var
// idx :integer;
KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Range :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='Tom Lee';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;
if Encrypt then
begin
Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen then KeyPos:= KeyPos + 1 else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
dest:=dest + format('%1.2x',[SrcAsc]);
offset:=SrcAsc;
end;
end
else
begin
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen Then KeyPos := KeyPos + 1 else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
end;
Result:=Dest;
end;
procedure TTomEncryption.Execute;
var
EncryptionFlag:Boolean;
begin
if length(FInputString)=0 then
begin
FOutputString:='';
exit;
end;
if FAction = atEncryption then
EncryptionFlag:=True
else
EncryptionFlag:=False;
FOutputString:=EncryptionEngine(FInputString,FKeyString,EncryptionFlag);
end;
end.