So I have the code bellow to create an encrypted string using orgTxt and rndTxt, when I debug the code in Visual Studio I get an error of IndexOutOfRangeException in the second for loop.
I check the value of the index with breakpoints and it seems perfectly in range, anyone has an idea what the problem is? If more information needed to help solve this error just leave me comment please.
//variables
string scrtTxt = null;
string rndTxt = null;
string orgTxt = reader.ReadToEnd();
//assigning random a string from key (set of all capital letters) to rndTxt
for (int i = 0; i < fileInfo.Length; i++)
{
rndTxt += key[random.Next(0, key.Length)];
}
//generating the encrypted message scrtTxt
int j = 0;
for (int i = 0; i < fileInfo.Length; i++)
{
if ((orgTxt[i] + rndTxt[j] - 'A') <= 'Z' && (orgTxt[i] + rndTxt[j] - 'A') >= 'A')
scrtTxt += Convert.ToChar((orgTxt[i] + rndTxt[j] - 'A'));
if ((orgTxt[i] + rndTxt[j] - 'A') > 'Z')
scrtTxt += (char)(scrtTxt[i] - 'Z' + 'A' - 1);//IndexOutOfRangeException error here
j = j + 1 == rndTxt.Length ? 0 : j + 1;
}
scrtTxtmust be less thani+1characters long at that point. Double-check it.