### CS27 : Fodszqujpo!

Since the dawn of time, man has wished to communicate in secret. In ancient Roman times, men used to shave their heads and have secret messages tattooed onto them, in the second world war, Germany communicated with the Enigma machine. In modern times, cryptography is crucial to the operation of the Internet.

We are learning ...
So that we can ...
• Describe Cryptography
• Explain how to implement the Caesar cipher (poor)
• Explain how to implement the Vernam cipher (excellent)
• Compare encryption techniques to assess their effectiveness.

 Activity 1 What is cryptography?

"The science of protecting data by scrambling the characters based on rules is called Cryptography. The scrambling method is called a cryptosystem or a cipher. The process of converting the original (plain text) data into the scrambled (ciphertext) version is called encryption. The opposite process (converting ciphertext into plaintext) is called decryptionA cryptographer is someone who studies the science of cryptography. A cryptanalyst is someone who studies other peoples scrambled messages with the intention of decrypting them through application of brute force methods or via analytical methods like frequency analysis. All ciphers use a key which instructs the encryption algorithm how to scramble the plaintext. In symmetric ciphers, the same key is used to both encrypt and decrypt the message and transmission of the key is a point of weakness in these systems. In asymmetric ciphers, different keys are used to encrypt and decrypt the message."

 Task 1.1 See, they were right!In your notebooks : Write definitions for all 8 key terms from the passage above. You may need to do some extra research to find out details for some of them but don't worry about going into too much depth :)OUTCOME : Definitions concerning cryptography!

 Activity 2 The Caesar Cipher

One of the earliest documented encryption systems is called the Caesar Cipher, so named because it is believed that it was used by Julius Caesar over two thousand years ago.

The Caesar Cipher is an example of a shift cipher because the plaintext characters are shifted to create the ciphertext characters. The encryption key for Caesar's secret system was simply 3 - the number of places to shift the letters.

Caesar never had to change the key for the reasons he has explained above, but even if he did, there are only 26 possible messages which could result from any one ciphertext - hence, shift ciphers are easy to decrypt and are therefore not secure (in their simplest form).

 Task 2.1 Cipher wheelCreate your own cipher wheel using the resource called Caesar Cipher Wheel (click to download). Make sure you position the inner wheel so that the slot lines up with the 'A' on the outer wheel!To encrypt : Rotate the middle wheel to the correct value for the `key`, locate the plaintext letter on the outside wheel and the ciphertext letter from the middle wheel.In your notebooks : Encrypt the following words ...`COMPUTING` ... key = 5`IS` ... key = -10`FUN` ... key = 14To decrypt : Either set the middle wheel to the correct value of `key`, locate the ciphertext letter on the middle wheel and read the plaintext letter from the outside wheel or set the middle wheel to `26-key`, locate the ciphertext letter on the outside wheel and read the plaintext letter from the middle wheel.In your notebooks : Decrypt the following words ...`WKH` ... key = 3`WUYMUL` ... key = -6`RXEWTG` ... key = 15`KU` ... key = 2`BGLXVNKX` ... key = 19 Download the script caesar.py and open it in using the Python programming environment, IDLE. Print out a copy for your notebooks.Look carefully at the script (or use the image below). The function, `caesar`, takes three parameters, the `message`, the `key` and a `decode` flag (which defaults to `False`).Try to encode and decode a message to make sure you understand how to use the `caesar` function. Provide evidence for your notebooks to show you have done this.In your notebooks : Explain the operation of the line ...`text = text + chr((ord(letter) - 65 + int(key)) % 26 + 65)`... particularly the use of the modulus arithmetic operator, `%`. What function does this operator perform? OUTCOME : Evidence that you can encrypt and decrypt messages using a Caesar Cipher plus evidence of a certain level of understanding of the function of the modulus arithmetic operator in this context.

 Activity 3 The Vernam Cipher

The Vernam cipher or the 'perfect cipher', is a crypto-algorithm where plaintext is combined with a randomly generated, variable length key. Each letter in the plaintext is encrypted with an individual, single use key which is chosen at random and never used twice.

Original patent document available in lesson resources

