For this problem, you will write a program to encrypt and decrypt a transposition cipher.

This is an individual assignment. Write your own solution and do not work in a pair/group on this program.

Files:

Starter files:

You will edit and turn in only the following file(s). The ZIP contains other files/libraries; do not modify these. Your code must work with the other files unmodified.

  • icon transpCipher.cpp, the C++ code for your solution
  • icon encrypted.txt, a text file with your own encrypted text that your program encrypted with the key "stanford".

Demo JAR:

  • icon Cipher demo JAR

    "How do I run the assignment solution demos?"

    Our assignments offer a solution 'demo' that you can run to see the program's expected behavior. On many machines, all you have to do is download the .jar file, then double-click it to run it. But on some Macs, it won't work; your operating system will say that it doesn't know how to launch the file. If you have that issue, download the file, go to the Downloads folder in your Finder, right-click on the file, and click Open, and then press Confirm.

    Some Mac users see a security error such as, "cs106x-life-demo.jar can't be opened because it is from an unidentified developer." To fix this, go to System Preferences → Security & Privacy. You will see a section about downloaded apps. You should see a prompt asking if you would like to allow access to our solution JAR. Follow the steps and then you should be able to open the demo.

    If all else fails, you could run the demo JAR from a terminal. Every operating system allows you to open a "terminal" or "console" window for typing raw system commands. Open your operating system's terminal or console window (Google if you need to learn how to do this), and then type:

    cd DIRECTORY_WHERE_DEMO_JAR_IS_STORED
    java -jar JAR_FILE_NAME
    

    For example, on a Mac machine, if your user name is jsmith12 and you have saved a demo JAR named hw1.jar in your Documents/106a directory, you would type:

    cd /users/jsmith12/Documents/106a
    java -jar hw1.jar
    

    Or on a Windows machine, if your user name is jsmith12 and you have saved a demo JAR named hw1.jar in your Documents/106a directory, you would type:

    cd C:\users\jsmith12\Documents\106a
    java -jar hw1.jar
    
    [an error occurred while processing this directive]

    "How do I run the assignment solution demos?"

    Our assignments offer a solution 'demo' that you can run to see the program's expected behavior. On many machines, all you have to do is download the .jar file, then double-click it to run it. But on some Macs, it won't work; your operating system will say that it doesn't know how to launch the file. If you have that issue, download the file, go to the Downloads folder in your Finder, right-click on the file, and click Open, and then press Confirm.

    Some Mac users see a security error such as, "cs106x-life-demo.jar can't be opened because it is from an unidentified developer." To fix this, go to System Preferences → Security & Privacy. You will see a section about downloaded apps. You should see a prompt asking if you would like to allow access to our solution JAR. Follow the steps and then you should be able to open the demo.

    If all else fails, you could run the demo JAR from a terminal. Every operating system allows you to open a "terminal" or "console" window for typing raw system commands. Open your operating system's terminal or console window (Google if you need to learn how to do this), and then type:

    cd DIRECTORY_WHERE_DEMO_JAR_IS_STORED
    java -jar JAR_FILE_NAME
    

    For example, on a Mac machine, if your user name is jsmith12 and you have saved a demo JAR named hw1.jar in your Documents/106a directory, you would type:

    cd /users/jsmith12/Documents/106a
    java -jar hw1.jar
    

    Or on a Windows machine, if your user name is jsmith12 and you have saved a demo JAR named hw1.jar in your Documents/106a directory, you would type:

    cd C:\users\jsmith12\Documents\106a
    java -jar hw1.jar
    

Definitions

Word Definition
encipher / encrypt To change a readable message into a non-readable message via a key
decipher / decrypt To change an encrypted message into a readable message via a key
key A set of characters used to perform the encryption and decryption algorithm, e.g, "abc123"
brute force Checking a set of keys via either a list of keys or a permutation of all possible keys
plaintext The readable, unencrypted message, e.g., "meet me at seven pm in gates"
ciphertext The unreadable, encrypted message, e.g., "m_en_me_eevis_et_gean_t_pa_smt"

Transposition Cipher Description

One of the most important tools in modern computing is the ability to encrypt data. There are numerous different types of ciphers, including the Caesar cipher (as we discussed in class), the "one-time-pad" (a perfect cipher, but not without issues), public-key ciphers (used in part for virtually all secure web transactions), etc.

In this assignment, you will create a program that encrypts and decrypts text using a "transposition" cipher. In a future assignment, you will write a program that attempts to break your own transposition cipher with brute force and without knowing the key.

A transposition cipher is a method for encrypting a message by transposing the characters in the message by putting the message into a rectangular grid and reading down the columns in an order based on a textual key.

For example, the message "MEET ME AT SEVEN PM IN GATES" (called the "plaintext") with the key "COMPSCI" would be put into a grid like this (with underscores representing spaces):

C O M P S C I
-------------
M E E T _ M E
_ A T _ S E V
E N _ P M _ I
N _ G A T E S

The letters would then be read out in columns, based on the alphabetic ordering of the letters in the key:

C O M P S C I
0 4 3 5 6 1 2

Note: Notice that the alphabetic ordering for duplicate letters puts the left-most letter first. I.e., if a word has duplicate letters, the left-most duplicate letter comes first in the column order. The word MISSISSIPPI would have the following order: {1, 4, 7, 10, 0, 8, 9, 2, 3, 5, 6}, because the first I (column 1) is the first in the alphabet, then the second I (column 4) is next, etc.

The first column that would be read would be M_EN, then ME_E, then EVIS, etc., for a final encryption of:

