繁体   English   中英

C# Affine Cipher 遇到字母 I 和 Z 时出错

[英]C# Affine Cipher errors when it encounters the letters I and Z

嘿,作为一个狂热的 Stack Overflow 用户,我现在必须发表我的第一篇文章,因为我最近遇到了一个问题,即字母 I 和 Z 没有被程序正确拾取并且现在没有被加密。 下面是代码,任何想法都会很棒。 也请无视乱码xD爱C#但讨厌整理代码。 无论如何谢谢你。

public static void mainMenu()
    {
        Console.Clear();
        Console.WriteLine("| Affine Cipher Program - Group 40 |");
        Console.WriteLine("*----------------------------------*");
        Console.WriteLine("\n\n 1) Encrypt \n 2) Decrypt \n 3) Crack");
        try
        {
            int menuEntry = Convert.ToInt32(Console.ReadLine());
            if (menuEntry == 1)
            {
                EncryptSettings();
            }
            else if (menuEntry == 2)
            {
                DecryptSettings();
            }
            else if (menuEntry == 3)
            {
                findKeySettings();
            }
        }
        catch
        {
            E0x0001("menuEntry");
        }
    }
    public static void EncryptSettings()
    {
        int[] valueA = new int[] { 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 };
        Console.Clear();
        Console.WriteLine("Please enter the plain text that you would like to encrypt");
        try
        {
            string pTextN = Console.ReadLine();
            string pText = String.Concat(pTextN.Where(c => !Char.IsWhiteSpace(c)));
            Console.WriteLine(pText);
            if (pText.Length == 0)
            {
                E0x0001("plainTextValue");
            }
            else
            {
                int num = 0;
                string[] chars = new string[] { "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("\nPlease select a value for Value A from the list below;");
                foreach (int n in valueA)
                {
                    Console.WriteLine(n);
                }
                int valA = Convert.ToInt32(Console.ReadLine());
                bool contNum = valueA.Contains(valA);
                if (contNum == true)
                {
                    Console.WriteLine("\nPlease enter any letter");
                    string valB = Console.ReadLine();
                    if(valB.Length > 0 && valB.Length < 2)
                    {
                        foreach (int chr1 in valB)
                        {
                            string c1 = Char.ConvertFromUtf32(chr1);
                            num = Array.IndexOf(chars, c1) + 1;
                            valB = char.ToUpper(valB[0]).ToString();
                        }
                        
                    }else
                    {
                        E0x0001("ValueB");
                    }
                    Console.WriteLine("The string " + pText + " will now be encrypted with the values " + valA + " and " + valB + ", Press any key to continue");
                    Console.ReadLine();
                    pText.ToUpper();
                    string[] cText = new string[] { };
                    foreach (int chr in pText)
                    {
                        Console.WriteLine("ASCII is :" + chr);
                        string c = Char.ConvertFromUtf32(chr);
                        c = char.ToUpper(c[0]).ToString();
                        Console.WriteLine("Letter is: " + c);
                        Console.WriteLine($"Index - {Array.IndexOf(chars, c) + 1}");
                        int workable = Array.IndexOf(chars, c) + 1;
                        //int final = workable * valA + num  % 26;
                        int final = workable * valA;
                        final = final + num;
                        final = final % 26 - 2;
                        
                        Console.WriteLine("Number: " + final + " Letter: " + chars.GetValue(final) + "\n");
                    }
                    Console.ReadLine();

                    //maths notes//
                    //Encryption
                    //cText == (pText * valA) + valB
                    //Decryption
                    //pText == (cText - valB) * valA^-1

                }
                else
                {
                    E0x0001("ValueA");
                }
            }
        }
        catch
        {
            E0x0001("Value");
        }

    }

我一直在使用的输入案例是主输入为“Missing”,用户输入为 3 和 a。 这会产生错误,在控制台中显示以下内容; 抛出异常:mscorlib.dll 中的“System.IndexOutOfRangeException”。 在深入研究该程序并获得一些非常有用的评论后,我发现当此异常被称为“数组边界外的索引”时,也会显示该异常,并且进一步研究后发现 HResult 为 -2146233080。 这只会让我在看的时候更加困惑。 最后被调用的是 arrays; int[] valueAstring[] chars 最后一件事,实际程序在Console.WriteLine("Number: " + final + " Letter: " + chars.GetValue(final) + "\n");上失败 . 任何帮助是极大的赞赏!

!!!更新!!!

我的朋友指出了修复程序的代码中的一个问题,我需要更改添加数组值的方式。 代码现在可以工作了! 如果人们好奇,我会发布更新的代码,但感谢您的所有帮助!

好吧,所有的 try catch 都对你没有帮助,因为它们隐藏了实际的错误,除非你将它们完全打印出来。

您的代码在这里失败

    int final = workable * valA;
   final = final + num;
    final = final % 26 - 2;

    Console.WriteLine("Number: " + final + " Letter: " + chars.GetValue(final) + "\n");

因为在处理“Z”时最终结果为 -2(char.GetValue 需要一个正整数)

  • 可行的是26
  • valA 是 19(我是在提示时输入的)
  • 数字是 26

我不知道你的算法是如何工作的,所以不能建议修复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM