CSC 226

CSC 226 logo

Software Design and Implementation

A14: CaesarCipher

Ciphers and Cryptography

You may work alone or with a pair partner for this. If you work with a partner, be sure to follow good "pair-programming" practices. For this assignment you will be adding to a class.

Ciphers and a little history

Encryption has been in the news a great deal recently:
The Google article proclaims the following:

"Google has begun routinely encrypting Web searches conducted in China, posing a bold new challenge to that nationís powerful system for censoring the Internet and tracking what individual users are viewing online.

The company says the move is part of a global expansion of privacy technology designed to thwart surveillance by government intelligence agencies, police and hackers who, with widely available tools, can view e-mails, search queries and video chats when that content is unprotected.

Encryption and privacy are clearly big news.† But, how do encryption and decryption work?

Suppose you intercepted an email between two people that was using some kind of encryption, thereby creating a cryptogram:

pyhv ngyvr owf nrjrw urovn omy yhv posbrvn qvyhmbs pyvsb yw sbxn gywsxwrws, o wrt wosxyw, gywgrxjrf xw kxqrvsu, owf frfxgosrf sy sbr dvydynxsxyw sbos okk lrw ovr gvrosrf rzhok.

wyt tr ovr rwmomrf xw o mvros gxjxk tov, srnsxwm tbrsbrv sbos wosxyw, yv owu wosxyw ny gywgrxjrf owf ny frfxgosrf, gow kywm rwfhvr. tr ovr lrs yw o mvros qosskr-pxrkf yp sbos tov. tr bojr gylr sy frfxgosr o dyvsxyw yp sbos pxrkf, on o pxwok vrnsxwm dkogr pyv sbynr tby brvr mojr sbrxv kxjrn sbos sbos wosxyw lxmbs kxjr. xs xn oksymrsbrv pxssxwm owf dvydrv sbos tr nbyhkf fy sbxn.

qhs, xw o kovmrv nrwnr, tr gow wys frfxgosr -- tr gow wys gywnrgvosr -- tr gow wys bokkyt -- sbxn mvyhwf. sbr qvojr lrw, kxjxwm owf frof, tby nsvhmmkrf brvr, bojr gywnrgvosrf xs, pov oqyjr yhv dyyv dytrv sy off yv frsvogs. sbr tyvkf txkk kxsskr wysr, wyv kywm vrlrlqrv tbos tr nou brvr, qhs xs gow wrjrv pyvmrs tbos sbru fxf brvr. xs xn pyv hn sbr kxjxwm, vosbrv, sy qr frfxgosrf brvr sy sbr hwpxwxnbrf tyvc tbxgb sbru tby pyhmbs brvr bojr sbhn pov ny wyqku ofjowgrf. xs xn vosbrv pyv hn sy qr brvr frfxgosrf sy sbr mvros sonc vrloxwxwm qrpyvr hn -- sbos pvyl sbrnr bywyvrf frof tr socr xwgvronrf frjysxyw sy sbos gohnr pyv tbxgb sbru mojr sbr kons phkk lronhvr yp frjysxyw -- sbos tr brvr bxmbku vrnykjr sbos sbrnr frof nbokk wys bojr fxrf xw joxw -- sbos sbxn wosxyw, hwfrv myf, nbokk bojr o wrt qxvsb yp pvrrfyl -- owf sbos myjrvwlrws yp sbr drydkr, qu sbr drydkr, pyv sbr drydkr, nbokk wys drvxnb pvyl sbr rovsb.

Looks like gibberish, right? Actually, what was used was a simple cipher-substitution in which each letter ("a" through "z") in the original text was replaced by a different, random one. The cryptogram above, for example, has had every occurrence of the letter 'e' replaced with an 'r', every occurrence of 'w' by a 't', and so forth. The word "tr", which is the sixth word in the last paragraph, was therefore originally "we". The trick is to find which letter substitution was used and you can then reverse the process to get the original message.

Though not the first cipher, one of the simplest and most notable ciphers is the Caesar cipher, used by Julius Caesar to send messages to his generals during war. The cipher was simple; each letter is simply shifted in the alphabet by a number, which Caesar and the general would agree upon ahead of time. So a shift of +3 would equate to 'a' becoming 'd', 'b' would become 'e', and so on. The following Class demonstrates a Caesar cipher (be sure to download all files and place them in the same directory):

CRC Card for the CaesarCipher

Class name: CaesarCipher
Class Methods: Class Collaborations (other classes):
  • __init__() constructor for the CaesarCipher class
  • import_file() imports a file stored in the variable self.input_file and imports a string representing the contents of the file
  • export_file() exports a file
  • encrypt() converts an original message into a ciphered message which is returned with each letter shifted to the right by the key
  • decrypt() converts a ciphertext and returns the original message which is found by shifting each letter to the left by the key
  • testit() prints the result of a unittest.
  • CaesarCipher_test_suite() provides tests for the CaesarCipher class
  • None
Class Data: Class Collaborations (other classes):
  • self.alphabet is "ABCDEFGHIJKLMNOPQRSTUVWXYZ" which is the alphabet used to do our shifts
  • self.input_file is the file to be encrypted or decrypted
  • self.key is the integer amount each message/cipher will be shifted
  • self.message holds the message
  • self.cipher holds the cipher
  • self.crypt_type holds either "encrypt" or "decrypt"
  • None

The instructions

Modify the file to complete the following tasks:

Caesar has two letters to send (be sure to download these files into the same directory as your code!):

Complete the code in main() to encrypt the above messages and generate two new encrypted files:

  • cipher_to_friend_1.txt
  • cipher_to_friend_2.txt

Caesar also has multiple letters from a friend which he needs to decrypt (again, be sure to download this file into the same directory as your code!). Here is one of these letters. Note that your code should be able to work with any letter which was encrypted using the Caesar cypher:

The CaesarCipher Class is incomplete; while the program can encrypt a message, the program should also be able to decrypt the message. Complete the following tasks related to decryption:
  • Complete the decrypt() function for decrypting the message inside the CaesarCipher Class.
  • Once you've completed the decrypt function, make the correct calls in the main() function to construct a new CaesarCipher object and decrypt the message.
  • Use the export_file() function to write your decrypted message to a file named "message_from_friend_3.txt"
Suggestions and Requirements:
  • Be sure to modify the standard header at the top of your program with name, username, assignment number, purpose and acknowledgements.
  • Use only meaningful Class, variable and function names.
  • Use comments to clarify sections of code
  • The highest level of the program (i.e., no indenting) must only contain the following:
    • the header
    • any import statements
    • Class definitions
    • a call to the main() function

When you are finished writing and testing your program, create a ZIP file named and submit the following to the A14 link on Moodle:

  • cipher_to_friend_1.txt
  • cipher_to_friend_2.txt
  • message_from_friend_3.txt

Submitted files with incorrect filenames may not receive full credit because it makes grading more difficult for us and the TAs, so please check filenames carefully!

Copyright © 2016 | | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License