Supplied file(s) $sup06/demo_encrypt_decrypt (the demo executable)
$sup06/encrypt_decrypt.txt (the TextItems file)
Files to submit encrypt_decrypt.cpp (your source code)
encrypt_decrypt (your executable)
my_tests.sh (your testing script)
Where to put them Copy them to your u##/submissions/s06 directory.
When they're due Sunday, March 10, 2025 @11:59pm

Overview

This week you will design and write a program to encrypt and decrypt textfiles. Such programs are nice exercises because they always involve making good choices for the structures and algorithms you need to use, and they require you (or should require you) to think through your approach before starting, in a way that not all exercises do.

Also, because you now have quite a number of STL facilities at your disposal, you have quite a variety of things to choose from when solving any problem with a C++ program, including this one. So, feel free to choose and use whatever works, from the STL and (of course) from the utilities package. In fact, think of this as another opportunity to get your "STL chops" up to speed.

Steps to Perform

  1. Copy the demo executable and its accompanying TextItems file.
  2. Study the program description provided on the second screen that appears when you run the program with no command-line parameters, and then run the program a few times to produce the program's output when any of the errors mentioned actually occur.
  3. Next, study the encryption/decryption (or encoding/decoding) scheme described below. Make up a few short (or long, if you like) textfiles and try encrypting and decrypting them. Enter different phrases for doing so. Do this until you understand how the program works. It would probably also be helpful to work through a very short example with pencil and paper.
  4. Write pseudocode for a solution to this problem, and revise it until you are convinced that a program that implements your pseudocode will be the one you want.
  5. Translate your pseudocode into C++ and continue testing your program until you are convinced it works correctly.
  6. Make sure your source code is identified, formatted, and documented properly, according to our current rules and guidelines.
  7. Finally, submit the required files by copying them to the appropriate subdirectory in your account.

Additional Notes, Requirements, Specifications and/or Hints (if any)

  1. The phrase used for encrypting/decrypting must be just a single line of text.
  2. To compare two files use the diff command, and you should have some diff commands for file comparison in your testing script. Also, don't forget to include in your testing some files that are encrypted by your program and decrypted by the sample executable, and vice versa. This is a kind of "acid test" that should give you reasonable confidence that your program is working properly if it passes that test.

Description of the Encoding/Decoding Scheme

The encryption scheme used is a straightforward "substitution cipher". That is, each line of the encoded message contains exactly the same number of characters as the corresponding line in the original plain text, and once you have decoded each character of the encoded message you immediately have the original plain text. This also implies that blank lines in the plain text are preserved in the encrypted text, which is in fact the case.

The original plain text of any message may contain any or all of the printable characters in the Standard ASCII Character Set. That is, the original textfile may contain any or all of those characters with numerical codes from 32 (the blank space) through to 126 (the tilde, or '~', character).

The phrase to be used for both encrypting and decrypting is obtained from the user, as the last of the inputs on the command line, after which this happens:

  1. First, all duplicate characters are removed from this phrase.
  2. Second, the remaining (unique) characters (from the phrase) are placed in their natural ASCII order, and then reversed. Or, I suppose, you could simply say that they are placed in the reverse order of their natural ASCII order.
  3. Third, the remaining (missing) characters from the complete set of printable ASCII characters are added to the end of this list of characters, in their natural ASCII order.
  4. The now-complete list of printable ASCII characters (in some order) is reversed one final time.

This process results in a list containing all printable ASCII characters in some order. Let's call the resulting list of characters "the key".

If you now imagine a line consisting of all the printable ASCII characters in their natural order placed underneath the above line (that is, underneath "the key"), then the encryption of each character in a message of plain text would be performed by finding the character to be encrypted in the bottom line and using whatever character lies immediately above it in "the key" as the corresponding encrypted character.

Similarly, when decoding any given character, you would find the encrypted character in the top line (that is, in "the key"), and then the character immediately below it would be the corresponding plain text character.