簡體   English   中英

.NET 到 Delphi(string.Substring 函數)

[英].NET to Delphi (string.Substring function)

我正在嘗試將 .NET 函數移植到 Delphi。

這是 .NET 函數:

public static string xorEncrypt(string sA, int32 displace)
{
    displace = Math.Abs((((((displace * 82) + (displace / 3)) + (displace % 322)) - (displace % 17)) - ((displace * 7) % 811)));
    string str1 = System.String.Empty;
    string str2 = RWCustom.Custom.EncryptionString;
    int num1 = 0;
    while (num1 < sA.Length) 
    {
        str1 = str1 + (char)((sA.Chars(num1) ^ str2.Chars(((num1 + displace) % str2.Length))));
        num1 = (num1 + 1);
    }
    return str1;
}

這是我的功能:

function xorEncrypt(sA: String; displace: Integer): String;
var
   str1: String;
   str2: String;
   num1: Integer;
begin
   Result := '';
   str1 := '';
   str2 := ''; // ?!?
   num1 := 0;
   displace := Abs((((((displace * 82) + (displace div 3)) + (displace mod 322)) - (displace mod 17)) - ((displace * 7) mod 811)));
   while num1 < sA.Length do
   begin
      str1 := str1 + Char(Ord(sA.Chars[num1]) xor Ord(str2.Chars[((num1 + displace) mod str2.Length)]));
      Inc(num1);
   end;
   Result := str1;
end;

所以,我已經有兩個問題:

  1. 字符串 str2 = RWCustom.Custom.EncryptionString;

     private string EncryptionString { get { return this.encrptString.Substring(54, 1447); } }

    encrptString 是一個字符串,但在 mscorlib 上查看 Substring 函數:

     public string Substring(int32 startIndex, int32 length) { return this.InternalSubStringWithChecks(startIndex, length, false); }

    和 InternalSubStringWithChecks 函數:

     internal string InternalSubStringWithChecks(int32 startIndex, int32 length, bool fAlwaysCopy) { int nt321 = this.Length; if (startIndex < 0) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex")); } if (startIndex > nt321) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndexLargerThanLength")); } if (length < 0) { throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NegativeLength")); } if (startIndex > (nt321 - length)) { throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_IndexLength")); } if (length! == 0) { return this.Empty; } return this.InternalSubString(startIndex, length, fAlwaysCopy); }

    我不知道在 Delphi 中應該如何。

  2. (displace / 3) 它是 .NET 上的整數除法? 我應該將我的代碼更改為 (displace / 3) 並將 displace 設置為 Variant 嗎?

注意到我對這種“加密”的保留意見后,讓我實際回答這個問題。

您有幾個不同的問題需要解決,其中第一個是您的字符串索引大多會偏離一個。 請記住,Delphi 字符串是從一開始的,而 C# 使用從零開始的字符串,因此您需要對此進行調整。 我做了一些,但只需檢查你看到的每個字符串索引。

第一個影響你的地方是你的EncryptionString函數:

property EncryptionString: string read GetEncryptionString;

function TMyClassNameHere.GetEncryptionString: string;
begin
  Result := Copy(encrptString, 55, 1447); // Was encrptString.Substring(54, 1447)
end;

因此,雖然長度保持不變,但您的字符串索引會向上移動 1。

然后,主要功能:

 class function TMyClassNameHere.xorEncrypt(sA: string; displace: Integer): string;
 var
   str1: string;
   str2: string;
   num1: Integer;
begin
  displace := Abs((((((displace * 82) + (displace / 3)) +
         (displace mod 322)) -
         (displace mod 17)) -
         ((displace * 7) mod 811)));
  str1 := System.String.Empty;
  str2 := RWCustom.Custom.EncryptionString;
  num1 := 1; // Was 0
  while (num1 <= sA.Length)     do
  begin
    str1 := str1 + Char[(sA[num1] xor str2[((num1 + displace) mod str2.Length)])];
    num1 := num1 + 1;
  end;
  Result := str1;
end;

我改成了類函數(因為原來是靜態的)。 此外, num1被初始化為 1 而不是 0。

我認為其余的計算應該沒問題,但我顯然無法測試,因為我不知道輸入是什么或它應該如何工作。 但這是我在這些限制范圍內提出的 Delphi 翻譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM