求教,我刚学,请大神帮我看一下,这是我c#文件加密与解密程序,为啥运行的时候有时正常,有时出错

int Key, Key1;
while (true)
{
Console.WriteLine("加密请输入(1),解密请输入(2)");
int n = int.Parse(Console.ReadLine());
if (n == 1)
{
Console.WriteLine("---------------加密-------------");
Console.WriteLine("请输入密钥:");
Key = int.Parse(Console.ReadLine());
Console.WriteLine("请输入明文:");
string str = Console.ReadLine();
char[] p = str.ToCharArray();
for (int i = 0; i < p.Length; i++)
{
string ciphertext = Convert.ToString(Convert.ToChar(p[i] + Key % 26));
Console.WriteLine("加密后得到的密文为:{0}", ciphertext);
}
}
if (n == 2)
{
Console.WriteLine("---------------解密-------------");
Console.WriteLine("请输入密钥:");
Key1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入密文:");
string str1 = Console.ReadLine();
char[] c = str1.ToCharArray();
for (int j = 0; j < c.Length; j++)
{
string plaintext = Convert.ToString(Convert.ToChar(c[j] - Key1 % 26));
Console.WriteLine("解密后得到的明文:{0}", plaintext);
}
}
Console.WriteLine();
Console.WriteLine("---------------感谢使用--------------");
}

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int Key, Key1;
            while (true)
            {
                Console.WriteLine("加密请输入(1),解密请输入(2)");
                string n = Console.ReadLine();
                if (n == "1")
                {
                    Console.WriteLine("---------------加密-------------");
                    Console.WriteLine("请输入密钥:");
                    Key = int.Parse(Console.ReadLine());
                    Console.WriteLine("请输入明文:");
                    string str = Console.ReadLine();
                    char[] p = str.ToCharArray();
                    string ciphertext = "";
                    for (int i = 0; i < p.Length; i++)
                    {
                        ciphertext += Convert.ToString(Convert.ToChar(p[i] + Key % 26));
                    }
                    Console.WriteLine("加密后得到的密文为:{0}", ciphertext);
                }
                if (n == "2")
                {
                    Console.WriteLine("---------------解密-------------");
                    Console.WriteLine("请输入密钥:");
                    Key1 = int.Parse(Console.ReadLine());
                    Console.WriteLine("请输入密文:");
                    string str1 = Console.ReadLine();
                    char[] c = str1.ToCharArray();
                    string plaintext = "";
                    for (int j = 0; j < c.Length; j++)
                    {
                        plaintext += Convert.ToString(Convert.ToChar(c[j] - Key1 % 26));

                    }
                    Console.WriteLine("解密后得到的明文:{0}", plaintext);
                }
                if (n != "1" && n != "2")
                    break;
                Console.WriteLine();
                Console.WriteLine("---------------感谢使用--------------");
            }
        }
    }
}

给你修改了下,自己看吧。

建议你增加一些调试的输出,看看正确与错误是有什么不同。有可能是你输入出错了,或其它一些人为的原因引起的。

建议研究一下调试debug,这个是非常有用的,解决和分析问题的重要方法