如何将将文件中的字符串加密,就像delphi中pas编译后的dcu文件,打开显示的是乱码.

解决方案 »

  1.   

      C1=52845; //字符串加密算法的公匙
      C2=22719; //字符串加密算法的公匙
    //字符串加密函数
    function Encrypt(const S: String; Key: Word): String;
    var
       I : Integer;
    begin
          Result := S;
          for I := 1 to Length(S) do
          begin
             Result[I] := char(byte(S[I]) xor (Key shr 8));
             Key := (byte(Result[I]) + Key) * C1 + C2;
             if Result[I] = Chr(0) then
                Result[I] := S[I];
          end;
          Result := StrToHex(Result);
    end;//字符串解密函数
    function Decrypt(const S: String; Key: Word): String;
    var
       I: Integer;
       S1: string;
    begin
       S1 := HexToStr(S);
       Result := S1;
       for I := 1 to Length(S1) do
       begin
          if char(byte(S1[I]) xor (Key shr 8)) = Chr(0) then
             begin
                Result[I] := S1[I];
                Key := (byte(Chr(0)) + Key) * C1 + C2; //保证Key的正确性  
             end
          else
             begin
                Result[I] := char(byte(S1[I]) xor (Key shr 8));
                Key := (byte(S1[I]) + Key) * C1 + C2;
             end;
       end;
    end;