M_EN ME_E EVIS ET_G EAN_ T_PA _SMT

Without spaces it would look like this:

M_ENME_EEVISET_GEAN_T_PA_SMT

This final output is called the ciphertext, and that is what you can text to your friends without worrying about other people knowing what you are talking about.

To decrypt an encrypted message, you need to know the key, and you can reverse the process to get back the original message. First, put the message into columns based on the length of the key. In the example, the ciphertext has twenty-eight letters, and the key has seven letters, so we know that we will have columns of length four.

C C I M O P S
0 1 2 3 4 5 6
-------------
M M E E E T _
_ E V T A _ S
E _ I _ N P M
N E S G _ A T

Going back to the alphabetic ordering of the key, and putting the current columns under them, this tells us how to re-order the current columns:

C O M P S C I 
0 4 3 5 6 1 2 (key alphabetic order)
0 1 2 3 4 5 6 (ciphertext column order)

The translation from the ciphertext grid columns to the actual output columns is as follows:

ciphertext column      plaintext column
0                  ->        0
4                  ->        1
3                  ->        2
5                  ->        3
6                  ->        4
1                  ->        5
2                  ->        6
In other words, we rearrange the columns like this:

0 4 3 5 6 1 2 
-------------
M E E T _ M E
_ A T _ S E V
E N _ P M _ I
N _ G A T E S

Finally, the original plaintext is read back by reading across the grid by rows, to produce:

MEET_ME_AT_SEVEN_IN_GATES

Implementation details

Your program needs to be able to encrypt and decrypt text using a transposition cipher. First, your program should ask the user if he or she wants to encrypt or decrypt a message. Then, it should ask the user for the text to encrypt or decrypt. Finally, it should ask for the encryption/decryption key. Your program should read a single line of text, then use the key to encrypt or decrypt the text, and then print out the results.

You may use any of the containers we have discussed already to hold the data for the string manipulation. One suggestion would be to hold the entire plaintext in a single string, and then write a function to convert it into columns, each of which is also a string.

Because the plaintext length must evenly divide into the length of the key, if the actual text does not divide the key evenly, you should "pad" your plaintext with a character to make it divide evenly. We will choose a character with the tilde ('~') character (it is defined as a constant, PAD, in the starter code), because it is easy to see and will help debugging. To pad your strings, simply add the padding character to the end of your string until the key evenly divides the length of the string. The pad characters will be present in your encrypted text, and you will remove the padded characters when you decrypt the string.

Notes:

Logs

Log example 1:

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 1
Please enter the text to encrypt: meet me at seven pm in gates
Please type in a key: compsci
Encrypted text:

"m enme eeviset gean t pa smt"

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 2
Please enter the text to decrypt: m enme eeviset gean t pa smt
Please type in a key: compsci
Decrypted text:

"meet me at seven pm in gates"

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 0
Goodbye!

Log example 2 (notice the space at the beginning of the ciphertext!):

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 1
Please enter the text to encrypt: Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun.
Please type in a key: Douglas Adams
Encrypted text:

" rrademxl ~itss r yly~Featahihsewuhtnesalme. doif flul~ne hono  e~ nw ewlG ruocau e asdnt fo s inl~a chbere g taefntraad~hb ntptero~rukel a aas"

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 2
Please enter the text to decrypt:  rrademxl ~itss r yly~Featahihsewuhtnesalme. doif flul~ne hono  e~ nw ewlG ruocau e asdnt fo s inl~a chbere g taefntraad~hb ntptero~rukel a aas
Please type in a key: Douglas Adams
Decrypted text:

"Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun."

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 0
Goodbye!

Log example 3 (bad input from user):

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 1
Please enter the text to encrypt: I have a dream!
Please type in a key: king
Encrypted text:

"aae~ edmIv ah r!"

Welcome to the Transposition Cipher Machine!
Please choose:
1) encrypt text
2) decrypt text
Please type your choice, 0 to end: 2
Please enter the text to decrypt: aae~ edmIv ah r!
Please type in a key: mlk

*** *** STANFORD C++ LIBRARY *** A string exception occurred during program execution: *** Ciphertext length is not divisible by key length! *** *** Stack trace (line numbers are approximate): *** 0x1097a41ce decrypt(string, string) *** 0x1097a2dc1 main() ***

Possible Extra Features:

Though your solution to this assignment must match all of the specifications mentioned previously, it is allowed and encouraged for you to add extra features to your program if you'd like to go beyond the basic assignment. Note that our motivation for allowing extra features is to encourage your creativity, not inflate your grade; so if any points are given for your extra features, they will be minimal. The purpose is to explore the assignment domain further, not to disrupt the class grading curve. Make sure to see the notes below about how to separate your extra code and how to turn it in properly.

Here are some example ideas for extra features that you could add to your program:

Indicating that you have done extra features: If you complete any extra features, then in the comment heading on the top of your program, please list all extra features that you worked on and where in the code they can be found (what functions, lines, etc. so that the grader can look at their code easily).

Submitting a program with extra features: Since we use automated testing for part of our grading process, it is important that you submit a program that conforms to the preceding spec, even if you want to do extra features. If your feature(s) cause your program to change the output that it produces in such a way that it no longer matches the expected sample output test cases provided, you should submit two versions of your program file: a first one with the standard file name without any extra features added (or with all necessary features disabled or commented out), and a second one whose file name has the suffix -extra.cpp with the extra features enabled. Please distinguish them in by explaining which is which in the comment header. Our turnin system saves every submission you make, so if you make multiple submissions we will be able to view all of them; your previously submitted files will not be lost or overwritten.