c# - 凯撒密码移位(使用字母数组)

标签 c# arrays caesar-cipher

我目前正在用 C# 编写 Caesar Cipher 程序来完成我的作业,但我遇到了问题。

我正在使用一个存储整个字母表的数组来处理此任务,并声明一个由数组中的字符索引定义的 shift 变量 - for 循环的迭代。移位计算是在 foreach 循环中完成的,它从从文本文件读取的字符串中获取一个字符。 Foreach 循环包含在一个 for 循环中,该循环迭代以输出每个可能的移位。

但是,问题是当我尝试通过 shift 变量的值访问数组中的字符时,程序似乎没有访问我想要的字符,它只是输出与原始字符相同的字符字符串。

这是程序的代码:

using System; 
using System.IO;

public class caesar_shift
{
    public static void Main()
    {
        string file = @"C:\Users\terasss2\Desktop\Programming and Data Structures\caesarShiftEncodedText.txt";      //String variable that stores a file location
        string encrypted_text = File.ReadAllText(file);     //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
        string decoded_text = " ";
        int shift = 0;
        char character = '0';

        char[] alphabet = new char[26]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

        Console.WriteLine("The encrypted text is \n{0}", encrypted_text);       //Display the encrypted text

        for(int i = 0; i < alphabet.Length; i++)        //Start a loop which will display 25 different candidates of decipher
        {
            foreach(char c in encrypted_text)
            {
                character = c;

                if(character == '\'' || character == ' ')
                    continue;

                shift = Array.IndexOf(alphabet, character) - i;     //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable

                if(shift <= 0)
                    shift = shift + 26;

                if(shift >= 26)
                    shift = shift - 26;

                character = alphabet[shift];    //Set the character to a shifted letter by accessing the array element of a value shift

                Console.WriteLine(character);

                decoded_text = decoded_text + character; 
             }  

            Console.WriteLine("\nShift {0} \n {1}",i + 1, decoded_text);

         }
       }
}

最佳答案

我试了一下你的代码。下面给出解决办法,但要注意:只能用大写字母,因为上下图有区别。我使用了 ToUpper() 方法。对我来说很好用。我认为这就是您的问题所在。

public static void Main()
    {
        string encrypted_text = "BCD";     //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
        string decoded_text = " ";
        int shift = 0;
        char character = '0';
        encrypted_text = encrypted_text.ToUpper();

        char[] alphabet = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

        Console.WriteLine("The encrypted text is \n{0}", encrypted_text);       //Display the encrypted text

        for (int i = 0; i < alphabet.Length; i++)        //Start a loop which will display 25 different candidates of decipher
        {
            decoded_text = "";
            foreach (char c in encrypted_text)
            {
                character = c;

                if (character == '\'' || character == ' ')
                    continue;

                shift = Array.IndexOf(alphabet, character) - i;     //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable
                if (shift <= 0)
                    shift = shift + 26;

                if (shift >= 26)
                    shift = shift - 26;


                decoded_text += alphabet[shift];
            }
            Console.WriteLine("\nShift {0} \n {1}", i + 1, decoded_text);
        }
    }

关于c# - 凯撒密码移位(使用字母数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693247/

相关文章:

C 语言凯撒密码

c# - 在给定 HWND 的情况下如何获取窗口的子窗口?

c# - 浏览器如何显示从 C# Web 服务返回的 JSON

c# - 在 WPF 项目中包含 DLL 作为嵌入式资源

c# - MxParser 和 nuget

javascript - 通过属性和值获取数组对象项的索引

java - 对两个平行数组进行排序

c - 为什么我的输出是无穷大?你能找出程序中的错误吗?

python - 我无法让我的 python 加密程序正常工作