给你一个简单的例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StrUtils, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
LCW:Integer;
LS2E:Integer;
LAM:Integer;
MP:Integer;
Matrix:String;
mov1:String;
mov2:String;
CodeWord:String;
CWL:String;
EncryptedString:String;
EncryptedLetter:String;
strCryptMatrix:array[0..96] of String;
procedure SetKeyStringProperty(sKeyString:String);
Function Encrypt(mstext:String):String;
procedure CreateInitialize();
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SetKeyStringProperty(sKeyString:String);
begin
CodeWord:=sKeyString;
End;
Function TForm1.Encrypt(mstext:String):String;
var X:Integer;
Y:Integer;
Z:Integer;
C2E:String;
Str2Encrypt:String;
begin
Str2Encrypt := mstext;
LS2E := Length(mstext);
LCW := Length(CodeWord);
EncryptedLetter := '';
EncryptedString := '';
Y := 1;
For X := 1 To LS2E do begin
C2E := copy(Str2Encrypt, X, 1);
MP := Pos(C2E,Matrix);
CWL := Copy(CodeWord, Y, 1);
For Z := 1 To LAM do begin
If copy(strCryptMatrix[Z], MP, 1) = CWL Then begin
EncryptedLetter := Leftstr(strCryptMatrix[Z], 1);
EncryptedString := EncryptedString + EncryptedLetter;
break
End;
end;
Y := Y + 1;
If Y > LCW Then Y := 1;
end;
Result := EncryptedString;
End;
procedure TForm1.CreateInitialize();
var W:Integer;
X:Integer;
begin
Matrix := '8x3p5BeabcdfghijklmnoqrstuvwyzACDEFGHIJKLMNOPQRSTUVWXYZ1246790';
// Matrix := Matrix + Chr(13);
// Matrix := Matrix + Chr(10);
// Matrix := Matrix + Chr(34);
W := 1;
LAM := Length(Matrix);
strCryptMatrix[1] := Matrix;
For X := 2 To LAM do begin
mov1 := LeftStr(strCryptMatrix[W], 1);
mov2 := RightStr(strCryptMatrix[W], (LAM - 1));
strCryptMatrix[X] := mov2 + mov1;
W := W + 1;
end;
End;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetKeyStringProperty('AAA');
edit2.Text:=Encrypt(edit1.Text);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateInitialize;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit3.Text:=Encrypt(edit2.Text);
end;
end.
在Matrix中任意改变顺序加密就不一样,还可以添加其他可见字符,先运行Button1Click加密edit1.text到edit2.text,马上再运行Button2Click将edit2.text解密到edit3.text,即edit1.text的值