function TForm1.cryptstr(const s:string
stype: dword):string;
var
i: integer;
fkey: integer;
begin
result:='';
case stype of
0:
begin
randomize;
fkey := random($ffff);
for i:=1 to length(s) do
result := result+chr( ord(s) xor i xor fkey);
result := result + char(fkey);
end;
1:
begin
fkey := ord(s[length(s)]);
for i:=1 to length(s) - 1 do
result := result+chr( ord(s) xor i xor fkey);
end;
end;
function StrXor(const SrcStr,KeyStr:String):String;
var
i,n:Integer;
begin
Result:=SrcStr;
//异或操作的长度为两个字符串中短的那个的长度,以免发生越界
if Length(SrcStr)>Length(KeyStr) then
n:=Length(KeyStr)
else
n:=Length(SrcStr);
for i:=1 to n do //xor
Byte(Result):=Byte(SrcStr) xor Byte(KeyStr);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
mstr:String;
begin
mstr:=StrXor('user','name');
Caption:=StrXor(mstr,'name');
end;