Comp122 lab 7 lab report and source code

625 views

Published on

Comp122 lab 7 lab report and source code

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
625
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Comp122 lab 7 lab report and source code

  1. 1. COMP122 Lab 7 Lab Report and SourceCodeCLICK TO DOWNLOADhttp://homeworkfox.com/tutorials/computer-science/11810/comp122-lab-7-lab-report-and-source-code/COMP122Week 7 iLabThe focus of this lab is on using strings. You will have an opportunity to work withboth C style strings and the string data type. This lab also gives you an opportunityto use what you have learned previously, including using functions, array processing,repetition, and selection. You will also have an opportunity to work with file inputand output.You are to design and implement a program which does encryption and decryptionof data from files. Encryption is the process of taking plain lines of text andperforming some algorithmic transformation on the data to create an encrypted lineof text which looks nothing like the original. Decryption is the process of taking anencrypted line of text and performing some algorithmic transformation on the datato recover the original line of plain text.Encryption and Decryption ApproachOur approach to encryption and decryption involves two strings. The first is anencryption / decryption string which we will allow to be up to 128 lower casealphabetical characters in length. The second string is a line of text from a file that isto be encrypted or decrypted.Our basic strategy for encrypting data is based on mapping alphabetical charactersto specific values, then doing some simple mathematical operations to create a newvalue. First of all, every character in either the encryption string or the input string ismapped to a number between 0 and 25 based on its position in the alphabet.
  2. 2. =0 =1 = 25The mapped value of a character is easily obtained by doing the following:For lower case characters, subtract a from the character.For upper case characters, subtract A from the character.To calculate the modified value of the first character of input we add its mappedvalue to the mapped value from the first character of the encryption string. Thismodified value is then adjusted using % 26 to make sure that the final modifiedvalue is within the 0 - 25 range. To create the final encrypted character value for thefirst character, simply do the following:For lower case characters, add a to the modified value.For upper case characters, add A to the modified value.This is done for each alphabetic character in the input string. Non-alphabeticcharacters simply maintain their present value. If the input string is longer than theencryption string, simply reuse mapped values from the encryption string. Forinstance, if the encryption string has 10 characters (index values 0 - 9), whenprocessing the 11th input character (index 10), simply use the input character index% length of encryption string (in this case 10 % 10 is 0) to select the value from theencryption string to use for mapping.The decryption process is basically the same as the encryption process. The onlydifference is the value of the mapped character from the encryption string.For lower case encryption, the mapped from encryption string - aFor upper case encryption, the mapped from encryption string - AFor lower case decryption, the mapped - (character from encryption string - a)For upper case decryption, the mapped - (character from encryption string - A)Program RequirementsYour program must meet the following requirements:1. You must ask the user if they want to perform an encryption or decryptionoperation.2. You must ask the user to enter the name of the file they want to encrypt ordecrypt.3. You must get an encryption key from the user which can be up to 128 characters.The key must be all lower case alphabetic characters.
  3. 3. 4. You must have a function which takes the encryption key and creates anencryption map from it. For each character in the encryption key string, subtract thelower case letter a and store the result in the corresponding encryption map array.5. You must have a function which takes the encryption key and creates adecryption map from it. For each character in the encryption key string, subtract thelower case letter a from it. Then subtract that result from 26 and store the value inthe corresponding decryption map array.6. You must have a function which will do the encryption or decryptiontransformation. This function takes the following parameters: A constant C string containing the line of text to be transformed. A constant C character array which contains the encryption or decryption map. An integer which contains the length of the encryption map. A string reference (output) which will contain the encrypted or decrypted stringupon completion.The core of the encryption / decryption algorithm is as follows: For each character (the ith character) in the text input line do the following: if the character is not alphabetical, add it to the end of the output string if the character is lower case alphabetical subtract the character a from the character get the ith % map length element from the map and add it to the character adjust the value of the character % 26 to keep it within the alphabet add the character a to the character add the encrypted character value to the end of the output string if the character is upper case alphabetical do the same thing as for lower case except use A instead of a7. For decryption, the main program should create an ifstream for the file to bedecrypted. It should use the getline method of the ifstream to read lines from thefile, call the encryption / decryption function with the line to be decrypted, anddisplay the string which contains the result of the encryption / decryption functioncall. Repeat until the ifstream reaches the end of the file, then close the ifstream.8. For encryption, the main program should create an ifstream for the file to beencrypted. It should also create an ofstream for the file where the encrypted resultwill be stored. The file name for this file can be gotten from the user or can be theinput file name with a special extension added at the end. The getline method of theifstream is used to read lines from the input file. Then the encryption / decryptionfunction is called to encrypt the line. Display the string containing the result andwrite the string to the ofstream. Close the ifstream and ofstreams when finished.
  4. 4. 9. Make sure that your program allows the user to encrypt / decrypt more than onefile per session. This means adding a loop which allows the entire program to repeatuntil the user has nothing more to do.Hints 1. Use C strings for the encryption string and the file names. Use char arrays for theencryption and decryption maps. You cannot treat these as C strings because themaps can contain 0 as a valid data item rather than the end of string marker. 2. Use a string type variable to hold the encrypted and decrypted strings. The stringtype allows you to add characters to the end of a string usingthepush_back method, and it allows you to dump the contents of the string usingthe erase method. 3. For input streams, you can use the eof method to determine when you havereached the end of a file. 4. Use a character array to read data from the files. Set the maximum length forthis buffer to be 256 characters.Development StrategyI would recommend that you build this project in two phases. The first phase shouldconcentrate on getting the encryption and decryption map functions and theencryption / decryption function working. You can test this by using fixed C stringsfor the input line and the encryption string. Call the map functions, then encrypt thefixed input string, output the result, then decrypt the encrypted string and outputthe result. When your final output is the same as the original input, your encryption /decryption functions are working. The second phase adds the file operations.Testing and DeliverablesWhen you think you have a working program, use Notepad to create a file with plaintext in it. You should enter some different length lines containing a variety ofcharacters. Your file should have at least 10 lines. You should try using short andlong encryption keys. Using your program, encrypt the file, then decrypt theencrypted file. Take a screen shot of your decrypted output and paste it into a Worddocument. Also copy the contents of your original file and the encrypted file into theWord document. Clearly label the contents of the Word document. Then copy yoursource code into your document. Make sure that you have used proper coding styleand commenting conventions!

×