C#凯撒密码无法解密 [英] C# Caesar Cipher can't decipher

查看:167
本文介绍了C#凯撒密码无法解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个C#凯撒密码作为作业。我一直在尝试这样做一段时间,并且没有任何进展。



我现在遇到的问题是,而不是使用encrypted_text和解密它,它只是循环通过该字母表,忽略它开始的字符。应该是什么发生是要将encrypted_text和循环通过字母表改变每个字母一定的数字。



这是我到目前为止:

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

string encrypted_text =exxego;
string decoded_text =;
字符;
int shift = 0;
bool userright = false;

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

do
{

Console.WriteLine(你想转移多少次(0到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); (int i = 0; i< alphabet.Length; i ++)
{
decoded_text =;


foreach(charc in encrypted_text)
{
character = c;

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

shift = Array.IndexOf(字母,字符) - i;
if(shift< = 0)
shift = shift + 26;

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


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

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

}
}

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

解决方案

你的字母表包含大写字母,但您的输入完全是小写。您需要通过将所有输入投入同一案件或处理大写/小写字母来处理这种情况。


I am trying to make a C# Caesar Cipher for an assignment. I have been trying to do this for quite a while and am making no headway whatsoever.

The problem I am having now is that rather than use my encrypted_text and deciphering it, it just cycles through that alphabet, ignoring the character that it started on.What is supposed to happen is it is meant to take the encrypted_text and cycle through the alphabet changing each letter by a certain number.

This is what I have so far:

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();

    }
}

As you can see from my picture, I am getting close. I just need to be able to crack this. Any help would be greatly appreciated. Please forgive me if this is a simple question/solution, I am really new to this.

解决方案

Your alphabet contains upper case characters, but your input is fully in lower case. You need to handle this situation either by casting all input to same case, or handling both upper/lower case letters.

这篇关于C#凯撒密码无法解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