我有加密和解密函数如下,希望你作一下参考:加密函数是vb写的,转换一下就可以了!!
'用户口令加密
Public Function PassWordCode(password As String) As String Dim il_bit, il_x, il_y, il_z, il_len, i As Long
Dim is_out As String
il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len
il_bit = AscW(Mid(password, i, 1)) 'W系列支持unicode
il_y = (il_bit * 13 Mod 256) + il_x
is_out = is_out & ChrW(Fix(il_y)) '取整 int和fix区别: fix修正负数
il_x = il_bit * 13 / 256
Next
is_out = is_out & ChrW(Fix(il_x))
password = is_out
il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len
il_bit = AscW(Mid(password, i, 1))
'取前4位值
il_y = il_bit / 16 + 64
is_out = is_out & ChrW(Fix(il_y))
'取后4位值
il_y = (il_bit Mod 16) + 64
is_out = is_out & ChrW(Fix(il_y))
Next
PassWordCode = is_out
End Function解密函数如下:
function PassWordDeCode(password: string): string;
var
il_x, il_y, il_len, I, il_bit: Integer;
begin
Result := '';
for I := 1 to Length(password) div 2 do begin
il_bit := Ord(password[I * 2 - 1]);
il_y := (il_bit - 64) * 16;
il_y := il_y + Ord(password[I * 2 ]) - 64;
Result := Result + Chr(il_y);
end;
password := Result;
Result := '';
il_len := Length(password);
il_x := Ord(password[il_len]);
for i := (il_len - 1) downto 1 do begin
il_y := il_x * 256 + Ord(password[I]);
il_x := il_y mod 13;
Result := Chr(il_y div 13) + Result;
end;
end;
'用户口令加密
Public Function PassWordCode(password As String) As String Dim il_bit, il_x, il_y, il_z, il_len, i As Long
Dim is_out As String
il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len
il_bit = AscW(Mid(password, i, 1)) 'W系列支持unicode
il_y = (il_bit * 13 Mod 256) + il_x
is_out = is_out & ChrW(Fix(il_y)) '取整 int和fix区别: fix修正负数
il_x = il_bit * 13 / 256
Next
is_out = is_out & ChrW(Fix(il_x))
password = is_out
il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len
il_bit = AscW(Mid(password, i, 1))
'取前4位值
il_y = il_bit / 16 + 64
is_out = is_out & ChrW(Fix(il_y))
'取后4位值
il_y = (il_bit Mod 16) + 64
is_out = is_out & ChrW(Fix(il_y))
Next
PassWordCode = is_out
End Function解密函数如下:
function PassWordDeCode(password: string): string;
var
il_x, il_y, il_len, I, il_bit: Integer;
begin
Result := '';
for I := 1 to Length(password) div 2 do begin
il_bit := Ord(password[I * 2 - 1]);
il_y := (il_bit - 64) * 16;
il_y := il_y + Ord(password[I * 2 ]) - 64;
Result := Result + Chr(il_y);
end;
password := Result;
Result := '';
il_len := Length(password);
il_x := Ord(password[il_len]);
for i := (il_len - 1) downto 1 do begin
il_y := il_x * 256 + Ord(password[I]);
il_x := il_y mod 13;
Result := Chr(il_y div 13) + Result;
end;
end;
//复杂的,可以参考DES,RSA,AES...
unit EncryptIt;
interface
USES Classes;
const
C1 = 52845;
C2 = 22719;
function Encrypt(const S: String; Key: Word): String;
function Decrypt(const S: String; Key: Word): String;
procedure EncryptFile(INFName, OutFName : String; Key : Word);
procedure DecryptFile(INFName, OutFName : String; Key : Word);
implementationfunction Encrypt(const S: String; Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(Result[I]) + Key) * C1 + C2;
end;
end;function Decrypt(const S: String; Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(S[I]) + Key) * C1 + C2;
end;
end;
procedure EncryptFile(INFName, OutFName : String; Key : Word);
VAR
MS, SS : TMemoryStream;
X : Integer;
C : Byte;
begin
MS := TMemoryStream.Create;
SS := TMemoryStream.Create;
TRY
MS.LoadFromFile(INFName);
MS.Position := 0;
FOR X := 0 TO MS.Size - 1 DO
begin
MS.Read(C, 1);
C := (C xor (Key shr 8));
Key := (C + Key) * C1 + C2;
SS.Write(C,1);
end;
SS.SaveToFile(OutFName);
FINALLY
SS.Free;
MS.Free;
end;
end;procedure DecryptFile(INFName, OutFName : String; Key : Word);
VAR
MS, SS : TMemoryStream;
X : Integer;
C, O : Byte;
begin
MS := TMemoryStream.Create;
SS := TMemoryStream.Create;
TRY
MS.LoadFromFile(INFName);
MS.Position := 0;
FOR X := 0 TO MS.Size - 1 DO
begin
MS.Read(C, 1);
O := C;
C := (C xor (Key shr 8));
Key := (O + Key) * C1 + C2;
SS.Write(C,1);
end;
SS.SaveToFile(OutFName);
FINALLY
SS.Free;
MS.Free;
end;
end;end.