function StrToHexStr(const S:string):string;
//字符串转换成16进制字符串
var
I:Integer;
begin
for I:=1 to Length(S) do
begin
if I=1 then
Result:=IntToHex(Ord(S[1]),2)
else Result:=Result+IntToHex(Ord(S[I]),2);
end;
end;
function HexStrToStr(const S:string):string;
//16进制字符串转换成字符串
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin
while not (S[t] in ['0'..'9','A'..'F','a'..'f']) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
function XorStr(const S:string):string;
//异或字符串
const
aXorChar:array [0..2] of Byte =(3,9,15); //可以多写几个 ,这里用3个做示范
var
I:Integer;
begin
SetLength(Result,Length(S));
for I:=1 to Length(S) do
begin
Result[I]:=Char(Ord(S[I]) Xor aXorChar[I mod (High(aXorChar)+1)]);
end;
end;
function SetPassStr(const S:string):string;
//字符串加密
begin
Result:=StrToHexStr(XorStr(S));
end;
function GetPassStr(const S:string):string;
//字符串解密
begin
Result:=XorStr(HexStrToStr(S));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text:=SetPassStr(Memo1.Text);//测试加密
Memo3.Text:=GetPassStr(Memo2.Text);//测试解密
if Memo1.Text=Memo3.Text then
ShowMessage('测试正确');
//如果Memo3和Memo1的内容一样,则测试正确。
end;