当我使用以下代码描述用户名和密码时,我得到一个错误,如startIndex不能大于字符串的长度


    public string DecryptValue(string strText)
    {
        string DecriptedValue ="";
        string String = strText.Substring(26); // This line fails
        int Count = String.Length - 5;
        string EncripEdText = String.Substring(0, Count);
        int TotalChar = EncripEdText.Length / 2;
        int J = 0;
        for (int i = 1; i <= TotalChar; i++)
        {
            string EnChar = EncripEdText.Substring(J, 2);
            string Decrypt = FindPos(EnChar);
            DecriptedValue = DecriptedValue + Decrypt;
            J = J + 2;
        }
        return DecriptedValue;
    }
    public static string FindPos(string EnChar)
    {
        string StringValue = "abcdefghijklmnopqrstuvwxyz0123456789_-.*@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string Value = StringValue.Substring(Convert.ToInt32(EnChar), 1);
        return Value;
    }

在接受子字符串之前检查参数长度:

 public string DecryptValue(string strText)
    {
        if (strText.Length<26)
        {
          MessageBox.Show("invalid argument length");
          return;
        }
        string DecriptedValue ="";
        string myString = strText.Substring(26); // This line fails
        int myCount = myString.Length - 5;
        ...
}

相关内容

最新更新