function TMyClass.Crypt(s: string; Key: Word; const bEncrypt: boolean): string; //将字符串加密解密,bEncrypt=True表示加密 False表示解密 const SeedA = 56789; /// 常量,你可以修改 SeedB = 54329; /// 常量,你可以修改 var i: integer; ps, pr : ^byte; begin if bEncrypt then s := s+#0; SetLength(Result, Length(s)); ps := @s[1]; pr := @Result[1]; for i := 1 to length(s) do begin pr^ := ps^ xor (Key shr 8); if bEncrypt then Key := (pr^ + Key) * SeedA + SeedB else Key := (ps^ + Key) * SeedA + SeedB; pr := pointer(integer(pr) + 1); ps := pointer(integer(ps) + 1); end; end;
unit Eds; {Encrypt & Decrypt String}interfaceuses SysUtils;const StartKey = 973; {Start default key} MultKey = 790314; {Mult default key} AddKey = 23916; {Add default key}function EncryptString(s: string): string; //Encrypt to number function DecryptString(s: string): string; //Decrypt from numberfunction EncodeString(s: string): string; //Encrypt to character function DecodeString(s: string): string; //Decrypt from characterimplementation{$R-} {$Q-} function Encrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string; var I : Byte; begin Result := ''; for I := 1 to Length(InString) do begin Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8)); StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey; end; end;function Decrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string; var I : Byte; begin Result := ''; for I := 1 to Length(InString) do begin Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8)); StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey; end; end; {$R+} {$Q+}function EncodeString(s: string): string; begin Result := Encrypt(s, StartKey, MultKey, AddKey); end;function DecodeString(s: string): string; begin Result := Decrypt(s, StartKey, MultKey, AddKey); end;Function Int2Str(int1: Integer; Len: Integer): string; var i, j: integer; begin if Length(inttostr(int1)) >= Len then Result:=Inttostr(int1) else begin Result := ''; i := Len - Length(IntToStr(Int1)); for j := 1 to i do Result := Result + '0'; Result := Result + IntToStr(Int1); end; end;function Char2ByteStr(s: string): string; var i: Byte; begin Result:=''; for i := 1 to Length(s) do Result := Result + Int2Str(Byte(s[i]), 3); end;function Byte2CharStr(s: string): string; var i: Integer; begin i := 1; Result := ''; if (Length(s) mod 3) = 0 then while i < Length(s) do begin Result := Result + Char(StrToInt(Copy(s, i, 3))); i := i + 3; end; end;function EncryptString(s: string): string; var years, months, days, hours, mins, secs, msec: Word; Sk, Mk, Ak: Longint; begin DecodeDate(Now, years, months, days); DecodeTime(Now, hours, mins, secs, msec); Sk := msec; if Sk < 256 then Sk:= Sk + 256; Mk := ((years - 1900) * 12 + months) * 30 + days + Sk * 10 + Sk; Ak := (23 * hours + mins) * 60 + secs + Sk * 10 + Sk; Result := Char2ByteStr(Encrypt(Int2Str(Sk, 3), StartKey, MultKey, AddKey)) + Char2ByteStr(Encrypt(Int2Str(Mk, 5), StartKey, MultKey, AddKey)) + Char2ByteStr(Encrypt(Int2Str(Ak, 5), StartKey, MultKey, AddKey)) + Char2ByteStr(Encrypt(s, Sk, Mk, Ak)); end;function DecryptString(s: string): string; var Sk, Mk, Ak: Longint; begin Sk := StrToInt(Decrypt(Byte2CharStr(Copy(s, 1, 9)), StartKey, MultKey, AddKey)); Mk := StrToInt(Decrypt(Byte2CharStr(Copy(s, 10, 15)), StartKey, MultKey, AddKey)); Ak := StrToInt(Decrypt(Byte2CharStr(Copy(s, 25, 15)), StartKey, MultKey, AddKey)); Result := Decrypt(Byte2CharStr(Copy(s, 40, Length(s)-39)), Sk, Mk, Ak); end;end.
{ TODO : 字符串加密函数 }
var
KeyLen: Integer;
KeyPos: Integer;
offset: Integer;
SrcPos: integer;
SrcAsc: integer;
dest: string;
Range: Integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
key := 'Think Space';
KeyPos := 0;
Range := 256;
Randomize;
offset := Random(Range);
dest := format('%1.2x', [offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc := (Ord(Src[SrcPos]) + offset) mod 255;
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
SrcAsc := SrcAsc xor Ord(Key[KeyPos]);
dest := dest + format('%1.2x', [SrcAsc]);
offset := SrcAsc;
end;
Result := Dest;
end;//==========================================================================
//** 字符串解密
//==========================================================================function Mydecrypt(Src, Key: string): string;
var
KeyLen: Integer;
KeyPos: Integer;
offset: Integer;
SrcPos: integer;
SrcAsc: integer;
dest: string;
TmpSrcAsc: Integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
key := 'Think Space';
KeyPos := 0;
offset := StrToInt('$' + copy(src, 1, 2));
SrcPos := 3;
repeat
SrcAsc := StrToInt('$' + copy(src, SrcPos, 2));
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset := srcAsc;
SrcPos := SrcPos + 2;
until SrcPos >= Length(Src);
Result := Dest;
end;
什么RC6,DES,IDA都可以
多谢!
const bEncrypt: boolean): string; //将字符串加密解密,bEncrypt=True表示加密 False表示解密
const
SeedA = 56789; /// 常量,你可以修改
SeedB = 54329; /// 常量,你可以修改
var
i: integer;
ps, pr : ^byte;
begin
if bEncrypt then
s := s+#0;
SetLength(Result, Length(s));
ps := @s[1];
pr := @Result[1];
for i := 1 to length(s) do
begin
pr^ := ps^ xor (Key shr 8);
if bEncrypt then
Key := (pr^ + Key) * SeedA + SeedB
else
Key := (ps^ + Key) * SeedA + SeedB;
pr := pointer(integer(pr) + 1);
ps := pointer(integer(ps) + 1);
end;
end;
SysUtils;const
StartKey = 973; {Start default key}
MultKey = 790314; {Mult default key}
AddKey = 23916; {Add default key}function EncryptString(s: string): string; //Encrypt to number
function DecryptString(s: string): string; //Decrypt from numberfunction EncodeString(s: string): string; //Encrypt to character
function DecodeString(s: string): string; //Decrypt from characterimplementation{$R-}
{$Q-}
function Encrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string;
var
I : Byte;
begin
Result := '';
for I := 1 to Length(InString) do
begin
Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey;
end;
end;function Decrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string;
var
I : Byte;
begin
Result := '';
for I := 1 to Length(InString) do
begin
Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey;
end;
end;
{$R+}
{$Q+}function EncodeString(s: string): string;
begin
Result := Encrypt(s, StartKey, MultKey, AddKey);
end;function DecodeString(s: string): string;
begin
Result := Decrypt(s, StartKey, MultKey, AddKey);
end;Function Int2Str(int1: Integer; Len: Integer): string;
var
i, j: integer;
begin
if Length(inttostr(int1)) >= Len then Result:=Inttostr(int1)
else
begin
Result := '';
i := Len - Length(IntToStr(Int1));
for j := 1 to i do Result := Result + '0';
Result := Result + IntToStr(Int1);
end;
end;function Char2ByteStr(s: string): string;
var
i: Byte;
begin
Result:='';
for i := 1 to Length(s) do Result := Result + Int2Str(Byte(s[i]), 3);
end;function Byte2CharStr(s: string): string;
var
i: Integer;
begin
i := 1;
Result := '';
if (Length(s) mod 3) = 0 then
while i < Length(s) do
begin
Result := Result + Char(StrToInt(Copy(s, i, 3)));
i := i + 3;
end;
end;function EncryptString(s: string): string;
var
years, months, days, hours, mins, secs, msec: Word;
Sk, Mk, Ak: Longint;
begin
DecodeDate(Now, years, months, days);
DecodeTime(Now, hours, mins, secs, msec);
Sk := msec;
if Sk < 256 then Sk:= Sk + 256;
Mk := ((years - 1900) * 12 + months) * 30 + days + Sk * 10 + Sk;
Ak := (23 * hours + mins) * 60 + secs + Sk * 10 + Sk;
Result := Char2ByteStr(Encrypt(Int2Str(Sk, 3), StartKey, MultKey, AddKey)) +
Char2ByteStr(Encrypt(Int2Str(Mk, 5), StartKey, MultKey, AddKey)) +
Char2ByteStr(Encrypt(Int2Str(Ak, 5), StartKey, MultKey, AddKey)) +
Char2ByteStr(Encrypt(s, Sk, Mk, Ak));
end;function DecryptString(s: string): string;
var
Sk, Mk, Ak: Longint;
begin
Sk := StrToInt(Decrypt(Byte2CharStr(Copy(s, 1, 9)), StartKey, MultKey, AddKey));
Mk := StrToInt(Decrypt(Byte2CharStr(Copy(s, 10, 15)), StartKey, MultKey, AddKey));
Ak := StrToInt(Decrypt(Byte2CharStr(Copy(s, 25, 15)), StartKey, MultKey, AddKey));
Result := Decrypt(Byte2CharStr(Copy(s, 40, Length(s)-39)), Sk, Mk, Ak);
end;end.