using System; using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic // Skeleton Program code for the AQA COMP1 Summer 2013 examination // this code should be used in conjunction with the Preliminary Material // written by the AQA COMP1 Programmer Team // developed in the Visual Studio 2008 (Console Mode) programming environment (VB.NET) // VB converted to C# using https://codeconverter.icsharpcode.net/ // Console.ReadLine statements updated appropriately namespace SecretMessagesCSharp { class Program { static void Main(string[] args) { string Ciphertext; string Plaintext; char Choice; int AmountToShift; var StartPosition = default(int); var EndPosition = default(int); int SizeOfRailFence; int N; Ciphertext = ""; Plaintext = ""; do { DisplayMenu(); Choice = GetMenuChoice(); switch (Choice) { case 'a': { Plaintext = GetTextFromUser(); break; } case 'b': { DisplayPlaintext(Plaintext); break; } case 'd': { Ciphertext = GetTextFromUser(); break; } case 'e': { DisplayCiphertext(Ciphertext); break; } case 'g': { DisplayPlaintext(Plaintext); AmountToShift = GetKeyForCaesarCipher(); Ciphertext = UseCaesarCipher(Plaintext, AmountToShift); DisplayCiphertext(Ciphertext); break; } case 'h': { DisplayPlaintext(Plaintext); SizeOfRailFence = GetSizeOfRailFence(); Ciphertext = EncryptUsingRailFence(Plaintext, SizeOfRailFence); DisplayCiphertext(Ciphertext); break; } case 'j': { DisplayCiphertext(Ciphertext); AmountToShift = -GetKeyForCaesarCipher(); Plaintext = UseCaesarCipher(Ciphertext, AmountToShift); DisplayPlaintext(Plaintext); break; } case 'k': { DisplayCiphertext(Ciphertext); SizeOfRailFence = GetSizeOfRailFence(); Plaintext = DecryptUsingRailFence(Ciphertext, SizeOfRailFence); DisplayPlaintext(Plaintext); break; } case 'n': { GetPositionsToUse(ref StartPosition, ref EndPosition); N = GetValueForN(); Plaintext = EveryNthCharacterSteganography(StartPosition, EndPosition, N); DisplayPlaintext(Plaintext); break; } } if (Conversions.ToString(Choice) != "q") { Console.WriteLine("Press enter key to continue"); Console.ReadLine(); } } while (Conversions.ToString(Choice) != "q"); } public static void DisplayMenu() { Console.WriteLine(); Console.WriteLine("PLAINTEXT OPTIONS"); Console.WriteLine(" a. Get plaintext from user"); Console.WriteLine(" b. Display plaintext"); Console.WriteLine("CIPHERTEXT OPTIONS"); Console.WriteLine(" d. Get ciphertext from user"); Console.WriteLine(" e. Display ciphertext"); Console.WriteLine("ENCRYPT"); Console.WriteLine(" g. Caesar cipher"); Console.WriteLine(" h. Rail fence"); Console.WriteLine("DECRYPT"); Console.WriteLine(" j. Caesar cipher"); Console.WriteLine(" k. Rail fence"); Console.WriteLine("STEGANOGRAPHY"); Console.WriteLine(" n. nth character (text from file)"); Console.WriteLine(); Console.Write("Please select an option from the above list (or enter q to quit): "); } public static char GetMenuChoice() { char GetMenuChoiceRet = default; char MenuChoice; MenuChoice = char.Parse(Console.ReadLine()); Console.WriteLine(); GetMenuChoiceRet = MenuChoice; return GetMenuChoiceRet; } public static string GetTextFromUser() { string GetTextFromUserRet = default; string TextFromUser; Console.Write("Please enter the text to use: "); TextFromUser = Console.ReadLine(); GetTextFromUserRet = TextFromUser; return GetTextFromUserRet; } public static void GetPositionsToUse(ref int StartPosition, ref int EndPosition) { Console.Write("Please enter the start position to use in the file: "); StartPosition = int.Parse(Console.ReadLine()); Console.Write("Please enter the end position to use in the file: "); EndPosition = int.Parse(Console.ReadLine()); } public static string GetTextFromFile(int StartPosition, int EndPosition) { string GetTextFromFileRet = default; var CharacterFromFile = default(char); string TextFromFile; int Count; FileSystem.FileOpen(1, "diary.txt", OpenMode.Binary); var loopTo = StartPosition - 1; for (Count = 1; Count <= loopTo; Count++) FileSystem.FileGet(1, ref CharacterFromFile); TextFromFile = ""; var loopTo1 = EndPosition; for (Count = StartPosition; Count <= loopTo1; Count++) { FileSystem.FileGet(1, ref CharacterFromFile); TextFromFile = TextFromFile + CharacterFromFile; } FileSystem.FileClose(1); GetTextFromFileRet = TextFromFile; return GetTextFromFileRet; } public static int GetKeyForCaesarCipher() { int GetKeyForCaesarCipherRet = default; int Key; Console.Write("Enter the amount that shifts the plaintext alphabet to the ciphertext alphabet: "); Key = int.Parse(Console.ReadLine()); GetKeyForCaesarCipherRet = Key; return GetKeyForCaesarCipherRet; } public static string GetTypeOfCharacter(int ASCIICode) { string GetTypeOfCharacterRet = default; string TypeOfCharacter; if (ASCIICode >= Strings.Asc("A") & ASCIICode <= Strings.Asc("Z")) { TypeOfCharacter = "Upper"; } else if (ASCIICode >= Strings.Asc("a") & ASCIICode <= Strings.Asc("z")) { TypeOfCharacter = "Lower"; } else { TypeOfCharacter = "Other"; } GetTypeOfCharacterRet = TypeOfCharacter; return GetTypeOfCharacterRet; } public static int ApplyShiftToASCIICodeForCharacter(int ASCIICode, int AmountToShift) { int ApplyShiftToASCIICodeForCharacterRet = default; int NewASCIICode; string TypeOfCharacter; TypeOfCharacter = GetTypeOfCharacter(ASCIICode); if (TypeOfCharacter != "Other") { if (TypeOfCharacter == "Upper") { NewASCIICode = (26 + ASCIICode - Strings.Asc("A") + AmountToShift) % 26 + Strings.Asc("A"); } else { NewASCIICode = (26 + ASCIICode - Strings.Asc("a") + AmountToShift) % 26 + Strings.Asc("a"); } } else { NewASCIICode = ASCIICode; } ApplyShiftToASCIICodeForCharacterRet = NewASCIICode; return ApplyShiftToASCIICodeForCharacterRet; } public static string UseCaesarCipher(string OriginalText, int AmountToShift) { string UseCaesarCipherRet = default; string ChangedText; int Count; int ASCIICode; ChangedText = ""; var loopTo = OriginalText.Length - 1; for (Count = 0; Count <= loopTo; Count++) { ASCIICode = Strings.Asc(OriginalText[Count]); ASCIICode = ApplyShiftToASCIICodeForCharacter(ASCIICode, AmountToShift); ChangedText = ChangedText + (char)ASCIICode; } UseCaesarCipherRet = ChangedText; return UseCaesarCipherRet; } public static int GetSizeOfRailFence() { int GetSizeOfRailFenceRet = default; int SizeOfRailFence; Console.Write("Enter the number of lines in the rail fence: "); SizeOfRailFence = int.Parse(Console.ReadLine()); GetSizeOfRailFenceRet = SizeOfRailFence; return GetSizeOfRailFenceRet; } public static string EncryptUsingRailFence(string OriginalText, int SizeOfRailFence) { string EncryptUsingRailFenceRet = default; int Count1; int Count2; string Ciphertext; Ciphertext = ""; var loopTo = SizeOfRailFence; for (Count1 = 1; Count1 <= loopTo; Count1++) { Count2 = Count1 - 1; while (Count2 < OriginalText.Length) { Ciphertext = Ciphertext + OriginalText[Count2]; Count2 = Count2 + SizeOfRailFence; } } EncryptUsingRailFenceRet = Ciphertext; return EncryptUsingRailFenceRet; } public static string DecryptUsingRailFence(string Ciphertext, int SizeOfRailFence) { string DecryptUsingRailFenceRet = default; string Plaintext; int NoOfColumns; int NoOfRows; int NoOfCiphertextCharacters; int NoOfCiphertextCharactersProcessed; int i; int j; int PositionOfNextCharacter; int LastFullRowNo; int AmountToReduceNoOfColumnsTimesjBy; int BeginningOfNextRowIndex; Plaintext = ""; NoOfCiphertextCharacters = Ciphertext.Length; // The ciphertext was created from a visualisation of the plaintext as // a two-dimensional grid of characters with no of rows = size of rail fence NoOfRows = SizeOfRailFence; NoOfColumns = NoOfCiphertextCharacters / SizeOfRailFence; // If NoOfCiphertextCharacters divides exactly all rows will be full // otherwise the last column will be incomplete and NoOfColumns will not include last column if (NoOfCiphertextCharacters % SizeOfRailFence != 0) { NoOfColumns = NoOfColumns + 1; } // Calculate row no of last full row, 0 means every row full LastFullRowNo = NoOfCiphertextCharacters % SizeOfRailFence; NoOfCiphertextCharactersProcessed = 0; var loopTo = NoOfColumns; for (i = 1; i <= loopTo; i++) // Work along the columns building the plaintext a column at a time { AmountToReduceNoOfColumnsTimesjBy = 0; var loopTo1 = NoOfRows - 1; for (j = 0; j <= loopTo1; j++) // Work down the rows building the plaintext { if (LastFullRowNo != 0) // Last column doesn't have a character in every row { if (j > LastFullRowNo) // There are shorter rows to skip { AmountToReduceNoOfColumnsTimesjBy = AmountToReduceNoOfColumnsTimesjBy + 1; } } // NoOfColumns * j locates in ciphertext beginning of each row BeginningOfNextRowIndex = NoOfColumns * j - AmountToReduceNoOfColumnsTimesjBy; PositionOfNextCharacter = BeginningOfNextRowIndex + i - 1; NoOfCiphertextCharactersProcessed = NoOfCiphertextCharactersProcessed + 1; if (NoOfCiphertextCharactersProcessed <= NoOfCiphertextCharacters) { Plaintext = Plaintext + Conversions.ToString(Ciphertext[PositionOfNextCharacter]); } } } DecryptUsingRailFenceRet = Plaintext; return DecryptUsingRailFenceRet; } public static int GetValueForN() { int GetValueForNRet = default; int N; Console.Write("Enter the value of n: "); N = int.Parse(Console.ReadLine()); GetValueForNRet = N; return GetValueForNRet; } public static string EveryNthCharacterSteganography(int StartPosition, int EndPosition, int N) { string EveryNthCharacterSteganographyRet = default; string HiddenMessage; int CurrentPosition; CurrentPosition = StartPosition; HiddenMessage = ""; while (CurrentPosition <= EndPosition) { HiddenMessage = HiddenMessage + GetTextFromFile(CurrentPosition, CurrentPosition); CurrentPosition = CurrentPosition + N; } EveryNthCharacterSteganographyRet = HiddenMessage; return EveryNthCharacterSteganographyRet; } public static void DisplayPlaintext(string TextToDisplay) { Console.WriteLine(); Console.Write("The plaintext is: "); Console.WriteLine(TextToDisplay); } public static void DisplayCiphertext(string TextToDisplay) { Console.WriteLine(); Console.Write("The ciphertext is: "); Console.WriteLine(TextToDisplay); } } }