- 用VB写的加密函数Encrypt和解密函数Decrypt
1 '加密函数
2 Function Encrypt(ByVal PlainStr As String, ByVal Key As String) As String
3 On Error Resume Next
4 Dim Char As String, KeyChar As String, NewStr As String
5 Dim Pos As Integer
6 Dim I As Integer, Side1 As String, Side2 As String
7 Pos = 1
8 For I = 1 To Len(PlainStr)
9 Char = Mid(PlainStr, I, 1)
10 KeyChar = Mid(Key, Pos, 1)
11 NewStr = NewStr & Chr(Asc(Char) Xor Asc(KeyChar))
12 If Pos = Len(Key) Then Pos = 0
13 Pos = Pos + 1
14 Next I
15 If Len(NewStr) Mod 2 = 0 Then
16 Side1 = StrReverse(Left(NewStr, (Len(NewStr) / 2)))
17 Side2 = StrReverse(Right(NewStr, (Len(NewStr) / 2)))
18 NewStr = Side1 & Side2
19 End If
20 Encrypt = NewStr
21 End Function
22
23 '解密函数
24 Function Decrypt(PlainStr As String, Key As String)
25 Dim Char As String, KeyChar As String, NewStr As String
26 Dim Pos As Integer
27 Dim I As Integer, Side1 As String, Side2 As String
28 Pos = 1
29 If Len(PlainStr) Mod 2 = 0 Then
30 Side1 = StrReverse(Left(PlainStr, (Len(PlainStr) / 2)))
31 Side2 = StrReverse(Right(PlainStr, (Len(PlainStr) / 2)))
32 PlainStr = Side1 & Side2
33 End If
34 For I = 1 To Len(PlainStr)
35 Char = Mid(PlainStr, I, 1)
36 KeyChar = Mid(Key, Pos, 1)
37 NewStr = NewStr & Chr(Asc(Char) Xor Asc(KeyChar))
38 If Pos = Len(Key) Then Pos = 0
39 Pos = Pos + 1
40 Next I
41 Decrypt = NewStr
42
- 用Delphi写的加密函数Encrypt和解密函数Decrypt
1 unit crypt;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 Edit1: TEdit;
13 Edit2: TEdit;
14 procedure Button1Click(Sender: TObject);
15 private
16 { Private declarations }
17 public
18 { Public declarations }
19 end;
20
21 var
22 Form1: TForm1;
23
24 implementation
25 uses StrUtils;
26 {$R *.dfm}
27
28 Function Encrypt(PlainStr :String; Key :String) :String;
29 var
30 dchar,keychar:char;
31 newstr:string;
32 pos:integer;
33 i:integer;
34 Side1 , Side2 : String;
35 begin
36 // On Error Resume Next
37 Pos := 1;
38 For I := 1 To Length(PlainStr) do
39 begin
40 dChar := PlainStr[I];
41 KeyChar := Key[Pos] ;
42 NewStr := NewStr + Chr( ord(dChar) Xor ord(KeyChar)) ;
43 If Pos = Length(Key) Then
44 Pos := 0;
45 inc(Pos);
46 end;
47 If Length(NewStr) Mod 2 = 0 Then
48 begin
49 Side1 := ReverseString(LeftStr(NewStr, (Length(NewStr) div 2)));
50 Side2 := ReverseString(RightStr(NewStr, (Length(NewStr) div 2)));
51 NewStr := Side1 + Side2;
52 end;
53 Encrypt := NewStr;
54 end;
55
56
57 Function Decrypt(PlainStr: String; Key : String):string;
58 var
59 dChar :char;
60 KeyChar :char;
61 NewStr : String;
62 Pos : Integer;
63 I : Integer;
64 Side1 :String;
65 Side2 : String;
66 begin
67 Pos := 1;
68 If Length(PlainStr) Mod 2 = 0 Then
69 begin
70 Side1 := ReverseString(LeftStr(PlainStr, (Length(PlainStr) div 2)));
71 Side2 := ReverseString(RightStr(PlainStr, (Length(PlainStr) div 2)));
72 PlainStr := Side1 + Side2;
73 end;
74 For I := 1 To Length(PlainStr) do
75 begin
76 dChar := PlainStr[I];
77 KeyChar :=Key[Pos];
78 NewStr := NewStr + Chr(ord(dChar) Xor ord(KeyChar));
79 If Pos = Length(Key)
80 Then Pos := 0;
81 inc(Pos);
82 end;
83 Decrypt := NewStr;
84 end;
85 procedure TForm1.Button1Click(Sender: TObject);
86 var
87 s:string;
88 begin
89 s:=Encrypt('aaabbbccc','1234');
90 Edit1.Text:=s;
91 Edit2.Text:=Decrypt(s,'1234');
92 end;
93
94 end