In the second book of the Harry Potter Series, “The Chamber of Secrets” by J.K. Rowling, Harry Potter finds out that he can communicate with snakes using the Parseltongue language.
In this challenge we will write a Python script to translate English to Parseltongue and vice-versa.
Step 1: Parseltongue Encoder
To encode a message into Parseltongue you need to insert the string sequence “sss” between each character of your message. e.g.
Step 2: Parseltongue Decoder
To decode a message from Parseltongue remove the “sss” character sequences by only keeping 1 character out of 4 from the encoded message.
Check the following code, which uses two subroutines (functions) called encode() and decode() to encode a message in Parseltongue or decode a message from Parseltongue. Both functions use a technique called string concatenation to add one letter at a time to the message or cypher being generated.
Did you know? Encoding and decoding secret messages is a key application of Computer Science called cryptography. An encoded message is called a cipher.
As a wizard at Hogwarts school of witchcraft and wizardry, Harry Potter often needs to decipher secret messages. In this set of challenges you will write Python subroutines to encode or decode secret messages using a range of techniques.
Task 1: Mumbled Words
The Parseltongue coding technique described above is not very secure. It would be easy for anyone to look at a cipher text and to be able to find a way to decipher it without being told how to in the first instance.
To make this encryption technique more secure we will adapt the encode() function implemented in the trinket above. Instead of adding the string “sss” after each character we will add three random letters.
By completing this challenge, we are going to learn how to use ASCII code when manipulating strings.
You will use the chr() and ord() python instructions to convert characters into ASCII code and vice versa.
- print(chr(97)) would display the letter “a” on screen as 97 is the ASCII code for character “a”.
- print(ord(“a”)) would display the 97 on screen as 97 is the ASCII code for character “a”.
To complete this challenge, you may want to use our ASCII code helpsheet
The code to generate a random letter is as follows:
Use this code to tweak the the encode() function. The decode() function should not need to be updated and should still work with this new encryption technique.
Test your code. Do you find the cipher text to be more secure?
Task 2: Reversi Formula
Using this encryption techniques the cipher message is based on the actual message with the letters of the message appearing in reverse order. e.g.
Using this encryption technique, the same function can be used to both encode and decode a message.
Your task is to implement one function used to encode/decode a message, applying the “Reversi formula”.
Task 3: Intertwined Messages
Create two new functions to encode and decode two messages at the same time by intertwining each letter of these messages. Your encode() function will take two parameters, message1 and message2, and generate a cipher by intertwining each letter of both messages, one letter at a time.
Task 4: Caesar Cypher
In cryptography, a Caesar cipher, also known as shift cipher, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the message to encrypt is replaced by a letter some fixed number of positions down the alphabet.
Task 5: Your Turn
Can you think of any other approach you could use to encrypt a message? You may combine several of the techniques listed above, as combining several techniques will make your cipher more difficult to decode hence it will be a lot more secure!