.NET到Delphi(string.Substring函数)

.NET到Delphi(string.Substring函数),.net,delphi,.net,Delphi,我正在尝试将.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.St

我正在尝试将.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;
public string Substring(int32 startIndex, int32 length)
{
    return this.InternalSubStringWithChecks(startIndex, length, false);
}
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);
}
property EncryptionString: string read GetEncryptionString;
 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;
encrptString是一个字符串,但查看mscorlib,子字符串函数:

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;
public string Substring(int32 startIndex, int32 length)
{
    return this.InternalSubStringWithChecks(startIndex, length, false);
}
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);
}
property EncryptionString: string read GetEncryptionString;
 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;
和InternalSubStringWithChecks函数:

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;
public string Substring(int32 startIndex, int32 length)
{
    return this.InternalSubStringWithChecks(startIndex, length, false);
}
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);
}
property EncryptionString: string read GetEncryptionString;
 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;
内部字符串InternalSubStringWithChecks(int32 startIndex、int32 length、bool Fallways)
{
int nt321=此长度;
如果(起始索引<0)
{
抛出新ArgumentOutOfRangeException(“startIndex”,Environment.GetResourceString(“ArgumentOutOfRange_startIndex”);
}
如果(起始索引>nt321)
{
抛出新ArgumentOutOfRangeException(“startIndex”,Environment.GetResourceString(“ArgumentOutOfRange\u StartIndexLargerThanLength”);
}
如果(长度<0)
{
抛出新ArgumentOutOfRangeException(“长度”,Environment.GetResourceString(“ArgumentOutOfRange_NegativeLength”);
}
如果(起始索引>(nt321-长度))
{
抛出新ArgumentOutOfRangeException(“长度”,Environment.GetResourceString(“ArgumentOutOfRange_IndexLength”);
}
如果(长度!==0)
{
还这个。空的;
}
返回这个.InternalSubString(startIndex,length,fallways);
}
我不知道这在德尔福是怎么回事

  • (置换/3)它是.NET上的整数除法?我应该将代码更改为(displace/3)并将displace设置为Variant吗


  • 注意到我对这种“加密”的保留,让我来回答这个问题

    您有一些不同的问题需要解决,其中第一个问题是字符串索引大部分都将被关闭一个。请记住,Delphi字符串是基于一的,而C#使用基于零的字符串,因此需要对此进行调整。我做了一些,但只需检查您看到的每个字符串索引

    影响您的第一个地方是
    EncryptionString
    函数:

    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;
    
    public string Substring(int32 startIndex, int32 length)
    {
        return this.InternalSubStringWithChecks(startIndex, length, false);
    }
    
    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);
    }
    
    property EncryptionString: string read GetEncryptionString;
    
     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;
    

    因此,当长度保持不变时,字符串索引向上移动1

    然后,主要功能:

    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;
    
    public string Substring(int32 startIndex, int32 length)
    {
        return this.InternalSubStringWithChecks(startIndex, length, false);
    }
    
    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);
    }
    
    property EncryptionString: string read GetEncryptionString;
    
     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;
    
    类函数TMyClassNameHere.xorEncrypt(sA:string;displace:Integer):string;
    变量
    str1:字符串;
    str2:字符串;
    num1:整数;
    开始
    置换:=Abs((((置换*82)+(置换/3))+
    (替换模块322))-
    (替换模块17))-
    ((替换*7)mod 811));
    str1:=System.String.Empty;
    str2:=RWCustom.Custom.EncryptionString;
    num1:=1;//是0
    
    虽然(num1整数除法是div。这个函数是个笑话。它不是真正的加密,UTF-16不好用。这个函数从哪里来,为什么要使用它。至于子字符串,你可以从文档中找到它的作用。我把它作为练习留给你。Delphi相当于.NET的<代码>String.Substring()是(基于1的索引)和(基于0的索引).NET使用基于0的字符串索引。@Deluto欢迎使用堆栈溢出。正如其他人所回避的那样,使用基于
    xor
    的算法根本不是加密,而且极易受到攻击。我不知道有Delphi包装器,但Windows API有一些非常强大的加密功能,您应该能够使用。H以下是Microsoft文档:@CobusKruger我没有尝试使用此函数加密文本,我正在尝试解密。这是对游戏引擎使用的本地化文件的加密。谢谢。看到您将EncryptionString设置为属性,我很清楚。我需要将加密文本加载到全局变量“encrptString”以前…我在.NET代码中的某个点上错过了。基于零的字符串也是我不知道的另一件事。回家后我会再试一次。再次感谢。