copyright Steve J. Hodges

CS19 Fall 2017

Assignment 6 (Codebreaker!)

Project Directory


Resulting Executable


Description of the program

In this project you will be writing a program that encrypts and decrypts secret messages using an actual code that was used by an agent in South America during World War Two.


This is a program with many parts. Even though you should know what you need to do for each step of the process after reviewing the text of the assignment, it may not be immediately clear to you how to do each step. Please ensure that you start the assignment early enough so that you have time to work through each of the issues that will come up during the assignment!

Program Notes

This program is going to use two classes called BookCode and Cryptor. These classes should be easy-to-use and work efficiently.

BookCode class

The BookCode class should have a constructor that takes a string filename parameter for a wordlist. A wordlist file is a series of single words followed by an integer (separated by one single space) with one entry per line. The words will be all lowercase, and no integer value will be duplicated.

BookCode will have a public function named lookup(int). This function should accept an integer parameter, and return the corresponding string from the word list. This function should return an empty string if the integer doesn't have a matching string entry.

A sample file named codebreaker-words.txt has been copied to your home directory on Pengo. Your program should always open the file as specified on the command line.

To support efficient lookup, your BookCode class must use a Map (C++ Standard Template Library.) There will be some discussion of this standard data structure in class, but you will need to do some additional research!

Cryptor class

Create a second class called Cryptor. This class will use a BookCode object to lookup keywords, pass the filename of the wordlist to the BookCode object upon creation.

Your function should have two public functions named encrypt and decrypt. encrypt has two parameters: an int that represents a page, and a string to encode. decrypt has one parameter, a string to decode. Both functions should have a string return type which would be the encoded or decoded string.

Your Cryptor class will need a two dimensional array (type char would work) and you'll need to determine how you will represent the letters a-z, a way to indicate that a spot is currently empty, and a way to indicate that a spot has been "blocked out."

Do your best to minimize the number of public functions in the Cryptor class. Add as many helper functions as you need.

Other comments

The text to encode will be any string characters, you will need to convert the initial input to lowercase text only. The output of the decrypt function and the input to the decrypt function wil be a string consisting of a series of five lowercase character sequences each separated by a single space.

Don't forget destructors for your classes, if/where necessary.

Details of the encoding/decoding technique

Review this document codebreaker.pdf for the details on how this code works!

Because one or more letter xs may be appended to the message to be encoded, it would be hard to fully retrieve a message that itself ends with the letter x. To simplify the assignment, initial messages input won't end with the letter x. So for example I would encode "X marks the spot" and not "The spot is marked by x."

Sample Run

[steveh@pengo Codebreaker]$ ./p6 codebreaker-words.txt       
Codebreaker! Sample Solution by Steve J. Hodges
(e)ncrypt or (d)ecrypt? e
page number? 155
string to encrypt? Four score and seven years ago
encrypted message:
bmqbh nwlrb xaexe frsyg oeeou erava srcne sodna 
[steveh@pengo Codebreaker]$ 

[steveh@pengo Codebreaker]$ ./p6 codebreaker-words.txt 
Codebreaker! Sample Solution by Steve J. Hodges
(e)ncrypt or (d)ecrypt? d
string to decrypt? bmqbh nwlrb xaexe frsyg oeeou erava srcne sodna
decrypted message:
[steveh@pengo Codebreaker]$ 

[steveh@pengo Codebreaker]$ ./p6  
p6: must specifiy book code word file
[steveh@pengo Codebreaker]$
uahlz kakoi xxbkb goehe pscns oasgc rdtsu opaes torda umsoo tdbto doyds somem greal dhnyw fedoe aeede efano eeuto ynjui cgaei awund gdkmh dotoa nrext midpi webei uibrg estse tlcen iyelg rdset ttrre shlln etwra istrm onayn anhoe sttxx