如何创建C#Caesar Cipher? [英] How do I create a C# Caesar Cipher?

查看:48
本文介绍了如何创建C#Caesar Cipher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中创建一个密码,我需要从文本文档中获取加密文本,并在控制台中显示解密并将正确的解码导出到文本文档。



这是我到目前为止:



I'm trying to create a cipher in C#, I need to take in the encrypted text from a text document and have the decryptions show up in the console and have the correct one export to a text document.

This is what I have so far:

using System;
using System.IO;

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

   	}
}





我现在卡住了,我把它放进了加密文本,但它不解密它。它只是在控制台中打印加密文本。任何帮助将不胜感激。



谢谢。



I'm stuck right now, I have it feed in the encrypted text but it doesn't decipher it. It just prints in the console the encrypted text. Any help would be appreciated.

Thanks.

推荐答案

试试这个 链接


首先提供已知文本:假设一个和文本HELLO

使用仅包含IFMMP的NotePad创建一个文本文件并保存。

在线上设置一个断点:

Start by feeding it "known" text: assume a shift of one and the text "HELLO"
Create a text file using NotePad that contains just "IFMMP" and save it.
Put a breakpoint on the line:
char character = decr[i];



并运行你的应用程序,将它提供给您刚创建的文本文件。

然后一次一行地执行您的程序,并查看到底发生了什么。

第一行应该获取加密的字符,下一个应该解密它 - 所以你显示能够在调试器中看到解密的字符。

应该立即明白问题是什么! :笑:



提示:你不需要走远就能发现问题......


And run your app, feeding it the text file you just created.
Then step through your program one line at a time, and look at exactly what is going on.
The first line should fetch the encrypted character, the next should decrypt it - so you shown be able to see the decrypted character in the debugger.
It should be immediately obvious what the problem is! :laugh:

Hint: you won't need to go far to spot the problem...


这是一个有点简单。使用mod处理界外数字:



This is a bit simpler. Uses mod to deal with numbers outside bounds:

private abstract class CharClass
{
    //default shift
    private static int _shift = 13;
    //chars that can be shifted
    private const string Alphabet = "abcdefghijklmnopqrstuvwxyz";

    //update the shift number
    public static int Shift
    {
        private get { return _shift; }
        set { _shift = value; }
    }

    //perform the shift per char
    public static char ShiftChar(char c)
    {
        var index = Alphabet.IndexOf(c);
        if (index == -1)
            return c;
        index = ((index + Shift) + Alphabet.Length) % Alphabet.Length;
        return Alphabet[index];
    }

    //perform the shift on a string
    public static string ShiftString(string s)
    {
        return new string(s.Select(ShiftChar).ToArray());
    }

}





用法:(好吧,我的测试方法)





usage: (well, my test method)

[TestMethod]
public void Test1()
{
    CharClass.Shift = 5;

    const string unEncrypted = "the quick brown fox jumps over the lazy dog";

    var encrypted = CharClass.ShiftString(unEncrypted);

    CharClass.Shift = -5;

    var unEncryptedAgain = CharClass.ShiftString(encrypted);

    Assert.IsFalse(unEncrypted.Equals(encrypted));
    Assert.IsFalse(encrypted.Equals(unEncryptedAgain));
    Assert.IsTrue(unEncrypted.Equals(unEncryptedAgain));
}





注意:

只有<$ c $中的字符c>字母将被移位。在这种情况下,大写字符不会,也不会是空格或特殊字符。



可以更新此处理大写,但我我相信你可以管理它^ _ ^



希望有所帮助

Andy



NB:
ONLY the chars in Alphabet will be shifted. uppercase chars will not in this case, neither will whitespace or special chars.

I could update this to handle upper case too, but I'm sure you can manage it ^_^

Hope that helps
Andy


这篇关于如何创建C#Caesar Cipher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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