27.07.2015 Views

COMP 122 Lab 7 Lab Report and Source Code (Devry) / comp122dotcom

For more course tutorials visit www.comp122.com COMP 122 Week 7 iLab The focus of this lab is on using strings. You will have an opportunity to work with both C style strings and the string data type. This lab also gives you an opportunity to use what you have learned previously, including using functions, array processing, repetition, and selection. You will also have an opportunity to work with file input and output. You are to design and implement a program which does encryption and decryption of data from files. Encryption is the process of taking plain lines of text and performing some algorithmic transformation on the data to create an encrypted line of text which looks nothing like the original. Decryption is the process of taking an encrypted line of text and performing some algorithmic transformation on the data to recover the original line of plain text.

For more course tutorials visit
www.comp122.com
COMP 122
Week 7 iLab
The focus of this lab is on using strings. You will have an opportunity to work with both C style strings and the string data type. This lab also gives you an opportunity to use what you have learned previously, including using functions, array processing, repetition, and selection. You will also have an opportunity to work with file input and output.
You are to design and implement a program which does encryption and decryption of data from files. Encryption is the process of taking plain lines of text and performing some algorithmic transformation on the data to create an encrypted line of text which looks nothing like the original. Decryption is the process of taking an encrypted line of text and performing some algorithmic transformation on the data to recover the original line of plain text.

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>COMP</strong> <strong>122</strong> <strong>Lab</strong> 7 <strong>Lab</strong> <strong>Report</strong> <strong>and</strong> <strong>Source</strong> <strong>Code</strong> (<strong>Devry</strong>)<br />

Click Here to Buy the Tutorial<br />

http://www.comp<strong>122</strong>.com/product-23-<strong>COMP</strong>-<strong>122</strong>-<strong>Lab</strong>-7-<strong>Lab</strong>-<strong>Report</strong>-<strong>and</strong>-<br />

<strong>Source</strong>-<strong>Code</strong><br />

<strong>COMP</strong> <strong>122</strong><br />

Week 7 i<strong>Lab</strong><br />

For more course tutorials visit<br />

www.comp<strong>122</strong><br />

comp<strong>122</strong>.com<br />

The focus of this lab is on using strings. You will have an opportunity to work with both C<br />

style strings <strong>and</strong> the string data type. This lab also gives you an opportunity to use what you<br />

have learned previously, including using functions, array processing, repetition, <strong>and</strong> selection.<br />

You will also have an opportunity to work with file input <strong>and</strong> output.<br />

You are to design <strong>and</strong> implement a program which does encryption <strong>and</strong> decryption of data<br />

from files. Encryption is the process of taking plain lines of text <strong>and</strong> performing some<br />

algorithmic transformation on the data to create an encrypted line of text which looks nothing<br />

like the original. Decryption is the process of taking an encrypted line of text <strong>and</strong> performing<br />

some algorithmic transformation on the data to recover the original line of plain text.<br />

Encryption <strong>and</strong> Decryption Approach<br />

Our approach to encryption <strong>and</strong> decryption involves two strings. The first is an encryption /<br />

decryption string which we will allow to be up to 128 lower case alphabetical characters in<br />

length. The second string is a line of text from a file that is to be encrypted or decrypted.<br />

Our basic strategy for encrypting data is based on mapping alphabetical characters to specific<br />

values, then doing some simple mathematical operations to create a new value. First of all,<br />

every character in either the encryption string or the input string is mapped to a number<br />

between 0 <strong>and</strong> 25 based on its position in the alphabet.<br />

= 0<br />

= 1<br />

= 25<br />

The mapped value of a character is easily obtained by doing the following:<br />

For lower case characters, subtract 'a' from the character.<br />

For upper case characters, subtract 'A' from the character.<br />

To calculate the modified value of the first character of input we add its mapped value to the<br />

mapped value from the first character of the encryption string. This modified value is then


adjusted using % 26 to make sure that the final modified value is within the 0 - 25 range. To<br />

create the final encrypted character value for the first character, simply do the following:<br />

For lower case characters, add 'a' to the modified value.<br />

For upper case characters, add 'A' to the modified value.<br />

This is done for each alphabetic character in the input string. Non-alphabetic characters<br />

simply maintain their present value. If the input string is longer than the encryption string,<br />

simply reuse mapped values from the encryption string. For instance, if the encryption string<br />

has 10 characters (index values 0 - 9), when processing the 11th input character (index 10),<br />

simply use the input character index % length of encryption string (in this case 10 % 10 is 0)<br />

to select the value from the encryption string to use for mapping.<br />

The decryption process is basically the same as the encryption process. The only difference is<br />

the value of the mapped character from the encryption string.<br />

For lower case encryption, the mapped from encryption string - 'a'<br />

For upper case encryption, the mapped from encryption string - 'A'<br />

For lower case decryption, the mapped - (character from encryption string - 'a')<br />

