Вы находитесь на странице: 1из 10

COMP 122 Lab 7 Lab

Report and Source Code

Purchase here

http://www.devrytutorial.com/shop/comp-122-lab-
7-lab-report-source-code/

Product Description

COMP 122 Lab 7


Lab Report and
Source Code
The focus of this lab is on using strings. You will have an op
portunity to work with
both C style strings and the string data type. This lab also gi
ves you an opportunity to
use what you have learned previously, including using functi
ons, array processing,
repetition, and selection. You will also have an opportunity t
o work with file input and output.
You are to design and implement a program which d
oes 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 li
ne 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 lin
e of plain text.
Encryption and Decryption Approach
Our approach to encryption and decryption involves
two strings.
The first is an encryption / decryption string which w
e will allow to be up to 128 lower case alphabetical c
haracters
in length. The second string is a line of text from a fil
e that is to be encrypted or decrypted.
Our basic strategy for encrypting data is based on m
apping alphabetical characters to specific values,
then doing some simple mathematical operations to
create a new value. First of all, every character
in either the encryption string or the input string is
mapped to a number between 0 and 25 based on its
position in the alphabet.
=0
=1
= 25
The mapped value of a character is easily obtained b
y doing the following:
For lower case characters, subtract a from the char
acter.
For upper case characters, subtract A from the char
acter.
To calculate the modified value of the first character
of input we add its mapped value to the
mapped value from the first character of the encrypt
ion string. This modified value is then
adjusted using % 26 to make sure that the final modi
fied value is within the 0 25 range.
To create the final encrypted character value for the
first character, simply do the following:
For lower case characters, add a to the modified val
ue.
For upper case characters, add A to the modified va
lue.
This is done for each alphabetic character in the inpu
t string. Non-alphabetic characters simply maintain
their present value. If the input string is longer than t
he encryption string,
simply reuse mapped values from the encryption stri
ng. For instance, if the encryption string has
10 characters (index values 0 9), when processing
the 11th input character (index 10),
simply use the input character index % length of enc
ryption string (in this case 10 % 10 is 0)
to select the value from the encryption string to use
for mapping.
The decryption process is basically the same as the
encryption process.
The only difference is the value of the mapped chara
cter from the encryption string.
For lower case encryption, the mapped from encrypti
on string a
For upper case encryption, the mapped from encrypt
ion string A
For lower case decryption, the mapped (character f
rom encryption string a)
For upper case decryption, the mapped (character
from encryption string A)
Program Requirements
Your program must meet the following requirements:
1. You must ask the user if they want to perform an e
ncryption or decryption operation.
2. You must ask the user to enter the name of the fil
e they want to encrypt or decrypt.
3. You must get an encryption key from the user whi
ch can be up to 128 characters.
The key must be all lower case alphabetic characters
.
4. You must have a function which takes the encrypti
on key and creates an encryption
map from it. For each character in the encryption ke
y string, subtract the lower case letter a
and store the result in the corresponding encryption
map array.
5. You must have a function which takes the encrypti
on key and creates a decryption map from it.
For each character in the encryption key string, subtr
act the lower case letter a from it.
Then subtract that result from 26 and store the value
in the corresponding decryption map array.
6. You must have a function which will do the encryp
tion or decryption transformation. This function take
s
the following parameters:
A constant C string containing the line of text to be t
ransformed.
A constant C character array which contains the encr
yption or decryption map.
An integer which contains the length of the encryptio
n map.
A string reference (output) which will contain the enc
rypted or decrypted string upon completion.
The core of the encryption / decryption algorithm is
as follows:
For each character (the ith character) in the text inp
ut 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 wit
hin 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 in
stead of a
7. For decryption, the main program should create a
n ifstream for the file to be decrypted.
It should use the getline method of the ifstream to re
ad lines from the file, call the encryption /
decryption function with the line to be decrypted, an
d display the string which contains the
result of the encryption / decryption function call. Re
peat until the ifstream reaches
the end of the file, then close the ifstream.
8. For encryption, the main program should create a
n ifstream for the file to be encrypted.
It should also create an ofstream for the file where th
e encrypted result will be stored.
The file name for this file can be gotten from the use
r or can be the input file name with a special
extension added at the end. The getline method of t
he ifstream is used to read lines from the input file
. Then the encryption / decryption function is called t
o encrypt the line. Display the string containing
the result and write the string to the ofstream. Close
the ifstream and ofstreams when finished.
9. Make sure that your program allows the user to en
crypt / decrypt more than one file per session.
This means adding a loop which allows the entire pro
gram to repeat until the user has nothing more to do
.
Hints
1. Use C strings for the encryption string and the file
names. Use char arrays for the encryption
and decryption maps. You cannot treat these as C str
ings because the maps 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 a
nd decrypted strings.
The string type allows you to add characters to the e
nd of a string using thepush_back method,
and it allows you to dump the contents of the string
using the erase method.
3. For input streams, you can use the eof method to
determine when you have reached the end of a file.
4. Use a character array to read data from the files.
Set the maximum length for this buffer to be 256 ch
aracters.
Development Strategy
I would recommend that you build this project in two
phases. The first phase should concentrate
on getting the encryption and decryption map functi
ons and the encryption / decryption function
working. You can test this by using fixed C strings for
the input line and the encryption string. Call the map
functions, then encrypt the fixed input string, output
the result, then decrypt the encrypted string
nd output the result. When your final output is the sa
me as the original input, your
encryption / decryption functions are working. The s
econd phase adds the file operations.
Testing and Deliverables
When you think you have a working program, use No
tepad to create a file with plain text in it.
You should enter some different length lines containi
ng a variety of characters.
Your file should have at least 10 lines. You should try
using short and long encryption keys.
Using your program, encrypt the file, then decrypt th
e encrypted file.
Take a screen shot of your decrypted output and pas
te it into a Word document.
Also copy the contents of your original file and the e
ncrypted file into the Word document.
Clearly label the contents of the Word document. Th
en copy your source code into your document.
Make sure that you have used proper coding style an
d commenting conventions!

Вам также может понравиться