Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holborow and welcome to Computing.

I'm so pleased you've decided to join me for the lesson today.

In today's lesson, we're going to be using our knowledge of dictionaries to produce a dictionary with key-value pairs to create an encryption programme.

Welcome to today's lesson from the unit, "Programming Dictionaries and Data Files".

This lesson is called, "Dictionary Challenge" and by the end of today's lesson, you'll be able to create an encryption programme using a dictionary with key-value pairs.

Shall we make a start? We will be exploring these keywords throughout today's lesson.

Encryption, encryption.

The process of applying an algorithm to plain text to produce cypher text that cannot be understood without decryption.

Cypher.

Cypher.

An algorithm used to convert plain text messages to cypher text, encryption or cypher text to plain text, decryption.

Key-value pair.

When the key, the attribute identifier is paired with the data.

Look out for these keywords throughout today's lesson.

Today's lesson is broken down into two parts.

We'll start by describing the dictionary data structure.

We'll then move on to use a dictionary to produce key-value pairs.

Let's make a start by describing the dictionary data structure.

Laura says, "Hey, Jun, I have a secret message.

Try to crack the code to see what the message is.

Plus two.

RAVJQP KU CEG." Can you crack the code? Ah, Jun says, "I think the message is Python is ace." Did you get that? A cypher is a type of secret code where you swap the letters around so that no one can read the message.

A Caesar cypher is one of the oldest and most famous cyphers and it's named after Julius Caesar.

A Caesar cypher uses a key.

In this example, the key is plus two.

This means that the inner wheel moves two letters to the right.

A key is used to lock and unlock the message.

The message cannot be read without the key.

So here you can see that the inner wheel has shifted two letters to the right.

The inner wheel becomes the original message.

The outer wheel is used for the encrypted message.

To encrypt the word cat, you start by finding C on the inner wheel and looking at the outer wheel for the encrypted letter.

So C becomes E.

You continue for each letter until you've finished the encryption.

So the word cat becomes ECV.

Time to check your understanding.

A Cesar cypher uses a key.

In this example, the key is plus five.

What does the letter A now become? Is it A, F, B, V or C, E? Pause the video whilst you have a think.

Did you select A, F? Well done.

Remember we're using a plus five key here.

A dictionary data structure can be used to create a Caesar cypher.

This is ideal, because the dictionary maps one thing to another.

Unlike a record data structure, a dictionary data structure is dynamic.

You can change the size of the structure during execution.

This means that a dictionary can be populated during execution.

True or false? A dictionary data structure is static.

Pause the video whilst you have a think.

Did you select false? Well done.

A dictionary data structure is dynamic.

You can change the size of the dictionary data structure during execution.

The key-value can hold the original character and the pair can hold the encrypted character.

So here we have Caesar is equal and then we have the letter Y, which is the key-value and then we have the pair, which is A, and we do that for each letter.

What will be the output of this programme when it's executed? Maybe pause the video here whilst you have a think.

That's right, the programme will output B.

That's because on line five, we have encrypted is equal to Caesar and then the value Z.

The key-value is Z and the pair item is B.

So that's what will be printed out.

You can add a new key-value pairing to the dictionary using this piece of code.

So on line five we have Caesar and then we have D in square brackets and speech marks is equal to F in speech marks.

What will be the output of this programme when it's executed? Pause the video whilst you have a think.

That's right, the programme will output the whole dictionary, because we're saying print Caesar, which is the name of the dictionary.

Okay, we're moving on to our first task of today's lesson, task A.

I'd like you to describe the dictionary data structure using these terms. Data structure, dynamic, key-value pair, I'd then like you to explain how a dictionary is an appropriate choice for implementing the Caesar cypher algorithm.

Pause the video whilst you complete the activity.

How did you get on? Let's have a look at a sample answer together.

You were asked to describe the dictionary data structure using the terms provided.

A dictionary is a data structure that maps one thing to another.

It is a dynamic structure, which means the size of it can change during execution.

Dictionaries have key-value pairs which are linked to data items. The key is a unique identifier for an item of data and the value is the data that the key holds.

You were then asked to explain how a dictionary is an appropriate choice for implementing the Caesar cypher algorithm.

A dictionary is appropriate for implementing the Caesar cypher algorithm as the original character can be mapped to the encrypted character.

So we've described the dictionary data structure.

We're now going to move on to use a dictionary to produce key-value pairs.

ASCII is a character set used to represent all of the characters on a standard American or English keyboard.

Each character has a unique code.

You can find the codes by looking at an ASCII table.

If you can't see this one clearly, you can access them easily on the internet.

For each character in the table, the decimal code increases by one as the alphabet increases.

So A has the decimal value 65 and the hex value 41.

B, capital B this is, has the decimal value 66 and the hex value 42.