It is the only existing mathematically unbreakable cipher as long as ...
• There are only ever two copies of the key;
• Both sides of the communication link have the same key;
• The key is used only once;
• The key is destroyed immediately after use;
• The key contains truly (not pseudo) random data.
Technically, the original Vernam cipher is an example of a stream cipher where the plain text is logically combined with 'key stream' to generate the cipher text. This process happened electronically; originally, punched paper tape was used.

Click to engage

A key tape generator was used to produce two identical tapes - one for encryption and one for decryption.

Unashamedly stolen from Cryptomuseum

As with any symmetrical cipher, the only problem is transferring / concealing the key tapes, but this is the only weak point in the encryption method  ...

From "Untangling the Web" - Royal Institution Christmas Lectures

 Task 3.1 The original Vernam stream cipher You will be provided with an activity sheet called Vernam Cipher (or feel free to download it yourself) which explains how the original OTT Vernam stream cipher works and also gives you an exercise to carry out your own encryption. Your teacher will show you how to complete the sheet. OUTCOME : Completed ciphertext from the stream cipher on the worksheet.

The Vernam cipher led to the development of the One Time Pad, commonly in use during wartime.

One Time Pad used by soldiers in the second world war.
The table on the left is a reciprocal alphabet table.
The book on the right contains a series of unique one time pads.

The One Time Pad encrypts using a series of random letters using either modulus-26 mathematics (Vigenere tables) or encryption / decryption tables which use reciprocal alphabets. Again, two copies of each pad are made and shared between sender and receiver (the only weak point). Each pad is used only once and then destroyed.

 Task 3.2 One Time Pad You will be provided with a copy of the activity sheet called One Time Pad Encryption (or you can download it yourself) which allows you to perform a One Time Pad (OTP) encryption (obvs). Your teacher may show you how to complete the sheet, if you're lucky. OUTCOME : Completed ciphertext example.

 Activity 4 Cipher strength

