ASCII Code
We are going to build a Caesar Cipher using Python. To make this easy, we need to understand a little bit about how the alphabet is stored on a computer using ASCII. ASCII stands for the American Standard Code for Information Interchange and was a system that was invented to store the alphabet in the language computers understand - Binary.
Task 1cWe need to create a Python program to encrypt and decrypt messages.
Order these tasks in order to decrypt a message: a) add the shift to the integer b) enter a message c) output the encrypted string d) convert the character to its integer equivalent e) add the new character to the encrypted string f) enter the shift g) convert the integer to its character equivalent h) loop through every character in the sentence How would the algorithm change if you wanted to decrypt a message? Task 3Now we are going to program the algorithm using the Python programming language. First, using comments (#), write out the algorithm in the Python IDE. Use these comments as a guide for writing the code needed below.
You cannot apply a shift directly to a letter, because they are different data types. Trying to add a shift of 3 to "A" like this: "A" + 3 = "D" Will not work because "A" is a character and 3 is an integer, so you would encounter an error. With this in mind, we will have to know how to convert a character to its ASCII integer equivalent. In Python we can use: ord() To convert a character to an integer and: chr() To convert an integer to a character again. With this new knowledge, use the algorithm and the variable table to program your encryption program. |
After watching the video you should understand that binary numbers can represent letters. As was explained, we look at the last 5 numbers and calculate the decimal number (base 10) to find out what letter we are looking for. So:
00001 = 1 = A 00010 = 2 = B 00011 = 3 = C However, each letter is actually represented by 8 bits of data, not just 5. The first three bits tell us if we are looking at a uppercase letter or a lowercase letter. Uppercase letters start with 010 and lowercase letters start with 011. So: 01000001 = 65 = A 01100001 = 97 = a Task 2Complete the variable table for the program you are about to create
Task 4Now create a menu and allow the user to either encrypt or decrypt a message.
Going for Gold? Complete this task using three procedures - one for the menu, one to encrypt and one to decrypt. |