And capital C has the decimal value 67 and the hex value 43.

What will be the decimal and hex values of capital D? Maybe pause the video whilst you have a think.

That's right, it just increments by one.

So D has the decimal value 68 and the hex value 44.

Python has two functions that perform ASCII conversions.

The first is chr, which stands for character, open brackets and then the decimal value, which we've got 97 here and close brackets.

This takes a decimal number and returns its character equivalent.

So this would return lowercase A.

The next one is ord, so O-R-D, open brackets and then we have speech marks and lowercase A and close brackets.

This takes a character and returns its decimal equivalent.

So this will return the value 97.

Time to check your understanding.

What character will this code return? CHR (66).

Is it A, capital B, B, lowercase B or C, capital A? Pause the video whilst you have a think.

That's right, it will return uppercase B, because that is the value held in decimal 66.

So far you've seen how to return one character that has been converted using a cypher.

How could you convert a whole word or sentence? Laura here says, "Could I use iteration to loop through the characters until I get to the end?" That's a great idea, Laura.

Iteration can be used to loop through a string of characters.

So here we have an example.

We have word is equal to pizza and then we have for letter in word print (letter).

The text output for this will be each letter of the word pizza returned on a new line.

Laura says, "I need to join one character to another to reveal the message, but I'm not sure how." Can you think of a way? The plus symbol can be used to concatenate or join together one string value with another string value.

So here we've got the example, letter is equal to A, anotherletter is equal to B, and then we're replacing the value of letter with letter is equal to letter plus anotherletter.

Time to check your understanding.

What value is held by letter at the end of this programme? Is it A, a, B, ab or C, b? Pause the video whilst you have a think.

Did you select B? Well done.

Remember, the plus can be used to join two strings or concatenate two strings together.

Okay, we're moving on to our next task of today's lesson and you've done a fantastic job so far, so well done.

I'd like you to open the starter programme at oak.

link/caesar-cipher.

I'd like you to create a Caesar cypher encryption programme that A, prompts the user to enter an encrypted key between one and 25, B, uses a function to populate a dictionary that will form the Caesar cypher.

The function should accept the encryption key as an argument, create a dictionary called Caesar, add the space character to the dictionary.

For each letter in the alphabet, it should create a dictionary pair that contains the plain text letter as the key and the encrypted letter as the key-value pair.

If the encrypted letter is higher than Z, it should go back 26 letters to mimic a Caesar's wheel.

It should then finally return the Caesar dictionary to the main programme.

Pause the video whilst you go and have a go at the task.

How did you get on? Did you manage to create your solution? Great work, well done.

Let's have a look at some code together.

If you want to visit the full working solution, you can go to oak.

link/caesar-cipher-solution.

So here we have our code.

We've got a subroutine which is initiated line one, which is called populate_cipher, which has the argument encrypt_key.

We then have caesar is equal to and we're setting up an empty dictionary here, so we've got the curly brackets and empty speech marks, but nothing inside that dictionary at the moment.

We then have a for loop.

So we say for x in range 65, 91.

Remember, we're using the values from the ASCII table here and then we have key is equal to chrx.

If x plus encrypt_key is more than 90, then the pair is going to be equal to chr(x + encrypt_key minus 26), else the pair is equal to chr (x + encrypt_key).

So that if statement there is just checking if the character needs to loop back to the start of the alphabet.

We then have Caesar in square brackets, key is equal to pair and then return Caesar to the main programme.

On line 12, we ask the user what is the encryption key and then we store that as encrypt_key as an integer value on line 13.

On line 14, we're calling the subroutine.

So caesar is equal to populate_cipher and we're passing it the encryption key that the user has just entered.

And we're passing it the encryption key that the user has just entered.

On line 16, we ask the user to enter their text to encrypt and we're then storing that as a variable called plain text, but we're forcing it to be uppercase.

We set cypher text to be empty and then we have another for loop, which says for letter in plain text encrypted is equal to caesar letter, cypher text is equal to cypher text plus encrypted and we print out the cypher text on line 25.

So here's an example text output.

What is the encryption key? Two.

Enter your text to encrypt.

The user has entered cat and then it has returned ECV, which has encrypted that cat into the new letters, ECV.

Remember, if you didn't quite get your programme working correctly, you can use the solution to help you amend your programme and get your Caesar cypher working.

Pause the video now if you need to go back and make any changes.

Okay, we've come to the end of today's lesson, "Dictionary Challenge".

Let's summarise what we have learned.

A cypher is a type of secret code where you swap the letters around so that no one can read your message.

A Caesar cypher is one of the oldest and most famous cyphers.

A dictionary data structure can be used to create a Caesar cypher as a dictionary maps one thing to another.

I hope you've enjoyed today's lesson and I hope you'll join me again soon.

Bye.