Daniel B.

asked • 12/06/20

C++ Project/Program Encrypt/Decrypt

Need this program coded in c++, attached are the instructions and the given hint by the prof. Use the hint to code the program that is required for this assignment instructions. Use Classes/Arrays/Functions/Etc to code this program.

Hint:

Your project takes a string entered from the keyboard and encrypts it using several different encryption

schemes, one of those is the substitution cipher where each letter in the alphabet is mapped to a

corresponding letter in a another character set. Of course, this type of problem is a well suited to an

array–based solution.

Consider that a string is just an array of characters, thus given the text “this is a test” it might look

something like this in memory.

t h i s i s a t e s t


Let’s declare the string to be of the name “clearText”, that means that “clearText” is the location of the

first element of the string in memory. That means we can apply our indexing to the string so that we can

identify each element of the string by its index, remembering that we start our index at 0.

0 1 2 3 4 5 6 7 8 9 10 11 12 13

t h i s i s a t e s t

If we wanted to retrieve the second “s” from the string, we could just do the following....

string clearText = "this is a test";

cout << clearText[6] << endl;

One other useful function from the string library is the ability to return the length of a string using the

following function that will return 14. Note that the index of the last letter is 13 because of zero

indexing. Here is the code to output the length.

cout << clearText.length() << endl;

Now that we can identify the elements of the string, we can consider the encryption problem. We need

to map the alphabet to a substitution of our choice so that we can encrypt the data.

We can create two new strings that represent the alphabet and the cipher key just by declaring strings

as follows..

string alpha = "abcdefghijklmnopqrstuvwxyz";

string subst = "qwertyuiopasdfghjklzxcvbnm";

First to perform the encryption we need to do is identify each character in the clearText string and find

its index in the alpha array, we can set up a loop to do that for us.

int alphaIndex;

for (int i = 0; i < 26; i++)

if (clearText[0] == alpha[i])

alphaIndex = i;

cout << alphaIndex << endl;

In our case this will assign the value 19 to the variable alphaIndex because the “t” in the clearText string

located at clearText[0] is the 19th index of the alpha string.

Now we can use that information to map the “t” to its corresponding character in the subst array and

output the encrypted value. Based on the substitution string we defined the result will be “z”.


cout << subst[alphaIndex] << endl;

In addition, I can do the reverse of this process to decrypt the text. Thus if I was given “z” as the

encrypted letter I could find its index in the subst array and then map it to the same location in the alpha

array to get the “t”.

To store the data I could create another array, let’s say encryptedData to save the characters as I decode

them. For this project the maximum length of the string will be 50 so I can create the array as follows...

array <char, 50> encryptedData{};

So as I encrypt the data I can save it by assigning each character to the array and then output it.

encryptedData[0] = subst[alphaIndex];

cout << encryptedData[0] << endl;


Now let’s take a look at the Caesar or as it’s sometimes called the shifting cipher. For this part I only

really need the alpha array because I will simply shift up and down the array to encrypt and decrypt the

data. Let take the example where the Caesar shift is 3, in that case an “a” would be a “d” etc....

So to encrypt the data we can do something similar to the substitution cipher, first we need to find the

index of the character in our clearText then we simply add the shift value to the index to get our

encrypted data.

int caesarIndex;

int shift = 3;

for (int i = 0; i < 26; i++)

if (clearText[0] == alpha[i])

alphaIndex = i;

cout << alphaIndex << endl;

caesarIndex = alphaIndex + shift;

cout << alpha[caesarIndex] << endl;

encryptedData[0] = alpha[caesarIndex];

cout << encryptedData[0] << endl;

This does create one small problem though, what if the shifted value goes beyond the 25th index value?

This is easily solved by simply using the modulus operator by taking the caesarIndex and moding it with

26 to get the correct value, so the character “z” becomes “c”.

caesarIndex = (alphaIndex + shift) % 26;

These examples demonstrate one way to approach the problem, of course, there are many solutions

and even though I gave you the basics there are still many details to address.



Project:

Program Specification:

Using the techniques presented during this semester create a complete C++ program to emulate an

Encryption/Decryption Machine. The machine will be capable of the following:

 Encrypt a string entered by the user

 Choose between two different encryption methods

 Decrypt a string entered by the user

 Choose between two different decryptions methods

 Decrypt without knowing the encryption method (provide all possible outputs)

The interface must be professional and fully intuitive to the user

The program will be menu driven.

The program will use a class to define and implement each of the methods as member functions and will

store the original string and the encrypted/decrypted strings as data members.

In addition to using a class you must also use all the major structures we used this semester including:

Selection statements (if, if-else, switch) the appropriate one(s) of course

Loops (while, for, do-while) the appropriate one(s) of course

Standard Libraries (don’t recreate the wheel)

Functions

Arrays


The two encryption/decryption methods are:

Substitution cipher

Caesar cipher

You will need to research each and determine how to implement them. Remember in the case of the

Caesar cipher there are 25 possible shifts, you must be able to choose or test for all 25 options.

1 Expert Answer

By:

Patrick B. answered • 12/06/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.