C# 错误索引超出数组范围(Visual Studio、Winform、.NET Framework 4.6)
C# Error Index was outside the bounds of the array (Visual Studio, Winform, .NET Framework 4.6)
我有一个生成密钥的程序,但我总是遇到 "Index was outside the bounds of the array" 异常。
我的代码是这样的:
private string generate()
{
try
{
int block = 11;
int size = 0;
string content = "zQzhPeWFXZG53N2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo"; //this key gets normally decrypted with aes but that is not the problem
block = Convert.ToInt32(content.Split(':')[2]);
size = Convert.ToInt32(content.Split(':')[1]);
content = content.Split(':')[0];
Random r = new Random();
char[] blocked = new char[block];
for (int i = 0; i < block; i++)
{
blocked[i] = content[r.Next(content.Length)];
}
string key = "";
while (this.getValue(key, content, blocked) <= (size - (content.Length)))
{
key += content[r.Next(content.Length)];
}
key += content[size - this.getValue(key, content, blocked)];
key = new string(blocked) + key;
return key;
}
catch (Exception ex)
{
return ex.Message;
}
}
我认为错误出在 for 或 while 循环中,但奇怪的是,在我的电脑上它可以工作,但在我朋友的电脑上它不工作,我尝试了所有方法,但我不知道如何我可以适应这个错误。
有人知道解决这个问题的方法吗?
好吧,看来您为给定的 content
字符串拆分了错误的字符。你可以看看这个fiddle:https://dotnetfiddle.net/Ubklbk
使用您的原始字符串:
using System;
public class Program
{
public static void Main()
{
int block = 11;
int size = 0;
//string content = "zQzhPeWFXZG:5:3:2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo"; //this key gets normally decrypted with aes but that is not the problem
string content = "zQzhPeWFXZG532tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo"; //this key gets normally decrypted with aes but that is not the problem
string[] split =content.Split(':');
foreach(var item in split)
{
Console.WriteLine(item);
}
block = Convert.ToInt32(content.Split(':')[2]);
size = Convert.ToInt32(content.Split(':')[1]);
content = content.Split(':')[0];
Console.WriteLine(block);
Console.WriteLine(size);
Console.WriteLine(content);
}
}
输出:
zQzhPeWFXZG532tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo
Run-time exception (line 16): Index was outside the bounds of the array.
Stack Trace:
[System.IndexOutOfRangeException: Index was outside the bounds of the array.]
at Program.Main() :line 16
在下一个 运行 中,我添加了一个 :
分隔符,返回我的字段类型,得到以下输出:
zQzhPeWFXZG
5
3
2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo
3
5
zQzhPeWFXZG
所以基本上,问题在于您使用 :
分隔符拆分原始字符串的方式。
我有一个生成密钥的程序,但我总是遇到 "Index was outside the bounds of the array" 异常。
我的代码是这样的:
private string generate()
{
try
{
int block = 11;
int size = 0;
string content = "zQzhPeWFXZG53N2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo"; //this key gets normally decrypted with aes but that is not the problem
block = Convert.ToInt32(content.Split(':')[2]);
size = Convert.ToInt32(content.Split(':')[1]);
content = content.Split(':')[0];
Random r = new Random();
char[] blocked = new char[block];
for (int i = 0; i < block; i++)
{
blocked[i] = content[r.Next(content.Length)];
}
string key = "";
while (this.getValue(key, content, blocked) <= (size - (content.Length)))
{
key += content[r.Next(content.Length)];
}
key += content[size - this.getValue(key, content, blocked)];
key = new string(blocked) + key;
return key;
}
catch (Exception ex)
{
return ex.Message;
}
}
我认为错误出在 for 或 while 循环中,但奇怪的是,在我的电脑上它可以工作,但在我朋友的电脑上它不工作,我尝试了所有方法,但我不知道如何我可以适应这个错误。
有人知道解决这个问题的方法吗?
好吧,看来您为给定的 content
字符串拆分了错误的字符。你可以看看这个fiddle:https://dotnetfiddle.net/Ubklbk
使用您的原始字符串:
using System;
public class Program
{
public static void Main()
{
int block = 11;
int size = 0;
//string content = "zQzhPeWFXZG:5:3:2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo"; //this key gets normally decrypted with aes but that is not the problem
string content = "zQzhPeWFXZG532tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo"; //this key gets normally decrypted with aes but that is not the problem
string[] split =content.Split(':');
foreach(var item in split)
{
Console.WriteLine(item);
}
block = Convert.ToInt32(content.Split(':')[2]);
size = Convert.ToInt32(content.Split(':')[1]);
content = content.Split(':')[0];
Console.WriteLine(block);
Console.WriteLine(size);
Console.WriteLine(content);
}
}
输出:
zQzhPeWFXZG532tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo
Run-time exception (line 16): Index was outside the bounds of the array.
Stack Trace:
[System.IndexOutOfRangeException: Index was outside the bounds of the array.]
at Program.Main() :line 16
在下一个 运行 中,我添加了一个 :
分隔符,返回我的字段类型,得到以下输出:
zQzhPeWFXZG
5
3
2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo
3
5
zQzhPeWFXZG
所以基本上,问题在于您使用 :
分隔符拆分原始字符串的方式。