繁体   English   中英

如何修改基于Char的加密代码以与Unicode Delphi一起使用?

[英]How do I modify my Char based encryption code to work with Unicode Delphi?

我一直试图在Delphi XE2中使一些遗留的Delphi 2007代码工作。

Function EncryptionWithPassword(Str,Pwd: AnsiString; Encode: Boolean): AnsiString;
var
  a,PwdChk,Direction,ShiftVal,PasswordDigit : Integer;
begin
  PasswordDigit := 1;
  PwdChk := 0;
  for a := 1 to Length(Pwd) do Inc(PwdChk,Ord(Pwd[a]));
  Result := PChar(Str);
  If Encode then Direction := -1 else Direction := 1;
  for a := 1 to Length(Result) do
    begin
      if Length(Pwd)=0 then
        ShiftVal := a
      else
        ShiftVal := Ord(Pwd[PasswordDigit]);
      if Odd(A) then
        Result[A] := RotateBits(Result[A],-Direction*(ShiftVal+PwdChk))
      else
        Result[A] := RotateBits(Result[A],Direction*(ShiftVal+PwdChk));
      inc(PasswordDigit);
      if PasswordDigit > Length(Pwd) then PasswordDigit := 1;
    end;

end;

Function RotateBits(C: Char; Bits: Integer): Char;
var
  SI : Word; 
begin 
  Bits := Bits mod 8; 
  // Are we shifting left? 
  if Bits < 0 then 
    begin 
      // Put the data on the right half of a Word (2 bytes) 
      SI := MakeWord(Byte(C),0); 
      // Now shift it left the appropriate number of bits 
      SI := SI shl Abs(Bits);
    end
  else
    begin
      // Put the data on the left half of a Word (2 bytes)
      SI := MakeWord(0,Byte(C));
      // Now shift it right the appropriate number of bits
      SI := SI shr Abs(Bits);
    end;
  // Now OR the two halves together
  SI := Lo(SI) or Hi(SI);
  Result := Chr(SI);
end;

无论我尝试什么,该函数都会损坏字符串。 应用AnsiString作弊后,我尝试使用字符数组等,但没有任何效果。 请任何有见识的人可以帮助和解释-我不知所措,并且正在延迟一个庞大的项目。

只需将Char更改为AnsiChar,一切都会正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM