从网上整理的一些delphi字符串加密解密方法

时间:2022-09-06 14:31:38

function Encode(Str: string): string;

var //加密

TmpChr: AnsiChar;

i, Len: integer;

begin

Result := Str;

Len := Length(Result);

TmpChr := Result[1];

for i := 1 to Len - 1 do

Result[i] := Result[i + 1];

Result[Len] := TmpChr;

end;

function Decode(Str: string): string;

var //解密

TmpChr: AnsiChar;

i, Len: integer;

begin

Result := Str;

Len := Length(Result);

TmpChr := Result[Len];

for i := Len downto 2 do

Result[i] := Result[i - 1];

Result[1] := TmpChr;

end;

{异或 加密解密}

const // 首先定义一个常量数组

XorKey: array[0..7] of Byte = ($B2, $91, $AA, $55, $93, $6D, $84, $47); //字符串加密用

//数组的值可以自己改

function Enc(Str: string): string; //字符加密函数   这是用的一个异或加密

var

i, j: Integer;

begin

Result := '';

j := 0;

for i := 1 to Length(Str) do

begin

Result := Result + IntToHex(Byte(Str[i]) xor XorKey[j], 2);

j := (j + 1) mod 8;

end;

end;

function Dec(Str: string): string; //字符解密函数

var

i, j: Integer;

begin

Result := '';

j := 0;

for i := 1 to Length(Str) div 2 do

begin

Result := Result + Char(StrToInt('$' + Copy(Str, i * 2 - 1, 2)) xor XorKey[j]);

j := (j + 1) mod 8;

end;

end;

function Crypt(s: string; Key: Word;

const bEncrypt: boolean): string;

const

SeedA = 787; ///   常量,你可以修改

SeedB = 787; ///   常量,你可以修改

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;

///////////////////

function EncrypStr(Src, Key: string): string; //字符串加密函数

//对字符串加密(Src:源 Key:密匙)

var KeyLen: Integer;

KeyPos: Integer;

offset: Integer;

dest: string;

SrcPos: Integer;

SrcAsc: Integer;

Range: Integer;

begin

KeyLen := Length(Key);

if KeyLen = 0 then key := 'delphi';

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 UncrypStr(Src, Key: string): string; //字符串解密函数

//对字符串解密(Src:源 Key:密匙)

var KeyLen: Integer;

KeyPos: Integer;

offset: Integer;

dest: string;

SrcPos: Integer;

SrcAsc: Integer;

TmpSrcAsc: Integer;

begin

KeyLen := Length(Key);

if KeyLen = 0 then key := 'delphi';

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;

//********************************************************

//             加密解密 (利用异或运算)    (方式2)

//********************************************************

function encryptstr(const s: string; skey: string): string; //加密

var

i, j: integer;

hexS, hexskey, midS, tmpstr: string;

a, b, c: byte;

begin

hexS := myStrtoHex(s);

hexskey := myStrtoHex(skey);

midS := hexS;

for i := 1 to (length(hexskey) div 2) do

begin

if i <> 1 then midS := tmpstr;

tmpstr := '';

for j := 1 to (length(midS) div 2) do

begin

a := strtoint('$' + midS[2 * j - 1] + midS[2 * j]);

b := strtoint('$' + hexskey[2 * i - 1] + hexskey[2 * i]);

c := a xor b;

tmpstr := tmpstr + myStrtoHex(chr(c));

end;

end;

result := tmpstr;

end;

function decryptstr(const s: string; skey: string): string; //解密

var

i, j: integer;

hexS, hexskey, midS, tmpstr: string;

a, b, c: byte;

begin

hexS := s; //应该是该字符串

if length(hexS) mod 2 = 1 then

begin

showmessage('密文错误!');

exit;

end;

hexskey := myStrtoHex(skey);

tmpstr := hexS;

midS := hexS;

for i := (length(hexskey) div 2) downto 1 do

begin

if i <> (length(hexskey) div 2) then midS := tmpstr;

tmpstr := '';

for j := 1 to (length(midS) div 2) do

begin

a := strtoint('$' + midS[2 * j - 1] + midS[2 * j]);

b := strtoint('$' + hexskey[2 * i - 1] + hexskey[2 * i]);

c := a xor b;

tmpstr := tmpstr + myStrtoHex(chr(c));

end;

end;

result := myHextoStr(tmpstr);

end;

没有一个个去研究,调试。以后在时间在慢慢试。

(完)