ISF DP Computer Science

Shift Ciphers #

In this lab you will continue to practice functions and are introduced to modulo and file handling.


Syllabus Topics [SL] #

  • B2.5.1 Construct code to perform file-processing operations.

Key Vocabulary #

WordDefinition
EncryptionConverting plain text into a secure format, cipher text, that cannot be easily understood by unauthorized people.
Encryption KeyA string of characters or numbers used by an encryption algorithm to encode or decode data.
ModuloAn operation that returns the remainder of a division.
PathLocation of a file

[0] Set up #

๐Ÿ’ป Go to your dpcs/unit01_cryptography folder.

cd ~/desktop/dpcs/unit01_cryptography/

๐Ÿ’ป Clone your repo and go into the directory. Be sure to replace yourGithubUsername with your actual username.

git clone https://github.com/isf-dp-cs/lab_shift_ciphers_yourGithubUsername
cd lab_shift_ciphers_yourGithubUsername

๐Ÿ’ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D


[1] File Handling #

In this lab you will use file handling techniques to encrypt and decrypt large text files.

๐Ÿ“– To read a whole file

file = open('example.txt', 'r')
file.read()
file.close()
  • open() - opens a file in a specific mode, if the file does not exisit it creates a new file
  • 'example.txt' is the name of the file you want to open or create
  • 'r' represents the mode. important modes to remember are:
    • 'r' - read the text
    • 'w' - write over existing text
    • 'a' - append text to the end of the file
  • read() - returns all text in the file
  • close() - closes the file

๐Ÿ“– To read a single line

file = open('example.txt', 'r')
file.readline()
file.close()

๐Ÿ“– To read a file line-by-line

file = open('example.txt', 'r')
for line in file:
    print(line)
file.close()

๐Ÿ“– To add to an existing file

file = open('log.txt', 'a')
file.write('A new entry. \n')
file.close()

๐Ÿ“– To write to a new file

file = open('new_document.txt', 'w')
file.write('Hello world')
file.close()

๐Ÿ’ป Open file_handling.py and construct code to perform the following actions.

  1. Open song.txt, read the file, and print the text
  2. Append the last line of the song. Be sure it is appended on the next line.
    • last line: You're my soda pop, gotta drink every drop
  3. Create a new file capitalized_song.txt with lyrics of the song in all capital letters

[2] Modulo #

Python has many operators that allow you to perform calculations with values. You’ve probably seen and used the basic ones like +(add), - (subtract), * (multiply), and / (divide).

However, Python has other operators that can be really helpful.

One such operator is the modulo operator (%). This operator takes two values, divides them, and returns the remainder of the division.

For example:

5/2 has a remainder of 1

5%2 = 1

Here are some more modulo examples:

print(5%2)
>> 1
print(3%3)
>> 0
print(6%2)
>> 0
print(9%2)
>> 1
print(3%4)
>> 3

[3] Caesar Cipher #

The caesar cipher is a type of substitution cipher used by ancient Romans. It takes a message, the plain text and transforms it by shifting each letter by a set value, the encryption key.

For example imagine that the alphabet is shifted by 3.

ABCDEFGHIJKLMNOPQRSTUVWXYZ
DEFGHIJKLMNOPQRSTUVWXYZABC

The plain text "beg" with the encryption key 3, becomes "ehj".

  • b shifts by 3, becoming e
  • e shifts by 3, becoming h
  • g shifts by 3, becoming j

๐Ÿ’ป In caesar_cipher.py, construct the function decrypt_caesar_cipher() to decrypt a message that has been encrypted by a caesar cipher.

๐Ÿ’ป Test your decryption function decrypt_caesar_cipher() on words and short phrases.

๐Ÿ’ป Use your decrypt_caesar_cipher() function and file handling methods to decrypt the message in caesar_encrypted_text.txt. You should then create a new file with the decrypted text.

โœ… Check your work by opening the created file and ensuring it makes sense as English text. Do you recogonize the text?


[4] Vigenere Cipher #

The Vigenere cipher is another substitution cipher. It takes a message, the plain text and transforms it by shifting each letter by a set value according to a repeating encryption key. Unlike the caesar cipher, the encryption key is a string.

For example, imagine that encryption key is 'be'.

be
15

'b' is number 1 in the alphabet and 'e' is number 5.

Therefore, we will shift our letters by 1 and 5, in an alternating pattern. For example the plain text "apple" with the encryption key "be", becomes "buqpf".

a->bshift by 1
p->ushift by 5
p->qshift by 1
l->qshift by 5
e->fshift by 1

๐Ÿ’ป In vigenere_cipher.py, construct the function decrypt_vigenere_cipher() to decrypt a message that has been encrypted by a vigenere cipher.

๐Ÿ’ป Test your decryption function decrypt_vigenere_cipher() on words and short phrases

๐Ÿ’ป Use your decrypt_vigenere_cipher() function and file handling methods to decrypt the message in "vigenere_encrypted_text.txt". The encryption key is the encryption key from the caesar_cipher problem written in English (e.g. 1 is one). You should then create a new file with the decrypted text.

โœ… Check your work by opening the created file and ensuring it makes sense as English text. Do you recogonize the text?


[5] Deliverables #

โšกโœจ Once you complete the lab, be sure to complete these two steps:

โœ๏ธ Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.

๐Ÿ’ป Push your work to Github

  • git status
  • git add -A
  • git status
  • git commit -m "describe your code here"
  • git push
  • git remote