admin

[系统应用] 字符串秘钥加解密函数(keystr秘钥)

//字符串秘钥加解密函数(keystr秘钥)

function Crypt3(s, keystr: string; constbEncrypt: boolean = True): string;

//字符串转为16进制表达字符


  function MyStrToHex(AStr: string): string;

  var

    I, Len: Integer;

    s: char;

  begin

    Len := length(AStr);

    Result := '';

    for I := 1 to Len do

    begin

      s := AStr[I];

      Result := Result + IntToHex(Ord(s), 2); //将字符串转化为16进制字符串,

    end;

  end;


var

  i, L: DWord;

  b, k: word;

  mm: Integer;

  key: array of word;

begin

  Result := '';

  L := Length(s);

  if L < 1 then

    exit;


//变换处理秘钥字符串

  keystr := MyStrToHex(keystr);

  mm := (length(keystr) div 2) - 1;

  if mm < 0 then

  begin

    exit;

  end;

  setlength(key, mm + 1);

  for i := 0 to mm do

  begin

    key[i] := StrToInt('$' + copy(keystr, i * 2 + 1, 2));

  end;


  k := 0;

  if constbEncrypt then

//加密

    for i := 1 to L do

    begin

      b := word(s[i]) xor ord(key[k]);

      Result := Result + IntToHex(b, 4);

      inc(k);

      if k > mm then

        k := 0;

    end

  else

  begin

//还原

    k := 0;

    i := 0;

    while i < L div 4 do

    begin

      Result := Result + chr(StrToInt('$' + copy(s, i * 4 + 1, 4)) xor key[k]);

      inc(i);

      inc(k);

      if k > mm then

        k := 0;

    end;

  end;

end;


#1楼
发帖时间:3月前   |   查看数:0   |   回复数:0
游客组