For upper case decryption, the mapped - (character from encryption string - 'A')<br />

Program Requirements<br />

Your program must meet the following requirements:<br />

1. You must ask the user if they want to perform an encryption or decryption operation.<br />

2. You must ask the user to enter the name of the file they want to encrypt or decrypt.<br />

3. You must get an encryption key from the user which can be up to 128 characters. The key<br />

must be all lower case alphabetic characters.<br />

4. You must have a function which takes the encryption key <strong>and</strong> creates an encryption map<br />

from it. For each character in the encryption key string, subtract the lower case letter 'a' <strong>and</strong><br />

store the result in the corresponding encryption map array.<br />

5. You must have a function which takes the encryption key <strong>and</strong> creates a decryption map<br />

from it. For each character in the encryption key string, subtract the lower case letter 'a' from<br />

it. Then subtract that result from 26 <strong>and</strong> store the value in the corresponding decryption map<br />

array.<br />

6. You must have a function which will do the encryption or decryption transformation. This<br />

function takes the following parameters:<br />

A constant C string containing the line of text to be transformed.<br />

A constant C character array which contains the encryption or decryption map.<br />

An integer which contains the length of the encryption map.<br />

A string reference (output) which will contain the encrypted or decrypted string upon<br />

completion.<br />

The core of the encryption / decryption algorithm is as follows:


For each character (the ith character) in the text input line do the following:<br />

if the character is not alphabetical, add it to the end of the output string<br />

if the character is lower case alphabetical<br />

subtract the character 'a' from the character<br />

get the ith % map length element from the map <strong>and</strong> add it to the character<br />

adjust the value of the character % 26 to keep it within the alphabet<br />

add the character 'a' to the character<br />

add the encrypted character value to the end of the output string<br />

if the character is upper case alphabetical<br />

do the same thing as for lower case except use 'A' instead of 'a'<br />

7. For decryption, the main program should create an ifstream for the file to be decrypted. It<br />

should use the getline method of the ifstream to read lines from the file, call the encryption /<br />

decryption function with the line to be decrypted, <strong>and</strong> display the string which contains the<br />

result of the encryption / decryption function call. Repeat until the ifstream reaches the end of<br />

the file, then close the ifstream.<br />

8. For encryption, the main program should create an ifstream for the file to be encrypted. It<br />

should also create an ofstream for the file where the encrypted result will be stored. The file<br />

name for this file can be gotten from the user or can be the input file name with a special<br />

extension added at the end. The getline method of the ifstream is used to read lines from the<br />

input file. Then the encryption / decryption function is called to encrypt the line. Display the<br />

string containing the result <strong>and</strong> write the string to the ofstream. Close the ifstream <strong>and</strong><br />

ofstreams when finished.<br />

9. Make sure that your program allows the user to encrypt / decrypt more than one file per<br />

session. This means adding a loop which allows the entire program to repeat until the user<br />

has nothing more to do.<br />

Hints<br />

1. Use C strings for the encryption string <strong>and</strong> the file names. Use char arrays for the<br />

encryption <strong>and</strong> decryption maps. You cannot treat these as C strings because the maps can<br />

contain 0 as a valid data item rather than the end of string marker.<br />

2. Use a string type variable to hold the encrypted <strong>and</strong> decrypted strings. The string type<br />

allows you to add characters to the end of a string using thepush_back method, <strong>and</strong> it allows<br />

you to dump the contents of the string using the erase method.<br />

3. For input streams, you can use the eof method to determine when you have reached the end<br />

of a file.<br />

4. Use a character array to read data from the files. Set the maximum length for this buffer to<br />

be 256 characters.<br />

Development Strategy


I would recommend that you build this project in two phases. The first phase should<br />

concentrate on getting the encryption <strong>and</strong> decryption map functions <strong>and</strong> the encryption /<br />

decryption function working. You can test this by using fixed C strings for the input line <strong>and</strong><br />

the encryption string. Call the map functions, then encrypt the fixed input string, output the<br />

result, then decrypt the encrypted string <strong>and</strong> output the result. When your final output is the<br />

same as the original input, your encryption / decryption functions are working. The second<br />

phase adds the file operations.<br />

Testing <strong>and</strong> Deliverables<br />

When you think you have a working program, use Notepad to create a file with plain text in it.<br />

You should enter some different length lines containing a variety of characters. Your file<br />

should have at least 10 lines. You should try using short <strong>and</strong> long encryption keys. Using<br />

your program, encrypt the file, then decrypt the encrypted file. Take a screen shot of your<br />

decrypted output <strong>and</strong> paste it into a Word document. Also copy the contents of your original<br />

file <strong>and</strong> the encrypted file into the Word document. Clearly label the contents of the Word<br />

document. Then copy your source code into your document. Make sure that you have used<br />

proper coding style <strong>and</strong> commenting conventions!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!