简体   繁体   中英

C# Error Index was outside the bounds of the array (Visual Studio, Winform, .NET Framework 4.6)

I have a program which generates a key but I always get a "Index was outside the bounds of the array" exception.

My Code is this:

   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;
            }
        }

I think the error is in the for or in the while loop but the weir thing on it is, on my pc it is working but on my friends pc it is not working and I tried everything but I dont know how I can fit this error.

Does someone know any fix for this?

Well it seems that you are splitting on the wrong character for your given content string. You can check out this fiddle: https://dotnetfiddle.net/Ubklbk

With your original string:

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);
    }
}

Output:

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

In the next run, I added a : separator at points that returned me your field types which yielded me the following output:

zQzhPeWFXZG
5
3
2tGTGlvmQzTm9UTU91NG9DNndySERrOEsveFpMQ3ExUGlo
3
5
zQzhPeWFXZG

So basically, the problem lies in the way your are splitting your original string with the : separator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM