the*_*mer -1 c# encryption indexoutofboundsexception
所以我有下面的代码使用orgTxt和创建一个加密的字符串rndTxt,当我在Visual Studio中调试代码时,我在第二个for循环中得到IndexOutOfRangeException的错误.
我用断点检查索引的值,它似乎完全在范围内,任何人都知道问题是什么?如果需要更多信息来帮助解决此错误,请留下评论.
//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;
}
Run Code Online (Sandbox Code Playgroud)
您正在scrtTxt从这行代码中读取数组.
scrtTxt += (char)(scrtTxt[i] - 'Z' + 'A' - 1);
Run Code Online (Sandbox Code Playgroud)
这是你想要做的,或者它应该是orgTxt或rndTxt?