Some encryption techniques are better then others. Here, the word 'better' means 'harder for a cryptanalyst to decrypt' (without knowledge of the key). Consider the following frequency analysis on message containing 6659 characters (you'll meet this later).

Letter frequency for natural text, sample text encrypted using a caesar cipher
and the same text encrypted using a vernam cipher. Click to enlarge.

As you can see, it is obvious from the letter frequency analysis for the caesar cipher what key has been used (10) as the 'E' is shifted 10 letters to the right. A cryptanalyst could decrypt this message, instantly! However, the same message encrypted with a Vernam cipher essentially normalises the letter frequency distribution. There is no pattern in the distribution and no way of decrypting the message (without the key of course!)

Why are you bothering?

A brute force attack on this cipher would be pointless ...
• Encrypt the word 'SECRET' using a random 6 character one time pad.
• Each letter has 26 potential keys.
• There are 6 letters in the message so there are 626 or 170,581,728,179,578,208,256 potential one time pads!
• Even checking 1,000,000 one time pads per second, it would still take us just over 5,409,111 years to check every one but in the process, we would generate every 6 letter word in the English language at some point and would be none the wiser as to the original message! Pointless!

 Task 4.1 Perform your own frequency analysisDownload the Python script vernam.py. Save this to a suitable place in your user area and open it with the Python programming environment, IDLE. The script uses class programming.Well, you create a vernam cipher object using `obj = vernam()` (where `obj` is some identifier of your choice) and use the object to perform encryption. The class contains the following attributes ...`obj.original`The original message including all illegal characters.`obj.message`The cleansed message to encrypt which only contains uppercase letters.`obj.pad`The one time pad which is used to encrypt the message.`obj.encrypted`The encrypted message.`obj.decrypted`The decrypted messageThe class provides the following methods ...`obj.generatepad(length=None)`Generate a pseudorandom one time pad of length `length`. This method is used by the `obj.encrypt` method to generate a one time pad of the same length as the message. The shortest pad you can generate is length of one and you can't generate a pad if there is no message and you don't specify a length. There is no additional benefit to generating a pad which is longer than the intended message.`obj.clearpad`Clear the one time pad from memory.`obj.loadpad(filename=None)`Load a one time pad from a file or from the console (if the filename is left blank). The one time pad is cleansed by removing all characters apart from the uppercase alphabet including spaces and line breaks. If the one time pad is shorter than the message, the encryption algorithm wraps around the pad.`obj.savepad(filename)`Save the one time pad to a text file. Filename must be supplied.`obj.clearmessage`Clear the message from memory.`obj.loadmessage(filename=None)`Load a message from a file or from the console (if the filename is left blank). The message is cleansed by removing all characters apart from the uppercase alphabet including spaces and line breaks. Any existing one time pad, encrypted and decrypted messages are cleared.`obj.savemessage(filename)`Save the (cleansed) message to a text file. Filename must be supplied.`obj.encrypt`Encrypts the message, if it exists. If there is no one time pad, it is generated automatically with the same length as the message or else an existing one time pad is used.`obj.saveencrypted(filename)`Save the encrypted message to a file. Filename must be supplied.`obj.encryptedtomessage`Transfer the `encrypted` message to `message`. Useful if you want to decrypt a message you have just encrypted.`obj.decrypt`Decrypt a message if it exists and there is an existing one time pad.`obj.savedecrypted(filename)`Save the decrypted message to a file. Filename must be supplied.`obj.frequency`Generate a letter frequency table for both the `message` and the `encrypted` message.Warning : If you run the script before you carry out the next task, you will get an error!Download the text file chapter_plain.txt and save this in the same folder as you saved the vernam.py script. This contains the first chapter from Around the World in 80 Days by Jules Verne. Read it if you like.Now, run the vernam.py script.The script will (ready?) create a cipher object called `cipher`, load the original text (chapter_plain.txt), cleanse it by removing all characters except uppercase letters, generate a pseudorandom one time pad, encrypt the cleansed message and generate a frequency table which shows the letter frequency distribution for the original, cleansed message and one for the encrypted message. For this vernam cipher, the encryption key (one time pad) is the same length as the original message - 6659 characters. Your frequency table will look different to mine because your key will be different.At the prompt : Issue the following commands ...`>>> cipher.original``>>> cipher.message``>>> cipher.pad``>>> cipher.encrypted`... and look carefully at what is generated.For the next two tasks, document what you have done and what you have learnt using a suitable combination of screenshots and written descriptions in a word processed document.Now create your own encrypted message. The (rather complicated) workflow is as follows ...1. Create a vernam cipher object [ `obj=vernam()` ]2. Specify the message to encrypt or decrypt by either :        Loading an existing text file [ `obj.loadmessage('message.txt')` ]        Specifying the message manually [ `obj.loadmessage()` ]3. Optionally, specify a bespoke one time pad by either :        Creating a random one time pad [ `obj.generatepad(length)` ]        Loading an existing text file [ `obj.loadpad('pad.txt')` ]        Specifying a one time pad manually [ `obj.loadpad()` ]4. Either :        Encrypt the message [ `obj.encrypt` ]        Decrypt the message [ `obj.decrypt` ]5. Perform a letter frequency analysis - [ `obj.frequency` ]6. You can also ...        View the original message [ `obj.original` ]        View the cleansed message [ `obj.message` ]        View the one time pad [ `obj.pad` ]        View any encrypted message [ `obj.encrypted` ]        View any decrypted message [ `obj.decrypted` ]        Transfer the encrypted message to message [ `obj.encryptedtomessage` ]        Save the message [ `obj.savemessage(filename)` ]        Clear the message [ `obj.clearmessage` ]        Save the one time pad [ `obj.savepad(filename)` ]        Clear the one time pad [ `obj.clearpad` ]        Save the encrypted message [ `obj.saveencrypted(filename)` ]        Save the decrypted message [ `obj.savedecrypted(filename)` ]Perform a random caesar cipher on the same message using the vernam class by generating a one time pad of length one and then re-encrypting the same message. Perform a frequency analysis on the encrypted message and you should see the shift clearly in the numbers. Document the original message and the frequency table which it generates.Now print out your word processed document for your notebooks.OUTCOME : Evidence that you can use the vernam cipher script and that you have performed some encryption and some frequency analysis of your own.

 Extension Activities