Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that encrypts a string of text with a maximum size of 132 ch

ID: 3703026 • Letter: W

Question

Write a C++ program that encrypts a string of text with a maximum size of 132 characters by XOR’ing a hex 0AC to each character in the input buffer. The program will display the input buffer, the encrypt buffer and the decrypted buffer. Your program will display each char of the buffer as a hex character. You will use an InputBuffer of size 133, a EncryptedBuffer of size 133, and a ClearBuffer of size 133.

No global variables can be used. You will create your buffers in main().

You will use a function called InputData to input the data to the inputBuffer. You will pass the InputBuffer to this function where the input data will be input. You will make sure to display the contents of the InputBuffer.

You will also have a function called Encrypt where you will pass the input Buffer and the EncryptedBuffer. You will have a variable named key of type short that will have the value 0xAC Note: The xor operator is ^. You will then XOR each character in the InputBuffer and then copy that character to the EncryptedBuffer. Then the function will display the contents of the EncryptedBuffer. A header file is required and will be named global.h. Make sure to add in guarding into this file.

You will have another function called DeCrypt where you will pass the EncryptedBuffer and the ClearBuffer. You will XOR the each character in the EncrytedBuffer and copy the result into the ClearBuffer. This function will then display the contents of the ClearBuffer. The program will loop until the user is asked to run again with a ‘y’ or quit with a ‘q’

Explanation / Answer

#include<iostream>  

#include<string>

using namespace std;

void InputData(string &InputBuffer ){

cout<<"Enter the input buffer : ";

getline(cin,InputBuffer);

if(InputBuffer.length() > 132){

InputBuffer="";

InputData(InputBuffer);

}

cout<<" InputBuffer is : "<<InputBuffer<<endl;

}

void Encrypt(string InputBuffer, string &EncrpytedBuffer){

const int KEY = 172; //OxAC

int len = InputBuffer.length();

char *cString = (char*)(InputBuffer.c_str());

for (int i = 0; i < len; i++){

int temp = InputBuffer[i]; //implicit casting from char to int

cString[i] = (char)(temp ^ KEY);

}

EncrpytedBuffer = cString;

cout<<" EncryptedBuffer is : "<<EncrpytedBuffer<<endl;

}

void DeCrypt(string EncrpytedBuffer, string ClearBuffer){

const int KEY = 172; //OxAC

int len = EncrpytedBuffer.length();

char *cString = (char*)(EncrpytedBuffer.c_str());

for (int i = 0; i < len; i++){

int temp = EncrpytedBuffer[i]; //implicit casting from char to int

cString[i] = (char)(temp ^ KEY);

}   

ClearBuffer = cString;

cout<<" DecryptedBuffer is : "<<ClearBuffer<<endl;

}

int main(void){

char ch;

do

{

string InputBuffer;

string EncrpytedBuffer;

string ClearBuffer;

InputData(InputBuffer);

Encrypt(InputBuffer,EncrpytedBuffer);

DeCrypt(EncrpytedBuffer,ClearBuffer);

printf(" Enter Y/Q for continue or quit ");

cin>>ch;

}while( ch == 'Y' || ch == 'y');

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote