Caesar Cipher

10 points

 

Overview
A cipher is a way of encoding a message so that, if it gets into the wrong hands, it will be difficult to read. The people the message is intended for know how to decode and read the message.

 

ASCII Table
The ASCII table associates a number with every key on a computer keyboard. For example, "A" is 65 in the ASCII table, "B" is 66, "C" is 67, etc. For the lowercase letters: "a" is 97, "b" is 98, "c" is 99, etc. Click here to view the full ASCII table. The keyboard characters are red and are in the column "Chr". The number for each character is in the column "Dec". You will make use of the ASCII table in writing your code.

 

Caesar Cipher
The Caesar Cipher replaces each character with a character that is a certain number of spaces. For example a Caesar Cipher could be set up as follows, with a shift of 17 spaces:
alphabet: 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
enciphered: R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k

Each character in the original alphabet (upper row) is written, in the encoded message, as the letter below it. An 'A' would be written as 'R', a 'J' would be written as a '[', a 'P' would be written as a 'a', and so on.

Start a new VB project and try these lines of code:
MsgBox(Chr(65))
MsgBox(Asc("A"))
You should have an understanding of what Chr() and Asc() do. You should see a connection between Chr(), Asc() and the ASCII table.

If you'd like to try to earn extra credit by doing a more challenging version of this program, then jump to the Extra Credit section below. You'll want to begin with an "extra credit" approach to the problem.

The input file you'll be working with ("scrambled.txt") is a text that has had a Caesar Cipher applied to it. Specifically, a right shift of 12 has been applied to each character. So an "A" in the original text appears as a "M" in scrambled.txt, etc. Your task is to unscramble the scrambled text so that you can read it. You'll do this by undoing the right-shift of 12, one character at a time.

 

Writing the Code
You can set up your program so that it runs when a button is clicked. This is code that you can include in your program that will read the entire contents of the input file 'scrambledText.txt' into the String 'fileReader':
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("scrambledText.txt")

You can set up your output file, for the unscrambled text, in this way:
Dim outFile As IO.StreamWriter
outFile = IO.File.CreateText("unscrambledText.txt")

You'll want to set up a loop to step through every character in 'fileReader'.

You'll want to use VB's Mid() function. Mid() extracts part of a String from a larger String. For example:
Mid("Rachel", 4, 1) would produce "h".
Mid("Vinnie", 4, 2) would produce "ni".
Mid("Amaya", 1, 3) would produce "Ama".
Mid("Lev", 2, 2) would produce "ev".
Mid("Katie", 5, 1) would produce "e".

Use syntax like this to write a String to the output file: outFile.Write("hello")

When the loop is done, you should close the output file: outFile.close()

This is what my program looks like when it is launched:

When the button is clicked and the program is finished unscrambling the text, this is what appears:

VB does not display the unscrambled text. Open a text editor like NoteTab Light to view the unscrambled text.

 

Extra Credit (7 points)
For the extra credit version of this program, do not make an assumption about the size of the shift that has been applied in order to scramble the original text. Instead, figure out yourself what size shift was applied. To do this, use a letter frequency analysis. The most commonly-used letter in English is "e", and 12.7% of letters in English are "E" or "e". The second-most common letter is "t" (9.0%) and "a" (8.2%). So use the fact that the most common lowercase letter in the original text is "e". You'll need to figure out which character in the scrambled text is the most common, and assume that it is the scrambled version of "e". Calculate how far apart the most common character in the scrambled text is from "e" and you'll know what size shift was applied in scrambling the text.

Then go about unscrambling the scrambled text.

 

Getting Started
Download scrambledText.txt here. Store this file in the bin/Debug folder within your Visual Basic project.