This programme provides a simple encryption routine using a caesar cypher, it turns plain text into ciphertext.
00:09
As always, we have some boilerplate code.
00:12
This time we'll start with the main programme.
00:15
First, we'll ask the user to enter a message and store that in a variable identified as actual message.
00:22
The stored message will become the result of encrypting the message using the actual message.
00:30
Then we'll output the stored message to the screen.
00:33
Okay, now we can write the encryption function that will take the message in as plain text as a parameter, and return the ciphertext static string encryptmessage.
00:44
With the parameter message, the encryption key will be five.
00:50
That's the number of characters we will shift.
00:52
So, so A will become F.
00:56
The encrypted message starts as an empty string and then we'll build it up by shifting each character in the message one at a time.
01:04
We can do that with a for loop for each character in message.
01:10
To shift each character, we need to know the ASCII code of the character and then add the key to it before turning it back into a character.
01:17
an integer ASCII code becomes the integer of the character.
01:25
This is an alternative way of casting a character into a number.
01:29
In effect, what it does is convert and store the UNICODE for the character.
01:35
Okay, step back here.
01:37
Every character in a computer, that's letters, numbers and symbols are all stored in binary zeros and ones, so each character has a binary number.
01:47
Standards such as Unicode and ASCII map characters to binary numbers to be stored in a computer.
01:54
It's helpful to us to convert our character into a number so that we can add the key to it.
02:01
We called our variable ASCII code because the first 128 characters in each standard are the same.
02:08
It was just a decision I took when identifying the variable.
02:11
I could have called the variable Unicode instead.
02:15
ASCII code then becomes whatever it is now plus the key.
02:21
Now we have a problem, because what do we do if we shift beyond the number 126 in the ASCII table?
02:28
Because 127 is delete, we need to cycle back to the first character again.
02:34
So if ASCII code is greater than 126, ASCII code becomes 32.
02:43
That's the space character plus ASCII code minus 127.
02:49
That does the looping back around for us.
02:52
We could also have used modulo here if we wanted to.
02:56
The encrypted message is then whatever characters it already includes plus the character of the ascii code we just calculated.
03:05
Note how we turn a number back into a character.
03:09
Pretty neat.
03:10
Once all the characters have been converted, we can return the encrypted message.
03:17
This is a variation of the Caesar Cypher.
03:19
because it uses some of the symbols in the ASCII table too, we could restrict it to just letters as an extension.
03:27
We'll give the programme a run with the message.
03:30
I love computer science.
03:32
Not a great encryption algorithm, but a great example of using the ASCII table in code.