CS91R Lab 00: Caesar Ciphers

Due Wednesday, January 29, before midnight

Goals

The goals for this lab assignment are:

  • Make sure you can access the CS lab remotely using vscode.

  • Set up your git repository

  • Remember how ASCII works

  • Get (re-)acquainted with UNIX

  • Use command-line parameters and standard input

  • Use pipes to connect together UNIX programs

  • Build a simple Caesar cipher encoder/decoder

Accessing the lab using vscode

The easiest way to connect to the CS machines from your own laptop is to install Visual Studio Code and set it up to remotely access your files. To do this, follow the instructions on how to setup Visual Studio Code for remotely accessing the CS department.

You can also remotely access the CS lab machines using ssh. However, you cannot easily run the code editor remotely connecting in this way. Directly ssh’ing into CS lab machines is useful if you want to run shell commands, or if you use a different editor like vim or emacs.

This tutorial covers getting your ssh keys setup so you don’t have to type your password every time.

Cloning your repository

Log into the CS91R-S25 github organization for our class and find your git repository for Lab 00, which will be of the format lab00-user1-user2, where user1 and user2 are you and your partner’s usernames.

You can clone your repository using the following steps while connected to the CS lab machines:

# create your cs91r/labs directories if you have not done so
$ cd
$ mkdir cs91r
$ cd cs91r
$ mkdir labs

# cd into your cs91r/labs sub-directory and clone your lab00 repo
$ cd ~/cs91r/labs
$ git clone git@github.swarthmore.edu:CS91R-S25/lab00-user1-user2.git

# change directory to list its contents
$ cd ~/cs91r/labs/lab00-user1-user2

# ls should list the following contents
$ ls
message.txt  README.md  sample.cast  sample.gif

Bash History

We’ll be learning all types of command-line tricks this semeser. One very useful thing about the command-line, we will be using, bash, is that it keeps track of a history of the UNIX commands you have typed previously. You can use the up/down keys to scroll through this history, and you see the history of what you have entered before by typing history in the bash shell ($). We will check in throughout the semester to see how many new things you have learned by looking at your history histogram.

For now, make a copy of your current history file to compare later.

$ cp ~/.bash_history ~/.bash_history0

Caesar Ciphers

A Caesar Cipher, also known as a shift cipher, is a primitive technique for encrypting (and decrypting) messages. Your task in this lab is to build a Python program that applies the cipher and figure out what is in this secret message.

$ cat message.txt
W ykilqpan💻 sehh zk sdwp ukq pahh ep pk zk, xqp pdwp iwu xa iqyd
zebbanajp bnki sdwp ukq dwz ej iejz🧠.

- Fkoald Saevajxwqi

Your program should encode each character by applying a shift, but only characters from the alphabet, not numbers or other types of characters like tabs or new lines.

Consider the following examples, characters typed on the keyboard and read by python’s input() are in bold:

$ python3 caesar.py
(Provide an error message to let the user know they should provide a shift)
$ python3 caesar.py 1
jkl 123
klm 123
$ python3 caesar.py 2
xyz-XYZ
zab-ZAB
$ python3 caesar.py -1
backward
azbjvzqc
$ python3 caesar.py 0
zero doesn't change anything
zero doesn't change anything

We can use UNIX to grab the input from a file or command rather than having to type it in via the keyboard. For example, we an use the file message.txt for input (this is called redirecting standard input):

$ python3 caesar.py 0 < message.txt
W ykilqpan💻 sehh zk sdwp ukq pahh ep pk zk, xqp pdwp iwu xa iqyd
zebbanajp bnki sdwp ukq dwz ej iejz🧠.

- Fkoald Saevajxwqi

We can also use a pipe (|) to connect the output of one program to the input of a another.

$ whoami | python3 caesar.py 1
sjdilfjui

This kind of pipe (function composition really) gives us a handy way of testing our program, by encoding the text and then immediately decoding.

$ echo 'hello' | python3 caesar.py 2 | python3 caesar.py -2
hello

For example, if our program is working this should show the same message for all 26 possible shifts

$ for s in {0..25} ; do echo 'hello' | python3 caesar.py $s | python3 caesar.py -$s ; done

hello
hello
hello
hello
...
hello

Command-Line Parameters and Standard-Input

In order to provide caesar ciphers with different shift amounts, we take a parameter on the command-line. In python, you can access it via the list sys.argv. The following program just echoes back to you what you pass on the command-line. Below, we use the keyword argument end to tell python to print a space instead of a newline.

import sys
for v in sys.argv:
    print(v, end=" ")    # end the line with a space instead of newline

Another way to get input into our programs, especially long streams of input, is to use standard input. If you used input() before in your Python programs, that was using standard input. The following program snippet looks similar, but how does it behave differently?

import sys
for v in sys.stdin:
    print(v)

Note: you can press ctrl-d to send the end-of-file marker via the keyboard, when you are done typing input.

Try on other texts

In the file /data/cs91r-s25/misc/winter.txt, there is a short paragraph written in French.

  • What happens if you try to run your cipher on that text?

  • Propose a solution.

  • Optional: Implement your proposed solution and see if it works!

In the file /data/cs91r-s25/demos/encodings/masha_U.txt, there is a short paragraph written in Russian.

  • Does your proposed solution still work?

  • If necessary, propose a new solution.

  • Optional: Implement and test your updated solution!

How to turn in your solutions

Submit the caesar.py program and provide a decrypted message in dmessage.txt. Show us how you cracked the code in the command-line by using asciinema to record a terminal session and include it your README.md file.

For example, here is an asciinema recording of the awk examples shown above:

To record your session in asciinema, use the following command:

$ asciinema rec -i 2 cc.cast

You can type exit or press ctrl-d to stop the cast session. When your session is over, convert it to a .gif file:

$ agg cc.cast cc.gif

Add any .cast and .gif files to your repository.

You can name your .cast files anything you’d like, but you will need at least one of them to include in your writeup. Do not worry if you make typos as you are working in a recording: we are not evaluating your ability to type!

Survey

Please complete the lab survey within one day of submitting the lab.