我一直试图让一些遗留的 Delphi 2007 代码在 Delphi XE2 中工作。
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 作弊后,我尝试使用字符数组等,但没有任何效果。请有一些见识的人可以提供帮助和解释-我束手无策,正在推迟一个巨大的项目。