public static void Main() { //CREATE AN INSTANCE OF ANOTHER CLASS TO ALLOW IT TO BE CALLED WITHIN THE CAESAR CLASS alterText alterText = new alterText(); //ALLOW THE USER TO CHOOSE WHETHER THEY WANT TO ENCRYPT OR DECRYPT DATA Console.WriteLine("Would you like to Decrypt or Encrypt data? \nPlease type \"1\" for Encyption \nPlease type \"2\" for Decryption"); //STORE THE USER'S INPUT AS A VARIABLE string userDecision = Convert.ToString(Console.ReadLine()); //TWO IF STATEMENTS AND AN ELSE STATEMENT TO ENSURE THE PROGRAM IS ABLE TO DEAL WITH ALL USER INPUTS if (userDecision == "1") { alterText.encryptText(); } else if (userDecision == "2") { alterText.decryptText(); } else { //CALLS THE MAIN FUNCTION IF THE USER ENTERS AN INVALID INPUT, SO THE PROGRAM WILL RE-ASK FOR AN INPUT Console.WriteLine("Please enter a valid input"); Main(); } }
//THREE VARIABLES ARE PASSED THROUGH AS ARGUMENTS public static void decryptAffine(string encryptedText, int a, int b) { string[] alphabetArray = 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" }; string plainText = ""; //CREATES THE MULTIPLICATATIVE INVERSE AS A VARIABLE BY CALLING A FUNCTION WHICH CALCULATES IT int modularMultiplicativeInverse = findInverse(a); foreach (char character in encryptedText) { if (character.ToString() == " ") { plainText += " "; continue; } else if (character.ToString() == "\r" || character.ToString() == "\r\n") { plainText += "\r\n"; continue; } else if (!(alphabetArray.Contains(character.ToString()))) { continue; } //CREATES A VARIABLE THAT STORES THE INDEX OF THE CURRENT DECRYPTED CHARACTER int index = Array.IndexOf(alphabetArray, character.ToString()); //USES THE DECRYPTION FORUMLA (TO REVERSE THE AFFINE ENCRYPTION) int newIndex = modularMultiplicativeInverse * (index - b); //CHECKS IF THE NEW, DECRYPTED INDEX (THAT WILL REPRESENT THE LETTER POSITION) IS LESS THAN 0 if (newIndex < 0) { //IF THE NEW INDEX IS LESS THAN 0, WE TAKE REMAINDER WHEN THE NEW INDEX HAS BEEN DIVIDED BY 26 (THE MODULUS) AWAY FROM 26 newIndex = 26 - (Math.Abs(newIndex) % 26); } else { //IF THE NEW INDEX IS NOT LESS THAN 0, THERE ARE NO PROBLEMS AND WE CAN SIMPLY PERFORM THE MODULUS OF 26 ON THE NEW INDEX newIndex = newIndex % 26; } plainText += alphabetArray[newIndex]; } Console.WriteLine("{0}", plainText); alterText alterText = new alterText(); alterText.writeToFile(plainText); }