字符串加密解密方法

  

function Decrypt(Src: string; Key: string): string;
var
  KeyLen, KeyPos, Offset, SrcPos, SrcAsc, TmpSrcAsc: Integer;
  Dest: string;
begin
  KeyLen := Length(Key);
  if KeyLen = 0 then
    Key := cPasswordKey;
  KeyPos := 0;
  Offset := StrToInt('$' + Copy(Src, 1, 2));
  SrcPos := 3;
  while SrcPos < Length(Src) do
  begin
    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;
  end;
  Result := Dest;
end;


function Encrypt(Src: string; Key: string): string;
var
  KeyLen, KeyPos, Offset, SrcPos, SrcAsc: Integer;
  Dest: string;
begin
  KeyLen := Length(Key);
  if KeyLen = 0 then
    Key := cPasswordKey;
  KeyPos := 0;
  Randomize;
  Offset := Random(256);
  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;

原文地址:https://www.cnblogs.com/hnxxcxg/p/2940597.html