C# 如何在Vigenere密码中不超过数组的边界?
在第4个字符后,我的程序崩溃,返回以下错误: 索引超出了数组的边界。在 System.String.get_字符(Int32索引)位于 Caesar_Cipher.Program.Main(字符串[]args) 我真的不知道如何修理它 代码如下:C# 如何在Vigenere密码中不超过数组的边界?,c#,encryption,cryptography,vigenere,C#,Encryption,Cryptography,Vigenere,在第4个字符后,我的程序崩溃,返回以下错误: 索引超出了数组的边界。在 System.String.get_字符(Int32索引)位于 Caesar_Cipher.Program.Main(字符串[]args) 我真的不知道如何修理它 代码如下: using System; using System.Collections.Generic; using System.Text; namespace vigenere_Cipher { class Program {
using System;
using System.Collections.Generic;
using System.Text;
namespace vigenere_Cipher {
class Program {
static void Main(string[] args) {
string text = string.Empty;
string key = string.Empty;
Console.Write("Enter the keyword: ");
key = Console.ReadLine();
for(int i = 0; i < key.Length; i++)
{
if(char.IsLetter(key[i]))
{
}
else
{
Console.WriteLine("a key must be alphabetical!");
}
}
Console.Write("Enter plaintext: ");
text = Console.ReadLine();
int counter = 0;
for(int j = 0; j <= text.Length; j++)
{
if(counter <= text.Length)
{
counter ++;
}
if(char.IsLetter(text[j]))
{
if(char.IsUpper(text[j]))
{
Console.Write("Encrypted string: " + Convert.ToChar(((text[j] + key[counter] -65)% 26) + 65));
}
if(char.IsLower(text[j]))
{
Console.Write("Encrypted string: " + Convert.ToChar(((text[j] + key[counter]-97)%26) + 97));
}
Console.Write("\n");
}
Console.ReadKey();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间vigenere_密码{
类程序{
静态void Main(字符串[]参数){
string text=string.Empty;
string key=string.Empty;
Console.Write(“输入关键字:”);
key=Console.ReadLine();
for(int i=0;i 对于(int j=0;C#中的j数组被索引为0…数组,长度-
,因此所有边界检查都应该像f(计数器)一样。我还建议学习如何调试程序,如果您循序渐进地遵循程序并检查变量值,则很容易发现错误。我添加了“-1”但它仍然不起作用,仍然以相同的错误使程序崩溃:/有两个地方存在此问题:在加密循环中设置计数器变量的方式和j
。