Calculate the modified value of the first character of input

Assignment Help C/C++ Programming
Reference no: EM13755410

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.

Encryption and Decryption Approach

Our approach to encryption and decryption involves two strings. The first is an encryption / decryption string which we will allow to be up to 128 lower case alphabetical characters in length. The second string is a line of text from a file that is to be encrypted or decrypted.

Our basic strategy for encrypting data is based on mapping 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.

A = a = 0
B = b = 1
Z = z = 25

The 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 mapped value to the 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 create the final encrypted character value for the first 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-alphabetic characters simply maintain their present value. If the input string is longer than the encryption string, simply reuse mapped values from the encryption string. 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 encryption 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 character from the encryption string.

For lower case encryption, the mapped value = character from encryption string - 'a'

For upper case encryption, the mapped value = character from encryption string - 'A'

For lower case decryption, the mapped value = 26 - (character from encryption string - 'a')

For upper case decryption, the mapped value = 26 - (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 encryption or decryption operation.

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

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.

4. You must have a function which takes the encryption key and creates an encryption map from it. For each character in the encryption key 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 encryption key and creates a decryption map from it. For each character in the encryption key string, subtract 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 encryption or decryption transformation. 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 string upon 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 'a'

7. For decryption, the main program should create an ifstream for the file to be decrypted. It should use the getline method of the ifstream to read lines from the file, call the encryption / decryption function with the line to be decrypted, and display the string which contains the result of the encryption / decryption function call. 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 be encrypted. It should also create an ofstream for the file where the encrypted result will be stored. The file name for this file can be gotten from the user or can be the input file name with a special extension added at the end. The getline method of the ifstream is used to read lines from the input file. Then the encryption / decryption function is called to 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 encrypt / decrypt more than one file per session. This means adding a loop which allows the entire program 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 strings 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 and decrypted strings. The string type allows you to add characters to the end of a string using the push_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 characters.

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 functions 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 and output the 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 Deliverables

When you think you have a working program, use Notepad to create a file with plain text in it. You should enter some different length lines containing 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 the encrypted file. Take a screen shot of your decrypted output and paste it into a Word document. Also copy the contents of your original file and the encrypted file into the Word document. Clearly label the contents of the Word document. Then copy your source code into your document. Make sure that you have used proper coding style and commenting conventions!

Reference no: EM13755410

Questions Cloud

Describe how big data and distributed data : Define ‘Big Data'. Describe how ‘Big Data' and distributed data fit in the modern business environment. The Internet is considered a ‘game changing' technology. Discuss at least three reasons why this is true
Explain key attributes of larry frymires leadership style : Identify and explain four key attributes of Larry Frymire's leadership style. Identify and explain four major influences from the tug of war between Frymire's role as leader and the newly elected governor.
Several convicted cases of cyberstalking : There are several convicted cases of cyberstalking and Internet fraud/abuse. Misha Glenny, a United Kingdom-based journalist, recently wrote a book on the subject matter appropriately titled Dark Markets
The required rate of return on the market : The risk-free rate of return is 8 percent, the required rate of return on the market, E[Rm] is 12 percent, and Stock X has a beta coefficient of 1.4. If the dividend expected during the coming year, D1, is $2.50 and g = 5%, at what price should Stock..
Calculate the modified value of the first character of input : 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 encryption string.
Show the effect of dollar appreciation and depreciation : Is China still using central bank foreign exchange policy to maintain the value of the yuan? What is the current policy of the United States on this issue - If the U.S. economy is operating near full employment and the exchange rate increases, exp..
Create a portfolio that is short one call : Assume that you create a portfolio that is short one call and long one put. The call has a delta of .2735, gamma of .0279, theta of -8.9173, vega of 9.9144, and rho of 3.5985. the put has a delta of -.7265, gamma of .0279, theta of -4.779, vega of 9...
How did second great awakening affect the reform movements : How did the Second Great Awakening affect the reform movements of the mid-1800s?
What was the result of the franco-prussian war : What was the result of the franco-prussian war?

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Write a matrix multiplication program

Write a C++, matrix multiplication program which you can instrument to monitor and measure execution time as a function of problem size. Make sure it works in Microsoft Visual Studio.

  Sequence of instructions that transfers data from memory

Write a sequence of instructions that transfers data from memory to an external I/O device by using channel 3 of the 8237 DMA controller. Transfer from 20000H-20FFFH.

  Draw a two-dimensional house seen from the front

Draw a two-dimensional house seen from the front, the way a child would: with a dor, two windows, and a roof with a chimney

  Write out the sorted array

Write complete specifications for this problem and design a modular solution. What classes and methods did you identify during the design of your solution?

  Prepare a program for sorting the numbers

Write a program that orders three double numbers by increasing value. The program should include a function named sort3 that takes three double * arguments (pointer to double). The function prototype is void sort3(double *x, double *y, double *z);

  Develop a time delay procedure

Develop a time delay procedure for the 2.0 GHz Pentium 4 that waits for 80s.

  Computer programming techniques

Construct a program from a design and use appropriate functions

  Guessing game

In C++ develop a "guessing game" where the computer and the user alternatively guess the value of a randomly selected secret number between 1 and 99 (or any other maximum value). Initially, the computer selects the random number, and the user make..

  Program of a class date which displays the date

Write a C++ program of a Class Date which displays the date. Use the subsequent requirements:

  How the function delete can be implemented for arrays

Explain in details how the function Delete() can be implemented for arrays to function exactly as it would with lists. Use pseudo code to clarify your explanation.

  Build a basic cipher base class

Build a randomly generated simple substitution cipher and use it to encode messages. Again the encoded messages will be in all upper case letters only.

  Write a value-returning function

Write a program that uses the function isNumPalindrome given(Palindrome Number). Test your program on the following numbers: 10, 34, 22, 333, 678, 67876, 44444, and 123454321.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd