zoukankan      html  css  js  c++  java
  • Delphi字符串加密/解密

    unit uEncrypt_Decrypt;
     
    interface
     
    uses SysUtils;
     
    const XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47);
     
    //通过密钥Key加密
    function EncryptString(Source, Key: string): string;
    function UnEncryptString(Source, Key: string): string;
     
    //异或加密
    function Enc(str: string): string;
    function Dec(str: string): string;
     
     
    implementation
     
    function EncryptString(Source, Key: string): string;
    var
      KeyLen,KeyPos,Offset,SrcPos,SrcAsc,Range: integer;
      Dest: string;
    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(Source) do
      begin
        SrcAsc := (Ord(Source[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 UnEncryptString(Source, Key: string): string;
    var
      KeyLen,KeyPos,Offset,SrcPos,SrcAsc,TmpSrcAsc: integer;
      Dest: string;
    begin
      KeyLen := Length(Key);
      if KeyLen = 0 then
        Key := 'delphi';
      KeyPos := 0;
      Offset := strtoint('$' + copy(Source, 1, 2));
      SrcPos := 3;
      repeat
        SrcAsc := strtoint('$' + copy(Source, 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(Source);
      result := Dest;
    end;
     
    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;
     
    end.
     
    ============================================
  • 相关阅读:
    T-SQL 数据库数据的高级查询
    数据库 T-sql 基础语句
    数据库的定义、关系型数据库的四种约束。。
    linux上使用crontab任务调度
    pip list 显示出以下错误: DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] se
    python报错:NameError: name 'converter' is not defined
    python3报错:TypeError: can't concat bytes to str
    Fiddler如何手机抓包
    数据分析----VBA的使用
    Excel进行数据分析
  • 原文地址:https://www.cnblogs.com/lrl45/p/5135488.html
Copyright © 2011-2022 走看看