Hey so as an avid Stack Overflow user I now have to make my first post that because i've been having an issue recently where the letters I and Z are not being picked up by the program correctly and are now not being encrypted. Below is the code, any ideas would be great. Also please ignore the messy code xD love C# but hate organising code. Anyway thank you.
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");
}
}
The input case i have been using is the main input as "Missing" and the user input as 3 and a. This produces and error that in the console states the following; Exception thrown: 'System.IndexOutOfRangeException' in mscorlib.dll. After some more digging into the program and some very helpful comments i've seen that when this exeption gets called 'Index outside the bounds of the array' is also shown and with some more digging saw the HResult was -2146233080. This only made me more confused while looking at it. Finally the called arrays are; int[] valueA
and string[] chars
. The final thing, the actual program fails on Console.WriteLine("Number: " + final + " Letter: " + chars.GetValue(final) + "\n");
. Any help is greatly appreciated!
!!!UPDATE!!!
My friend pointed out an issue in the code that fixed the program were i needed to change the way i added up the array value. The code now works! i will post the updated code if people are curious but thank you for all of your help!