This is a machine-translated text that may contain errors!
About cryptography tasks
These tasks are structured a little differently than the other tasks on Piggy, wondering a bit what you prefer! 😎
There will first be a section of information to begin with about the topics, then there will be some tasks afterwards!
The “levels” in this task are not quite like the levels before, here things are more divided into themes.
What is a “Cipher”?
Have you ever wanted to write a secret message to a friend, so that no one else can understand it? Then you need a Cipher, or chiffer in Norwegian! A cipher is simply a method that transforms regular text into “code” by replacing characters (often letters) with other characters. The result looks like gibberish to those who don’t know how the code works. The point is that only those who know the key (the rule for replacing the letters) can make the code understandable again. In other words: ciphers make secret messages possible, whether it’s childhood games with secret languages or real spies sending encrypted messages. 😄
Did you know?
The word “cipher” actually comes from an Arabic word: the word “sifr,” which means “zero.” Perhaps because the secret code looked like nothing (zero meaning!) when people couldn’t solve it!
There are many types of ciphers – some use numbers, some use symbols, and modern data encryption uses very complex algorithms. These complex algorithms require very complex mathematics, so let’s look at some simpler algorithms first!
Monalphabetic Ciphers
Let’s first look at some of the simplest (and oldest) coding methods that exist: monalphabetic ciphers.
Monalphabetic may sound like a difficult word, but we can break it down: mono means “one,” and alphabetic refers to the alphabet.
Thus, monalphabetic ciphers are codes where one single “encryption alphabet” is used for the entire message. That is, each letter in the original text is always replaced with the same letter throughout the encrypted message.
For example, if you have decided that A should be replaced with X, then all the A’s in the text are changed to X.
Caesar Cipher
The classic example of a monoalphabetic cipher is the Caesar cipher (named after Julius Caesar). This is basically a rule about “shifting” all the letters a certain number of places forward in the alphabet. Apparently, Caesar himself used a shift of 3 letters in his secret messages. It works such that A becomes D, B becomes E, C becomes F, and so on through the alphabet. (When one goes past Z, one starts at A again.) A message like ABC would thus become DEF if we use Caesar’s method.
Nobody Expects a Message in Caesar Cipher?
How the Caesar Cipher Works in Practice:
- Choose a key: Decide on a secret number (for example, 3) that indicates how many places you should shift each letter.
- Replace each letter: For each letter in the original message, find the letter that lies so many places after it in the alphabet (for key 3, A becomes D, B becomes E, etc. – remember to wrap around to A again after Z if necessary). Optionally, you can also include Æ, Ø, and Å, but this becomes a little more complicated.
- Encrypted message: Replace the letters and write the new message with the “shifted” letters. Voila – you have an unreadable, secret text that only those with the key can understand!
- To decrypt (i.e., turn it back into readable text again), you simply do the opposite shift back. If you know the key (e.g., 3), it is just as easy to read the message by moving the letters 3 back in the alphabet.
Safety?
These codes aren’t particularly secure in the long run. Because the pattern (the substitution) is fixed, a person with enough patience or some clever tricks can easily reveal the secret. For example, there are only a few possible shifts in the Caesar cipher, as many as the alphabet, so anyone can try them all until the message makes sense – or use letter frequencies to guess their way forward. In other words, maybe don’t use the Caesar cipher for super-secret diary entries or state secrets 😉.
Monalphabetic ciphers are a fantastic way to learn the principle behind encryption. They are simple and show how we can use a simple rule (a key) to transform an understandable text into something mysterious and incomprehensible – and back again. So next time you want to send a friend a secret message, you can use the Caesar cipher! Maybe you can create your own variant of Caesar’s secret alphabet? 🔐✨
Tasks
Utføre enkle beregninger
Performing Simple Calculations
Vurdere resultater
Evaluating Results
Dokumentere funn
Documenting Findings
Rapportere avvik
Reporting Deviations
Samarbeide med andre
Collaborating with Others
Programming language?
As before, feel free to use whichever programming language you like! The examples here will be in Python.
Task 1.1 - Caesar Cipher Encryption
Now we are actually going to write some code! We will start simple by creating the encryption, based on the theory it should be quite straightforward.
Implement encryption with a Caesar cipher by using a function that takes in text and a number that is the “key,” i.e., how much the alphabet should be rotated by.
Tips for procedure.
- Create a function called
caesarthat takes the text to be encrypted and a “shift,” i.e., how many places in the alphabet the text should be shifted. - Go through letter by letter in the text.
- We will not “shift” other characters than letters: find out how you check that a character in the text is a letter.
- We are going to “rotate” the letter by n places, so we need to add the rotation: find out how you can convert the text to numbers so that you can add the shift. Hint: The
ord()function. - Remember! Here you get different values depending on whether you have lowercase or uppercase letters. Refer to the ASCII Table.
- After you have a value, it’s as simple as adding n to the value. But what happens if you are at the end of the alphabet? You just get nonsense after the letter Z. How is this fixed? This requires some thinking.
Fix the encryption.
To fix the encryption completely requires some thinking.
- The first step to think about is using the modulus operator,
%. - Since the alphabet (in English) consists of 26 letters, we can take the modulus with
26. - But this doesn’t quite work, do you see the reason?
- Try to
printout the value of a character withord(), what do you get? - For
ayou get 97. If you take the modulus 26 with this, you get 19. Remember that modulus will always give an answer between 0 and the number. - This can be fixed by storing the starting value of uppercase and lowercase letters, subtracting this from the letter, and then taking the modulus. Then it becomes:
(ord(letter) - ord('a')) % 26 - To get the correct letter back, you just add the starting value again.
- After all this, you can finally convert the number back to a letter again. Here you can use the
chr()function. - Now you can finally add the letter to a result and return the encrypted text!
Solution:
def caesar_cipher(text, shift):
result = ""
for char in text:
if char.isalpha():
# Determine the starting point based on uppercase and lowercase letters
start = ord('A') if char.isupper() else ord('a')
# The tricky shift calculation
result += chr((ord(char) - start + shift) % 26 + start)
else:
result += char
return result
Task 1.2 - Caesar Cipher Decryption
Decryption is simply performing the opposite calculation to encryption. You subtract the offset instead of adding.
Tips for procedure.
Use the function you created in task 1 for this. Just take the same function, but in reverse. You can do this by shifting with 26 - shift.
Solution:
def caesar_decrypt(text, shift):
return caesar_cipher(text, 26 - shift)
Other Monoglyphic Ciphers (ex. Atbash)
There are other monoglyphic ciphers as well! One of the simpler ones is called the “Atbash” Cipher.
How Does Atbash Work?
This is very simple, instead of a rotation, letters are mapped to the opposite alphabet. Here is a table showing the mapping:
| 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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| z | y | x | w | v | u | t | s | r | q | p | o | n | m | l | k | j | i | h | g | f | e | d | c | b | a |
Task 1.3 - Atbash Cipher Encryption and Decryption
The nice thing about Atbash is that since encryption is a one-to-one transformation, it works directly in reverse. That is, if you have created the encryption, you have also automatically created the decryption.
How can this be done in practice?
You can either subtract the letter in relation to Z, or create a “Look-up” table. That is, a table or dictionary containing all the letters from a to z and what they should become. This can be a good solution if you want to create a different type of encryption.
Lookup-table implementation.
letters = {
'a': 'z'
'b': 'y'
'c': 'x'
'd': 'w'
# ... add the rest of the letters downwards
}
Using this table, you can go through letter by letter, then extract the value per letter from the lookup table, then print it out. What do you need to do for uppercase and lowercase letters?
Part 2 - Cryptanalysis of Monoalphabetic Ciphers
In this section, you will try to create an algorithm to “crack” a Caesar cipher, meaning to take an encrypted text and then retrieve the original text without knowing the key.
This can be done somewhat manually, or you can try to use simple “cryptanalysis”. This is a concept that we will look deeper into later, but for now we will only look at one of the simplest methods: frequency analysis. You can read more about this concept here: Frequency Analysis or here Wikipedia - frequency analysis.
This method can be used in more than just Caesar ciphers; it can be used in more complex algorithms as well, but the Caesar cipher is so simple that frequency analysis is trivial.
How does frequency analysis work?
Frequency analysis is, as the name suggests, a way to check the frequency of letters in a text. Why might this be useful? Imagine you have a long text, let’s imagine an English text, taken from Wikipedia - frequency analysis:
In cryptanalysis, frequency analysis is the study of the frequency of letters or groups of letters in a ciphertext. The method is used as an aid to breaking classical ciphers. Frequency analysis is based on the fact that, in any given stretch of written language, certain letters and combinations of letters occur with varying frequencies. Moreover, there is a characteristic distribution of letters that is roughly the same for almost all samples of that language. For instance, given a section of English language, E, T, A and O are the most common, while Z, Q, X and J are rare. Likewise, TH, ER, ON, and AN are the most common pairs of letters termed bigrams or digraphs), and SS, EE, TT, and FF are the most common repeats. The nonsense phrase ETAOIN SHRDLU represents the 12 most frequent letters in typical English language text. In some ciphers, such properties of the natural language plaintext are preserved in the ciphertext, and these patterns have the potential to be exploited in a ciphertext-only attack.If we were to transform this text using a Caesar cipher (and also remove commas, spaces, and other special characters), we would get the following cipher text:
xcrgneipcpanhxhugtfjtcrnpcpanhxhxhiwthijsnduiwtugtfjtcrnduatiitghdgvgdjehduatiitghxcprxewtgitmiiwtbtiwdsxhjhtsphpcpxsidqgtpzxcvraphhxrparxewtghugtfjtcrnpcpanhxhxhqphtsdciwtupriiwpixcpcnvxktchigtirwdulgxiitcapcvjpvtrtgipxcatiitghpcsrdbqxcpixdchduatiitghdrrjglxiwkpgnxcvugtfjtcrxthbdgtdktgiwtgtxhprwpgpritgxhixrsxhigxqjixdcduatiitghiwpixhgdjvwaniwthpbtudgpabdhipaahpbeathduiwpiapcvjpvttudgxchipcrtvxktcphtrixdcdutcvaxhwapcvjpvtitmixchdbtrxewtghhjrwegdetgixthduiwtcpijgpaapcvjpvteapxcitmipgtegthtgktsxciwtrxewtgitmipcsiwthtepiitgchwpktiwteditcixpaidqttmeadxitsxcprxewtgitmidcanpiiprzThis text looks impossible to “crack,” but with the help of “Frequency Analysis,” it’s not only possible, but easy.
This is a figure showing the distribution of letters in English. What we can see is that the letter
Eis the most frequent letter, followed byT,A, andO.This can be converted into a table and then used to count and analyze a given cipher text, so as to “crack” it. In the tasks below, you will create a program that can “crack” Caesar ciphers on its own. It’s true that the Caesar cipher is so simple that you can just check all 26 possibilities manually, but here we will find the solution, completely automatically.
Task 2.1 - Creating a Frequency Table
In a Python file, create a frequency table of the letters in the English language. You can try to find this yourself, but if you don’t feel like it, we understand!
If you absolutely want to find it yourself, you can do as in Task 2.2, but on a very large text.
English Letter Frequency (Answer)
english_letter_frequency = {
'E': 12.70,
'T': 9.06,
'A': 8.17,
'O': 7.51,
'I': 6.97,
'N': 6.75,
'S': 6.33,
'H': 6.09,
'R': 5.99,
'D': 4.25,
'L': 4.03,
'C': 2.78,
'U': 2.76,
'M': 2.41,
'W': 2.36,
'F': 2.23,
'G': 2.02,
'Y': 1.97,
'P': 1.93,
'B': 1.29,
'V': 0.98,
'K': 0.77,
'J': 0.15,
'X': 0.15,
'Q': 0.10,
'Z': 0.07
}
Task 2.2 - Counting the Frequency of Letters in Text
Now we will create an algorithm to find the frequency of letters in a given text.
Tips for procedure
- Begin with a function that takes in a text (can be anything).
- In the function, create a “dictionary” (Python Dictionaries), with entries for each letter of the alphabet, set to
0. ({'A' = 0, 'B' = 0, 'C' = 0, ..., 'Z' = 0}) - Go through the entire text and count each letter (increase by 1 in the corresponding entry in the dictionary). Here you should probably ignore characters that are not letters, also remember uppercase and lowercase letters.
- Keep track of how many letters have been counted in total.
- When you are finished counting, divide
/each value in the table by the length of the text and then multiply by 100, this will give you a percent frequency. (You can of course let your table be between 0 and 1 instead). - Now you should have a frequency table for the text.
Task 2.3 - Compare the frequency of a text with the actual frequency
When you have found the frequency of all the letters in the text, you can create a function that finds the “distance”. Huh? What is meant by that?!
You can imagine that the frequency of, for example, E in the text will be a number. You can find the “distance” this has with the actual frequency, which is 12.70. Example: The frequency is 9.63, what is the distance? The distance will be the absolute value (negative numbers become positive) between these two values: \(12.70 - 9.63 = 3.07\).
Create a function that goes through each of the letters and finds the distance. Then add all the distances together into a “total” distance.
Math function?
If you’re wondering what the math function for this looks like, it’s as follows:
\(\sum_{n=0}^{N} \lvert a - b\rvert\)
Tips for procedure
- Use a
for-loop to go through the entire frequency table. - For each letter in the frequency table, find the absolute value compared to the actual frequency. Use the
abs()function in Python for this. - Add together all the values, and you should get an end result.
Task 2.4 - “Crack” a Caesar Cipher
Now we will finally put together everything we have done so far! Now we will “crack” a Caesar cipher.
Create a program that “cracks” a Caesar cipher! Without user influence, you should be able to throw in an encrypted text and retrieve the decrypted text without needing a key.
Test data
Here is some test data you can use, what do these say?
| Test-data |
|---|
cqrbvnbbjpnrbjenahbnlancxwnqxynoduuhhxdjanjkuncxmnlxmnrclxvyuncnuhjwmqnanjanbxvnfxamboaxvxdaojexarcnsnmrqnuuxcqnanrcbxenajwjtrwrqjencqnqrpqpaxdwmhxdfnanarpqccqnwnpxcrjcrxwbfnanbqxac |
lsaizivxlmwqiwwekimwuymxiwlsvxwsmxqmklxrsxasvoewibtigxihlsaizivmjmxhsiwksshnsf |
bmtxymjwjsfdfsxbjwrjxyfsifsizsktqidtzwxjqkqtslqnajymjpnslgfwsfwitmjdtzhtrjrtxyhfwjkzqqdzutsdtzwmtzwynxstbxywzhpybjqajljyymjjytgjikwfshnxhtktwymnxwjqnjkrzhmymfspxynxgnyyjwhtqifsinfrxnhpfymjfwymfajdtzmfivznjylzfwistyfrtzxjxynwwnslbjqqlttisnlmynkdtzitrjjymtwfyntfsirfwhjqqzxymjwnafqxtkrdbfyhmgniymjrrfpjmfxyj |
zwkyvivrivrepzuzfkjzekyviffdnzcckyvpgcvrjvjkreulgjrzukyvjritrjkztkvrtyvirwkvircfexjzcvetvfevwivjydreifjvkfyzjwvvkefnkyvedzjkvinypufpfltfejzuvipflijvcwrezuzfkzehlzivukyvkvrtyvinzkyrjevvinvccrtklrccpzufekjrzukyvjkluvekslkzyrkvkfjvvpfljkreuzexlgkyvivrccsppflijvcw |
uwwilxchaniuffehiqhfuqmizupcuncihnbylycmhiqusuvyymbiofxvyuvfynizfscnmqchamulyniimguffniayncnmzunfcnnfyvixsizznbyaliohxnbyvyyizwiolmyzfcymuhsqusvywuomyvyymxihnwulyqbunboguhmnbchecmcgjimmcvfysyffiqvfuwesyffiqvfuwesyffiqvfuwesyffiqvfuweiibvfuweuhxsyffiqfynmmbueycnojufcnnfyvullsvlyuezumncmlyuxswigcha |
Tips for procedure
- Begin by creating a function that takes in text.
- Use the decryption function for Caesar cipher with rotation N on the text, N starts at 0.
- Create a frequency table of the result
- Determine the distance of the result in relation to the actual frequency table
- Either: a) keep track of the distance in a list, or b) keep track of the smallest value and the rotation (this becomes the key)
- Increase the rotation by 1 and repeat steps 2 to 6 until N reaches 26 (full rotation).
- Return the decrypted text, i.e. the smallest distance is the correct key.

