簡體   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