我这有几个加解密的函数,看看你是否用得着。
==============================
//uses Wait;
//---------密碼加密---------------------------
function PPbin(s:integer):Char;
begin
if s<0 then
s:=s+256
else if s>255 then
s:=s-256;
Result:=Chr(S); //IntToStr(integer(chr(s)));
end;
function pw2bin(str:string;l:boolean):string;
var
a1,a2,a3,a4,a5,a6:string;
begin
if l then begin
a1:=PPbin(ord(Str[1])+31);
a2:=ppbin(Ord(Str[2])-ord(Str[1])-2);
a3:=ppbin(ord(Str[3])-ord(Str[2])-3);
a4:=ppbin(ord(Str[4])-ord(str[3])-4);
a5:=ppbin(ord(Str[5])-ord(Str[4])-5);
a6:=ppbin(ord(Str[6])-ord(Str[5])-6);
end
else
begin
a1:=ppbin(ord(Str[1])-31);
a2:=ppbin(ord(Str[2])+ord(a1[1])+2);
a3:=ppbin(ord(Str[3])+ord(a2[1])+3);
a4:=ppbin(ord(Str[4])+ord(a3[1])+4);
a5:=ppbin(ord(Str[5])+ord(a4[1])+5);
a6:=ppbin(ord(Str[6])+ord(a5[1])+6);
end;
Result:=a1+a2+a3+a4+a5+a6;
end;
//-----------------------------------------
function Encrypt(S: string): string;
var
i,j,k1,k2: integer;
cSum,nSum: integer;
sTmp: string[8];
begin
Result := Space(8)+'??';
//檢查密碼是否合法.
S := TrimRight(S);
if Length(S) < 5 then
begin Showmessage('密碼長度最少必須5位'); Exit; end;
if Length(S) > 8 then
begin Showmessage('密碼長度超過8位'); Exit; end;
cSum:= 0;
nSum:= 0;
k1:=0;
k2:=0;
for i := 1 to Length(S) do begin
if not (S in ['A'..'Z','a'..'z','0'..'9']) then
begin Showmessage('密碼含有不合法字元'); Exit; end;
if S in ['A'..'Z','a'..'z'] then Inc(cSum);
if S in ['0'..'9'] then Inc(nSum);
end;
if cSum < 3 then
begin Showmessage('密碼最少必須含3位字母'); Exit; end;
if nSum < 2 then
begin Showmessage('密碼最少必須含2位數字'); Exit; end;
//加密.
Randomize;
while True do begin
k1 := Random(30);
k2 := Random(30);
sTmp := PadR(S,8);
Result := '';
for i := 1 to 8 do begin
j := k1 Mod 30 + i;
j := (k2+j) Mod 30 + 1;
Result := Result + Chr(Ord(sTmp) xor j)
end;
if (Pos('"',Result)>0) or (Pos('''',Result)>0) then
Else Break;
end;
Result := Result + Chr(k1+40) + Chr(k2+60);
end;
//--------密碼解密----------------------------
function Decrypt(S: string): string;
var
i,j: integer;
Key1,Key2: integer;
begin
Result := '??'+Space(6);
//檢查密碼是否正確.
if Length(S) <> 10 then Exit;
//解密
Key1 := Ord(S[9])-40;
Key2 := Ord(S[10])-60;
Result := '';
for i := 1 to 8 do begin
j := Key1 Mod 30 + i;
j := (Key2+j) Mod 30 + 1;
Result := Result + Chr(Ord(S) xor j)
end;
Result := Trim(Result);
end;
//--------字元加密----------------------------
function EncryStr(S: String;n:Integer): String;
var
i,j,k1,k2: integer;
sTmp: String;
begin
//加密.
Randomize;
while True do begin
k1 := Random(30);
k2 := Random(30);
sTmp := PadR(S,n);
Result := '';
for i := 1 to n do begin
j := k1 Mod 30 + i;
j := (k2+j) Mod 30 + 1;
Result := Result + Chr(Ord(sTmp) xor j)
end;
if (Pos('"',Result)>0) or (Pos('''',Result)>0) then
Else Break;
end;
Result := Result + Chr(k1+40) + Chr(k2+60);
end;
//--------字元解密----------------------------
function DecryStr(S: String;n:Integer): String;
var
i,j: integer;
Key1,Key2: integer;
begin
//檢查密碼是否正確.
if Length(S) <> n+2 then Exit;
//解密
Key1 := Ord(S[n+1])-40;
Key2 := Ord(S[n+2])-60;
Result := '';
for i := 1 to n do begin
j := Key1 Mod 30 + i;
j := (Key2+j) Mod 30 + 1;
Result := Result + Chr(Ord(S) xor j)
end;
Result := Trim(Result);
end;