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