狼行神码

导航

简单的加密解密

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
 8 
 9 type
10   TForm1 = class(TForm)
11     LabeledEdit1: TLabeledEdit;
12     LabeledEdit2: TLabeledEdit;
13     LabeledEdit3: TLabeledEdit;
14     Button1: TButton;
15     Button2: TButton;
16     Button3: TButton;
17     procedure Button1Click(Sender: TObject);
18     procedure Button2Click(Sender: TObject);
19   private
20     { Private declarations }
21   public
22     { Public declarations }
23   end;
24 
25 var
26   Form1: TForm1;
27 
28 implementation
29 
30 {$R *.dfm}
31 var
32 
33 XorKey: array[0..7] of Byte = ($AB, $CD, $E1, $0A, $1B, $2C, $3D, $4E);    //加密字符  16进制
34 
35 function Enc(str: string): string;  //加密函数
36 var
37  i, j: Integer;
38 begin
39  Result := '';
40  j := 0;
41  for i := 1 to Length(str) do
42  begin
43  Result := Result + IntToHex(Byte(str[i]) xor XorKey[j], 2);  //Byte(str[i]):取 str[i]的Ascll 后与 XorKey[j]异或 取2位 再转换成16进制
44  j := (j + 1) mod 8;
45  end;
46 end;
47 
48 
49 procedure TForm1.Button1Click(Sender: TObject);
50 begin
51   LabeledEdit2.Text:= Enc(LabeledEdit1.Text);
52 
53 end;
54 
55 
56 function Dec(str: string): string;   //解密函数
57 var
58  i, j: Integer;
59 begin
60  Result := '';
61  j := 0;
62  for i := 1 to Length(str) div 2 do
63  begin
64  Result := Result + Char(StrToInt('$' + Copy(str, i * 2 - 1, 2)) xor
65  XorKey[j]);
66  j := (j + 1) mod 8;
67  end;
68 end;
69 
70 procedure TForm1.Button2Click(Sender: TObject);
71 begin
72 LabeledEdit3.Text:= Dec(LabeledEdit2.Text);
73 end;
74 
75 end.

 

posted on 2017-05-25 09:53  狼行神码  阅读(270)  评论(0编辑  收藏  举报