凯撒密码C#-如何正确解密

凯撒密码C#-如何正确解密,c#,visual-studio-2019,caesar-cipher,C#,Visual Studio 2019,Caesar Cipher,当我输入一个带有“a”的字符串,并使用-1作为移位时,它返回符号。如何修复解密 using System; namespace CaesarCipher1 { class Program { static string Encrypt(string value, int shift) { char[] buffer = value.ToCharArray(); for (int i = 0; i &

当我输入一个带有“a”的字符串,并使用-1作为移位时,它返回符号。如何修复解密

using System;

namespace CaesarCipher1
{
    class Program
    {
        static string Encrypt(string value, int shift)
        {
            char[] buffer = value.ToCharArray();
            for (int i = 0; i < buffer.Length; i++)
            {
                char letter = buffer[i];
                letter = (char)(letter + shift);
                if (letter > 'z')
                {
                    letter = (char)(letter - 26);
                }
                else if (letter < 'a')
                {
                    letter = (char)(letter + 26);
                }
                // Store.
                buffer[i] = letter;
            }
            return new string(buffer);
        }
        static string Decrypt(string value, int shift)
        {
            return Encrypt(value, 26 - shift);
        }


        static void Main(string[] args)
        {
            bool Continue = true;

            Console.WriteLine("      Ceasar Cipher");
            Console.WriteLine("-------------------------\n");

            while (Continue)
            {
                try
                {
                    Console.WriteLine("\nType a string to encrypt:");
                    string UserString = Console.ReadLine();

                    Console.Write("\nShift: ");
                    int key = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine("\nEncrypted Data: ");

                    string cipherText = Encrypt(UserString, key);
                    Console.WriteLine(cipherText);
                    Console.Write("\n");

                    Console.WriteLine("Decrypted Data:");

                    string t = Decrypt(cipherText, key);
                    Console.WriteLine(t);

                    Console.WriteLine("\nDo you want to continue?");
                    Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
                    string response = Console.ReadLine();
                    Continue = (response == "Yes");

                }
                catch (FormatException ex)
                {
                    Console.WriteLine("You entered a bad operation, try another one");
                }
            }

        }
    }
}
使用系统;
名称空间密码1
{
班级计划
{
静态字符串加密(字符串值,int移位)
{
char[]buffer=value.ToCharArray();
for(int i=0;i'z')
{
字母=(字符)(字母-26);
}
否则如果(字母<'a')
{
字母=(字符)(字母+26);
}
//商店。
缓冲区[i]=字母;
}
返回新字符串(缓冲区);
}
静态字符串解密(字符串值,int移位)
{
返回Encrypt(值,26-shift);
}
静态void Main(字符串[]参数)
{
bool Continue=true;
控制台写入线(“Ceasar密码”);
Console.WriteLine(“---------------------------\n”);
while(继续)
{
尝试
{
Console.WriteLine(“\n键入要加密的字符串:”);
string UserString=Console.ReadLine();
控制台。写入(“\n移位:”);
int key=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“\n加密数据:”);
字符串密文=加密(用户字符串,密钥);
控制台写入线(密文);
控制台。写入(“\n”);
Console.WriteLine(“解密数据:”);
字符串t=解密(密文,密钥);
控制台写入线(t);
Console.WriteLine(“\n要继续吗?”);
WriteLine(“输入Yes继续,或按任何其他键,然后按enter退出:”;
字符串响应=Console.ReadLine();
继续=(响应=“是”);
}
捕获(格式化异常)
{
WriteLine(“您输入了一个错误的操作,请尝试另一个”);
}
}
}
}
}
凯萨密码 键入要加密的字符串: 表演

班次:-1

加密数据: zbs

解密数据: {ct

你想继续吗? 输入Yes继续,或按任何其他键,然后按enter退出: 对

键入要加密的字符串: 表演

班次:1

加密数据: bdu

解密数据:
采取行动

因为你用代码纠正超出范围的字母,比如
字母=(char)(字母-26);
你必须采取措施确保你的移位量不会太大。 } 我会将这一行添加到
Encrypt
函数的开头

shift %= 26;
这将确保您的班次永远不会超过26

static string Encrypt(string value, int shift)
{
    shift %= 26; // add this line
    char[] buffer = value.ToCharArray();
    for (int i = 0; i < buffer.Length; i++)

    // ...
静态字符串加密(字符串值,int移位)
{
shift%=26;//添加此行
char[]buffer=value.ToCharArray();
for(int i=0;i
如果移位为负数,则希望使用移位的绝对值进行解密,而不是
26-shift