CSCI 131 - Techniques in Programming, Spring 2014

    Home | | Schedule | | Assignments | | Lectures | | Resources

    Project Three Specifications
    Encryption

    Due Date: Friday, 3/21, at the beginning of class.

    Introduction. This project focuses on loops (for, while, do...while) and on writing functions.

    For this programming assignment, you must write at least two separate functions, one to get a legal input file and open it, and another to write a summary to an external file named, report.txt. You might also write a function to do the ciphering, but that is optional.

    Background.

    Imagine yourself back in junior high and it is the middle of health class with you on one side of the room and your best friend on the other. You're not interested in the topic and you want to send a message to your friend about all sorts of news like, well, I don't know, it's been too long since I was in junior high school. You would pass a note to him/her but the problem is that you don't want all of the people sitting between you and your friend to read your thoughts! What to do? Use a cipher.

    A cipher is a pair of algorithms: one for converting a message into nonsensical blather and another for converting that nonsensical blather back into the original message. It is also known as encryption and decryption. For instance, I can use a cipher to change the message "i think computer science is loads of fun!" into "j uijol dpnqvufs tdjfodf jt mpbet pg gvo!" And if you received that nonsensical message, you could use your cipher to convert it back into the original message.

    Here's how it works. Before class you and your friend decide on a key, which will be some number between 1 and 25. Then, when you want to send a message, you "shift" each letter in the alphabet over by as many letters as the key. For example, I used a key of 1 in the message above. The letter 'a' shifts to the letter 'b', 'b' shifts to 'c', etc. This shifting wraps around so that with a key of 1, the letter 'z' becomes 'a'. This cipher leaves spaces and punctuation as is.

    Try it out with pencil and paper. When you encrypt "hey, pal!" using a key of 3, what do you get? The message "mjd, ufq!" was encrypted with a key of 5, what is it? What would you get if you encrypted with a key of 26? How about 0?

    Cipher.

    Your job is to write a program that inputs an input file name that contains a message to be encrypted and an integer that is used as the key to the cipher code.

    The program should read characters from the input file and write new characters which are simply the original characters encrypted using the Cipher key to a file named "encoded.txt". You should convert any upper case letters to lower case before you encrypt them. However, any punctuation, numerals, spaces or other white space that the message contains should be left as is and not encrypted. A program to decrypt your message (and thus help test your encryption) is available in ~csci131/PROJECTS/PROJ3. Spend some time with pencil and paper thinking this one through before you type anything.

    Hint: In C++ a character is simply a short integer, i.e. a number. For example,

        (int)'a' is  97 
        
        (int)'b' is  98
    
        (int)'z' is 122
    
        (char)122 is 'z' 
    
        (char) ((int)'a' + 5 ) is 'f'
    
        (char) ('a' + 5 ) is 'f'
    
        ('c' - 'a') is 2
    

    However, you cannot simply add the key to the current character. Consider this:

        (char) ( (int) 'y' + 5 ) is '~'
    

    With a key of 5, 'y' should become 'd', not '~'.

    The Details.

    A common task for a computer program is to read data in from a file, process that data in some way and print it out in an organized fashion. In this project, your program will open a data file and read in some message to be encoded. While your program is encoding characters it will also be keeping records so that in addition to writing the encrypted message to a file, it will also write some statistics about the message to a different file. It will count the number of characters encoded and how many other characters were encountered and then print them out in a report.

    To accomplish this, your program will prompt the user for a filename (over and over, if necessary, until a file of a given name is found) and then open the file for reading. Your program should then prompt the user for an integer encryption key. Your program will read the file, character by character, until it reaches the end-of-file, convert any letters encountered to lower case and encrypt them, and then write the encrypted letter to the encoded.txt file. (Non-letters should be written to the file as is.) Two counters should be kept in order to write to the report. One counter will count the number of letters encrypted, the other will count the number of non-letter characters. When the end of the file is reached, the program will print out a report to a file, named report.txt.

    Input, Output, and Files.

    Assume the file named "text.txt" contains:

    I think computer science is loads of fun!
    I CANNOT imagine maJORING in
    **********anything*********
    ELSE!!  testing 1234.....
    

    Then, assume the following interaction at the command line occurs:

    radius% ./proj3
    Please enter the data file name: text.txt
    You typed text.txt
    Please enter the key to use: 2
    So, the key is 2.
    
    Message is encoded in file encoded.txt.
    Please also check report.txt
    

    The file named "encoded.txt" will contain:

    k vjkpm eqorwvgt uekgpeg ku nqcfu qh hwp!
    k ecppqv kocikpg oclqtkpi kp
    **********cpavjkpi*********
    gnug!!  vguvkpi 1234.....
    

    The file "report.txt" will contain:

    
    In the text from the input file:
    
         The number of letters encoded was: 76
        The number of other characters was: 49
    
    

    To test your program you can run the program, decode, available in ~csci131/PROJECTS/PROJ3. The following is the interaction with the program decode.

    radius% ./decode
    Please enter the name of the file to decode: encoded.txt
    You entered encoded.txt
    Please enter the key that was used to encode: 2
    
    Message is decoded in file decoded.txt.
    

    The following would appear in the file "decoded.txt".

    i think computer science is loads of fun!
    i cannot imagine majoring in
    **********anything*********
    else!!  testing 1234.....
    

    The specifications:

    • As in previous projects, your program should be contained in a single file, called "proj3.cc". This file will contain main(), as well as the prototypes and definitions of several other functions.
    • In main() you will declare several variables including ones to keep track of the number of letters and nonletters read in as well as the key. You will need a character variable, ch, to read in characters from the file. Finally, you will need variables of type ifstream for inputting from a file, and of type ofstream for the output files.
    • The program should first call a function:

        void getInputFile(ifstream& myInputFile)

      that prompts the user for an input filename, and attempts to open a file with that name. In this function you will need to declare a variable that is a string for getting the input file name. (Recall, however that you need to use
        myInputFile.open(myFileName.c_str( ))
      when opening a file where the name is stored in a string variable named myFileName. If the file is opened successfully, the function should return control to the program, which should move to the next step. If not, the function should prompt the user for another filename and try to open the file again (and so on until an input file is successfully opened). The parameter should serve to return the input file stream to the main( ) function.
    • The program should prompt and read in a key. This key needs to be adjusted to a value between 1 and 25. That said, the keys, 26, 52, 78 etc. should produce the same encryption as the key 0. The keys, 27, 53, 79, etc. should produce the same encryption as the key 1. The keys, 28, 54, 80, etc. should produce the same encryption as the key 2 and so on.
    • Once the data file has been opened for input, the dominant feature of main() is a while loop. To initialize this while loop, you should get() a single character from the file. Then, while you have not read past the end of the file (while ( inFile)) or while(!(inFile.eof())) you will keep reading in characters. Within the loop, you will test to see if the character is an alphabetic letter and convert it to lower case if necessary.
      • If the character is a letter, count it as such, encrypt it and write it to the file, "encoded.txt".
      • Otherwise, count it as a non-letter and write it to the file, "encoded.txt".
    • The final statement in the body of the while loop will get() the next character in the file.
    • When control passes out of the while loop, just close the both the input file and the encoded file, and send the integer counters to the function:

        void writeReport(int letters, int other)

      and then return 0. The function, writeReport (), should define an output file stream variable, open an output file called "report.txt", and output the final report to that output file.

    Program testing: The directory ~csci131/PROJECTS/PROJ3 contains, among other things, files named decode, test3, proj3a.dat, proj3b.dat, proj3c.dat, containing user input. It also contains message3a.txt, message3b.txt and message3c.txt which are input files containing the messages to be encrypted. Copy these files into a directory which also contains your proj3.cc. You can test your program on each of the messages separately, by typing ./proj3 and then entering the appropriate filename (e.g. message3a.txt) when prompted. You can then use the decode program to check the encryption. If it worked correctly, the decoded message should be the same as the original, except all the letters will be lower case. To test against the model output, type ./test3. The test3 script will compile your program and run it three times, using the three data files for input. Each run will generate two files, called "encoded.txt" and "report.txt", which will be concatenated into a file named output3. The perfect output, well, the output produced by my own proj3.cc can be seen in ~csci131/PROJECTS/PROJ3/model3.out. The first part of model3.out will be the screen interaction whereas the second part of the file will just be the two files (encoded.txt and report.txt) from each of three runs concatenated together.

    To submit your finished project:

    1. Submit your program file electronically in the directory which contains your (presumably thoroughly tested) proj3.cc. (The submit program will not accept a program that does not compile).

    2. Hand in a hard copy of the file you submitted electronically. Hand this to your instructor in class on the project's due date.

    3. Print out the grading header, put your name at the top of it, and hand it in with your hard copy.

    Get started early and have fun!

    Honor code: Please review the collaboration policy on the main course webpage. Also refer to the math and CS department honor code policy.


    Home | | Schedule | | Assignments | | Lectures | | Resources


    Constance Royden--croyden@cs.holycross.edu
    Computer Science 131
    Last Modified: February 26, 2014
    Page Expires: September 8, 2014