当使用凯撒解密程序时,很多“U”都会得到输出.C#.

本文关键字:输出 很多 凯撒 解密 程序 | 更新日期: 2023-09-27 18:20:40

using System;
class Decrypter 
{
static void Main ( string [] args )
{ 
    //The encrypted data is read from a file to a string, this 
    string encryptedData = System.IO.File.ReadAllText(@"C:'Users'TomTower'Desktop'Programming and Data Structures'Assessment 1'EncryptedText.txt");
    char[] alphabet = {'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'};
    // The code is supposed to take every character in an input string and left shift them back a 5 places. Y becomes T. B becomes W
    foreach(char c in encryptedData)
    {
        if (c == ' ')
            Console.Write(" ");
            else
            {
                int charPosition = 0;
                charPosition = Array.IndexOf(alphabet, c);
                charPosition = charPosition - 5;
                if (charPosition < 0)
                {    
                charPosition = charPosition + 26;
                }
                else;
                {
                Console.Write(alphabet[charPosition]);
                }
            };

输入数据如下:YMJNS HWJIN GQJQJ LFHDTKYMJR FYMJR FYNHF QLJSNZXLJT WLJGT TQJBN QQGJJCUQTW JINSF KWJJU ZGQNHYFQPN SMNXM TRJHN YDTKQNSHTQ STSYM JGNHJ SYJSFWDTKM NXGNW YMXYT UGTTQJBFXG TWSTS YMJXJ HTSITKSTAJ RGJWJ NLMYJ JSKNKYJJSN SYTRT IJXYK FRNQDHNWHZ RXYFS HJXYM JXTSTKFXMT JRFPJ WXYTU QFWLJQDXJQ KYFZL MYMJB JSYTSYTGJH TRJTS JTKYM JBTWQI'XKN SJXYR FYMJR FYNHNFSXBM TXJBT WPSTB KTWRXYMJGF XNXTK HTRUZ YJWXHNJSHJ FSIJQ JHYWT SNHHNWHZNY WDXYT UMJQF NIYMJKTZSI FYNTS XTKYM JINLNYFQJW FFSIN XBNIJ QDWJHTLSNX JIFXY MJKTW JKFYMJWTKY MJINL NYFQF LJXYTUGTTQ JFSFQ LJGWF NXSTBFKZSI FRJSY FQFXU JHYTKRTIJW SRFYM JRFYN HXFSITAJWY MJQFX YHJSY ZWDBFXZXJI YTKTW RYMJY MJTWJYNHFQ KTZSI FYNTS TKRTIJWSHT RUZYJ WXHNJ SHJJS我

我的输出如下所示:http://puu.sh/layx0/27e50b70a9.png

当使用凯撒解密程序时,很多“U”都会得到输出.C#.

如果文件中的文本包含不匹配的数据,则 Array.IndexOf 将返回 -1。 然后通过加 26 进行调整时,输出为"U"。

保护不良数据:

    foreach (char c in encryptedData)
    {
        if (c == ' ')
        {
            Console.Write(" ");
        }
        else
        {
            int charPosition = 0;
            charPosition = Array.IndexOf(alphabet, c);
            if (charPosition == -1)
            {//Check for non expected items
                Console.Write("*");
            }
            else
            {
                charPosition = charPosition - 5;
                if (charPosition < 0)
                {
                    charPosition = charPosition + 26;
                }
                Console.Write(alphabet[charPosition]);
            }
        }
    }