C# Caesar Shift decryption stops showing shifts af

2019-08-07 01:17发布

问题:

I am developing a Ceaser Cipher which decrypts the .TXT file attached. It's got to show all possible shifts and results in the decrytped text. But what I have got so far just shows about 8 shifts and each shift is getting longer with more text which is not meant to happen. Can someone help me identify the problem in the below code?

public class Program
{
    public static void Main(string[] args)
    {
        string text = System.IO.File.ReadAllText(@"FilePath");
        string file;
        int shift = 0;
        string decoded_text;

        char character = '0';

        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}", text); //Displays the encrypted text, shows that it gathered the text correctly from the fiile

        for (int i = 0; i < alphabet.Length; i++)
        {
            decoded_text = "";
            foreach (char c in text)
            {
                character = c;

                if (!alphabet.Contains(character))
                {
                    decoded_text += character;
                    continue;
                }

                shift = Array.IndexOf(alphabet, character) - i;
                if (shift <= 0) shift = shift + 26;

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

                text += alphabet[shift];
            }
            Console.WriteLine("\nShift {0} \n {1}", i + 1, text);
        }
    }
}

and this is the text contained in the .TXT file:

YMJNS HWJIN GQJQJ LFHDT KYMJR FYMJR FYNHF QLJSN ZXLJT WLJGT TQJBN QQGJJ CUQTW JINSF KWJJU ZGQNH YFQPN SMNXM TRJHN YDTKQ NSHTQ STSYM JGNHJ SYJSF WDTKM NXGNW YMXYT UGTTQ JBFXG TWSTS YMJXJ HTSIT KSTAJ RGJWJ NLMYJ JSKNK YJJSN SYTRT IJXYK FRNQD HNWHZ RXYFS HJXYM JXTST KFXMT JRFPJ WXYTU QFWLJ QDXJQ KYFZL MYMJB JSYTS YTGJH TRJTS JTKYM JBTWQ I’XKN SJXYR FYMJR FYNHN FSXBM TXJBT WPSTB KTWRX YMJGF XNXTK HTRUZ YJWXH NJSHJ FSIJQ JHYWT SNHHN WHZNY WDXYT UMJQF NIYMJ KTZSI FYNTS XTKYM JINLN YFQJW FFSIN XBNIJ QDWJH TLSNX JIFXY MJKTW JKFYM JWTKY MJINL NYFQF LJXYT UGTTQ JFSFQ LJGWF NXSTB FKZSI FRJSY FQFXU JHYTK RTIJW SRFYM JRFYN HXFSI TAJWY MJQFX YHJSY ZWDBF XZXJI YTKTW RYMJY MJTWJ YNHFQ KTZSI FYNTS TKRTI JWSHT RUZYJ WXHNJ SHJJS I