jhylands

skeleton code

Jun 3rd, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.29 KB | None | 0 0
  1. 'Skeleton Program code for the AQA COMP1 Summer 2013 examination
  2. 'this code should be used in conjunction with the Preliminary Material
  3. 'written by the AQA COMP1 Programmer Team
  4. 'developed in the Visual Studio 2008 (Console Mode) programming environment (VB.NET)
  5.  
  6. Module SecretMessages
  7.     Sub Main()
  8.         Dim Ciphertext As String
  9.         Dim Plaintext As String
  10.         Dim Choice As Char
  11.         Dim AmountToShift As Integer
  12.         Dim StartPosition As Integer
  13.         Dim EndPosition As Integer
  14.         Dim SizeOfRailFence As Integer
  15.         Dim N As Integer
  16.         Ciphertext = ""
  17.         Plaintext = ""
  18.         Do
  19.             DisplayMenu()
  20.             Choice = GetMenuChoice()
  21.             Select Case Choice
  22.                 Case "a"
  23.                     Plaintext = GetTextFromUser()
  24.                 Case "b"
  25.                     DisplayPlaintext(Plaintext)
  26.                 Case "d"
  27.                     Ciphertext = GetTextFromUser()
  28.                 Case "e"
  29.                     DisplayCiphertext(Ciphertext)
  30.                 Case "g"
  31.                     DisplayPlaintext(Plaintext)
  32.                     AmountToShift = GetKeyForCaesarCipher()
  33.                     Ciphertext = UseCaesarCipher(Plaintext, AmountToShift)
  34.                     DisplayCiphertext(Ciphertext)
  35.                 Case "h"
  36.                     DisplayPlaintext(Plaintext)
  37.                     SizeOfRailFence = GetSizeOfRailFence()
  38.                     Ciphertext = EncryptUsingRailFence(Plaintext, SizeOfRailFence)
  39.                     DisplayCiphertext(Ciphertext)
  40.                 Case "j"
  41.                     DisplayCiphertext(Ciphertext)
  42.                     AmountToShift = -GetKeyForCaesarCipher()
  43.                     Plaintext = UseCaesarCipher(Ciphertext, AmountToShift)
  44.                     DisplayPlaintext(Plaintext)
  45.                 Case "k"
  46.                     DisplayCiphertext(Ciphertext)
  47.                     SizeOfRailFence = GetSizeOfRailFence()
  48.                     Plaintext = DecryptUsingRailFence(Ciphertext, SizeOfRailFence)
  49.                     DisplayPlaintext(Plaintext)
  50.                 Case "n"
  51.                     GetPositionsToUse(StartPosition, EndPosition)
  52.                     N = GetValueForN()
  53.                     Plaintext = EveryNthCharacterSteganography(StartPosition, EndPosition, N)
  54.                     DisplayPlaintext(Plaintext)
  55.             End Select
  56.             If Choice <> "q" Then
  57.                 Console.WriteLine("Press enter key to continue")
  58.                 Console.ReadLine()
  59.             End If
  60.         Loop Until Choice = "q"
  61.     End Sub
  62.  
  63.     Sub DisplayMenu()
  64.         Console.WriteLine()
  65.         Console.WriteLine("PLAINTEXT OPTIONS")
  66.         Console.WriteLine("  a.  Get plaintext from user")
  67.         Console.WriteLine("  b.  Display plaintext")
  68.         Console.WriteLine("CIPHERTEXT OPTIONS")
  69.         Console.WriteLine("  d.  Get ciphertext from user")
  70.         Console.WriteLine("  e.  Display ciphertext")
  71.         Console.WriteLine("ENCRYPT")
  72.         Console.WriteLine("  g.  Caesar cipher")
  73.         Console.WriteLine("  h.  Rail fence")
  74.         Console.WriteLine("DECRYPT")
  75.         Console.WriteLine("  j.  Caesar cipher")
  76.         Console.WriteLine("  k.  Rail fence")
  77.         Console.WriteLine("STEGANOGRAPHY")
  78.         Console.WriteLine("  n.  nth character (text from file)")
  79.         Console.WriteLine()
  80.         Console.Write("Please select an option from the above list (or enter q to quit): ")
  81.     End Sub
  82.  
  83.     Function GetMenuChoice() As Char
  84.         Dim MenuChoice As Char
  85.         MenuChoice = Console.ReadLine
  86.         Console.WriteLine()
  87.         GetMenuChoice = MenuChoice
  88.     End Function
  89.  
  90.     Function GetTextFromUser() As String
  91.         Dim TextFromUser As String
  92.         Console.Write("Please enter the text to use: ")
  93.         TextFromUser = Console.ReadLine
  94.         GetTextFromUser = TextFromUser
  95.     End Function
  96.  
  97.     Sub GetPositionsToUse(ByRef StartPosition As Integer, ByRef EndPosition As Integer)
  98.         Console.Write("Please enter the start position to use in the file: ")
  99.         StartPosition = Console.ReadLine
  100.         Console.Write("Please enter the end position to use in the file: ")
  101.         EndPosition = Console.ReadLine
  102.     End Sub
  103.  
  104.     Function GetTextFromFile(ByVal StartPosition As Integer, ByVal EndPosition As Integer) As String
  105.         Dim CharacterFromFile As Char
  106.         Dim TextFromFile As String
  107.         Dim Count As Integer
  108.         FileOpen(1, AppDomain.CurrentDomain.BaseDirectory & "diary.txt", OpenMode.Binary)
  109.         For Count = 1 To StartPosition - 1
  110.             FileGet(1, CharacterFromFile)
  111.         Next
  112.         TextFromFile = ""
  113.         For Count = StartPosition To EndPosition
  114.             FileGet(1, CharacterFromFile)
  115.             TextFromFile = TextFromFile & CharacterFromFile
  116.         Next
  117.         FileClose(1)
  118.         GetTextFromFile = TextFromFile
  119.     End Function
  120.  
  121.     Function GetKeyForCaesarCipher() As Integer
  122.         Dim Key As Integer
  123.         Console.Write("Enter the amount that shifts the plaintext alphabet to the ciphertext alphabet: ")
  124.         Key = Console.ReadLine
  125.         GetKeyForCaesarCipher = Key
  126.     End Function
  127.  
  128.     Function GetTypeOfCharacter(ByVal ASCIICode As Integer) As String
  129.         Dim TypeOfCharacter As String
  130.         If ASCIICode >= Asc("A") And ASCIICode <= Asc("Z") Then
  131.             TypeOfCharacter = "Upper"
  132.         Else
  133.             If ASCIICode >= Asc("a") And ASCIICode <= Asc("z") Then
  134.                 TypeOfCharacter = "Lower"
  135.             Else
  136.                 TypeOfCharacter = "Other"
  137.             End If
  138.         End If
  139.         GetTypeOfCharacter = TypeOfCharacter
  140.     End Function
  141.  
  142.     Function ApplyShiftToASCIICodeForCharacter(ByVal ASCIICode As Integer, ByVal AmountToShift As Integer) As Integer
  143.         Dim NewASCIICode As Integer
  144.         Dim TypeOfCharacter As String
  145.         TypeOfCharacter = GetTypeOfCharacter(ASCIICode)
  146.         If TypeOfCharacter <> "Other" Then
  147.             If TypeOfCharacter = "Upper" Then
  148.                 NewASCIICode = ((26 + ASCIICode - Asc("A") + AmountToShift) Mod 26) + Asc("A")
  149.             Else
  150.                 NewASCIICode = ((26 + ASCIICode - Asc("a") + AmountToShift) Mod 26) + Asc("a")
  151.             End If
  152.         Else
  153.             NewASCIICode = ASCIICode
  154.         End If
  155.         ApplyShiftToASCIICodeForCharacter = NewASCIICode
  156.     End Function
  157.  
  158.     Function UseCaesarCipher(ByVal OriginalText As String, ByVal AmountToShift As Integer) As String
  159.         Dim ChangedText As String
  160.         Dim Count As Integer
  161.         Dim ASCIICode As Integer
  162.         ChangedText = ""
  163.         For Count = 0 To OriginalText.Length - 1
  164.             ASCIICode = Asc(OriginalText(Count))
  165.             ASCIICode = ApplyShiftToASCIICodeForCharacter(ASCIICode, AmountToShift)
  166.             ChangedText = ChangedText & Chr(ASCIICode)
  167.         Next
  168.         UseCaesarCipher = ChangedText
  169.     End Function
  170.  
  171.     Function GetSizeOfRailFence() As Integer
  172.         Dim SizeOfRailFence As Integer
  173.         Console.Write("Enter the number of lines in the rail fence: ")
  174.         SizeOfRailFence = Console.ReadLine
  175.         GetSizeOfRailFence = SizeOfRailFence
  176.     End Function
  177.  
  178.     Function EncryptUsingRailFence(ByVal OriginalText As String, ByVal SizeOfRailFence As Integer) As String
  179.         Dim Count1 As Integer
  180.         Dim Count2 As Integer
  181.         Dim Ciphertext As String
  182.         Ciphertext = ""
  183.         For Count1 = 1 To SizeOfRailFence
  184.             Count2 = Count1 - 1
  185.             While Count2 < OriginalText.Length
  186.                 Ciphertext = Ciphertext & OriginalText(Count2)
  187.                 Count2 = Count2 + SizeOfRailFence
  188.             End While
  189.         Next
  190.         EncryptUsingRailFence = Ciphertext
  191.     End Function
  192.  
  193.     Function DecryptUsingRailFence(ByVal Ciphertext As String, ByVal SizeOfRailFence As Integer) As String
  194.         Dim Plaintext As String
  195.         Dim NoOfColumns As Integer
  196.         Dim NoOfRows As Integer
  197.         Dim NoOfCiphertextCharacters As Integer
  198.         Dim NoOfCiphertextCharactersProcessed As Integer
  199.         Dim i As Integer
  200.         Dim j As Integer
  201.         Dim PositionOfNextCharacter As Integer
  202.         Dim LastFullRowNo As Integer
  203.         Dim AmountToReduceNoOfColumnsTimesjBy As Integer
  204.         Dim BeginningOfNextRowIndex As Integer
  205.         Plaintext = ""
  206.         NoOfCiphertextCharacters = Ciphertext.Length
  207.         'The ciphertext was created from a visualisation of the plaintext as
  208.         'a two-dimensional grid of characters with no of rows = size of rail fence
  209.         NoOfRows = SizeOfRailFence
  210.         NoOfColumns = NoOfCiphertextCharacters \ SizeOfRailFence
  211.         'If NoOfCiphertextCharacters divides exactly all rows will be full
  212.         'otherwise the last column will be incomplete and NoOfColumns will not include last column
  213.         If NoOfCiphertextCharacters Mod SizeOfRailFence <> 0 Then
  214.             NoOfColumns = NoOfColumns + 1
  215.         End If
  216.         'Calculate row no of last full row, 0 means every row full
  217.         LastFullRowNo = NoOfCiphertextCharacters Mod SizeOfRailFence
  218.         NoOfCiphertextCharactersProcessed = 0
  219.         For i = 1 To NoOfColumns 'Work along the columns building the plaintext a column at a time
  220.             AmountToReduceNoOfColumnsTimesjBy = 0
  221.             For j = 0 To NoOfRows - 1   'Work down the rows building the plaintext
  222.                 If LastFullRowNo <> 0 Then  'Last column doesn't have a character in every row
  223.                     If j > LastFullRowNo Then 'There are shorter rows to skip
  224.                         AmountToReduceNoOfColumnsTimesjBy = AmountToReduceNoOfColumnsTimesjBy + 1
  225.                     End If
  226.                 End If
  227.                 'NoOfColumns * j locates in ciphertext beginning of each row
  228.                 BeginningOfNextRowIndex = NoOfColumns * j - AmountToReduceNoOfColumnsTimesjBy
  229.                 PositionOfNextCharacter = BeginningOfNextRowIndex + i - 1
  230.                 NoOfCiphertextCharactersProcessed = NoOfCiphertextCharactersProcessed + 1
  231.                 If NoOfCiphertextCharactersProcessed <= NoOfCiphertextCharacters Then
  232.                     Plaintext = Plaintext + Ciphertext(PositionOfNextCharacter)
  233.                 End If
  234.             Next
  235.         Next
  236.         DecryptUsingRailFence = Plaintext
  237.     End Function
  238.  
  239.     Function GetValueForN() As Integer
  240.         Dim N As Integer
  241.         Console.Write("Enter the value of n: ")
  242.         N = Console.ReadLine
  243.         GetValueForN = N
  244.     End Function
  245.  
  246.     Function EveryNthCharacterSteganography(ByVal StartPosition As Integer, ByVal EndPosition As Integer, ByVal N As Integer) As String
  247.         Dim HiddenMessage As String
  248.         Dim CurrentPosition As Integer
  249.         CurrentPosition = StartPosition
  250.         HiddenMessage = ""
  251.         While CurrentPosition <= EndPosition
  252.             HiddenMessage = HiddenMessage & GetTextFromFile(CurrentPosition, CurrentPosition)
  253.             CurrentPosition = CurrentPosition + N
  254.         End While
  255.         EveryNthCharacterSteganography = HiddenMessage
  256.     End Function
  257.  
  258.     Sub DisplayPlaintext(ByVal TextToDisplay As String)
  259.         Console.WriteLine()
  260.         Console.Write("The plaintext is: ")
  261.         Console.WriteLine(TextToDisplay)
  262.     End Sub
  263.  
  264.     Sub DisplayCiphertext(ByVal TextToDisplay As String)
  265.         Console.WriteLine()
  266.         Console.Write("The ciphertext is: ")
  267.         Console.WriteLine(TextToDisplay)
  268.     End Sub
  269. End Module
Advertisement
Add Comment
Please, Sign In to add comment