加密解密函数如下:
function StrHexExchange(str:String):String;
var
i,l:integer;
k,t:byte;
begin
result:='';
l:=length(str);
for i:=1 to l do
  begin
  t:=ord(str[i]);
  k:=(t shl 4) or (t shr 4); //每个字符的高低位进行交换,再交换一次还还原了!  result:=result+chr(k);
  end;
end;
但在加密解密测试中发现有部分中文字出现乱码,如'存款'、'取款'希望高手们赐教

解决方案 »

  1.   

    k:=(t shl 4) or (t shr 4); 改为k:=((t shl 4) and $f0) or ((t shr 4) and $f); 试试
      

  2.   

    我是用TStringList.LoadFromFile 及 TStringList.SaveToFile来装入数据及写出到文本文件的
      

  3.   

    会不会问题出在TStringList的LoadFromFile 及 SaveToFile ????
    能不用其它方式导入内存及写入磁盘????
      

  4.   

    将ord(str[i])改为byte(Str[I])试试另外给你一个公用的加密解密函数
    //字符串加密函数
    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;