凯撒密码 (C#)

Caesar Cipher (C#)

我先说这是一个评估,所以我不希望你直接给我答案,我希望你能指出正确的方向,稍微调整一下我所做的,或者只是告诉我应该考虑做什么。

我正在尝试创建凯撒密码来破译我们收到的文本文档。它需要在控制台中打印所有可能的班次,并将最后一个班次输出到文本文档。我正在考虑稍后尝试对此添加频率分析以找到正确的,但我现在遇到了一些问题,我需要先解决这个问题。

这是我目前所做的:

using System;
using System.IO;
class cipher

{
public static void Main(string[] args)
{

    string output = "";
    int shift;
    bool userright = false;
    string cipher = File.ReadAllText("decryptme.txt");

    char[] decr = cipher.ToCharArray();

    do {

        Console.WriteLine("How many times would you like to shift? (Between 0 and 26)");
        shift = Convert.ToInt32(Console.ReadLine());
        if (shift > 26) {
            Console.WriteLine("Over the limit");
            userright = false;
        }
        if (shift < 0) {
            Console.WriteLine("Under the limit");
            userright = false;
        }
        if (shift <= 26 && shift >= 0)
        {
            userright = true;
        }
    } while (userright == false);


    for (int i = 0; i < decr.Length; i++)
    {
        {
        char character = decr[i];

        character = (char)(character + shift);

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

        if (character > 'Z')
            character = (char)(character - 26);

        else if (character < 'A')
            character = (char)(character + 26);


          output = output + character;
        }

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

    StreamWriter file = new StreamWriter("decryptedtext.txt");
    file.WriteLine(output);
    file.Close();

}

}

现在它编译并读取文档,但是当它在控制台中运行时,它打印编码文本中的移位 1 作为 1 个字母,移位 2 作为编码文本的 2 个字母,等等。

我不知道自己做错了什么,如有任何帮助,我们将不胜感激。我也开始考虑字母的 ASCII 值,但不知道如何实现它。

再一次,请不要只给我答案,否则我不会从中学到任何东西 - 我一直试图自己破解这个但没有运气。

谢谢。

将问题分解成更小的小块。首先打印一条移位线,比如移位 1。

当您让该部分正常工作时(并且仅在那时)扩展您的代码以打印 26 行,班次为 0、1、2、3,... 26。我不确定您的导师是否想要或开头的 shift 0 和结尾的 shift 26。你需要问。

同样,让它正常工作,编写新代码来仅分析一行,并给它某种分数。让它正常工作。

现在计算所有行的分数并选出得分最高的行。这应该是正确的答案。如果不是,那么您需要检查您的评分方法。

对一个非常简单的启动程序编写小的增量更改通常比尝试从空白屏幕直接转到完整、复杂的程序要容易得多。逐渐增加复杂性,一次一件,边做边测试。