【问题标题】:C# Caesar Cipher can't decipherC# Caesar Cipher 无法破译
【发布时间】:2015-12-16 02:42:34
【问题描述】:

我正在尝试为作业制作 C# Caesar Cipher。我一直在尝试这样做很长一段时间,但没有取得任何进展。

我现在遇到的问题是,而不是使用我的 encrypted_text 并对其进行解密,它只是 cycles through that alphabet, ignoring the character that it started on.应该发生的事情是它意味着采用 encrypted_text 并循环遍历字母表,将每个字母更改为某个数字.

这是我目前所拥有的:

using System;
using System.IO;
class cipher
{
    public static void Main(string[] args)
    {

        string encrypted_text = "exxego";
        string decoded_text = "";
        char character;
        int shift = 0;
        bool userright = false;

        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' };

        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 < alphabet.Length; i++)
        {
            decoded_text = "";
            foreach (char c in encrypted_text)
            {
                character = c;

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

                shift = Array.IndexOf(alphabet, character) - i;
                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);
        }

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

    }
}

正如你从我的照片中看到的,我越来越近了。我只需要能够破解这个。任何帮助将不胜感激。如果这是一个简单的问题/解决方案,请原谅我,我对此真的很陌生。

【问题讨论】:

  • 我很抱歉,但我忍不住要交织一个双关语:你不能去死...
  • 只是好奇这个类是什么和在哪里,这不是今天的第一个实例。这里是other question
  • 这是林肯大学。那个人和我做的评价一模一样……小世界!

标签: c# encryption caesar-cipher


【解决方案1】:

您的字母表包含大写字符,但您的输入完全是小写。您需要通过将所有输入转换为相同大小写或同时处理大写/小写字母来处理这种情况。

【讨论】:

  • 谢谢!我被愚蠢地困在这个问题上的时间比我愿意承认的要长。如果可以的话,我会在 3 分钟内接受你的回答。再次感谢!
  • 我也建议你不要做这样的条件: if (shift
  • 更好的建议,谢谢。你帮了大忙!
  • 与 Stodgy 上的另一个问题非常相似,一直到 if (shift &lt;= 0) shift = shift + 26;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多