C#console应用程序caesar cipher显示不正确的输出

时间:2015-12-01 16:22:39

标签: c# console-application caesar-cipher

我正在为一项任务制作凯撒密码加密器/解密器。我试图解密.TXT文件的内容。应用程序正在完美地读取.TXT文件的内容,但是,当显示解密文件时,每个班次只显示6个字符。我希望为.TXT文件的所有内容显示每个班次的每个可能结果(请参阅图片以了解问题。感谢先进的任何帮助。[1]:http://i.stack.imgur.com/9Dgz4.png

    static void decryption()
    {   Console.ForegroundColor = ConsoleColor.DarkBlue;
        Console.WriteLine ("\n*********************************** Decryption *********************************");
        Console.ResetColor();
        //pulls getPath from varables class
        string path = globalVars.getPath();


    string encrypted_text = path;     //String variable that contains [enter image description here][1]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);
    }
}

0 个答案:

没有答案