Danny B.

asked • 12/02/20

C++ Help Finish Code Has Error's Encrypt/Decrypt Using Substitution and Caesar Cipher

Here is my code so far please help me fix it.

#include<iostream>

#include<vector>

#include<set>

#include<string>


using namespace std;


class Substitution {

int key;

string input;

bool encipher;

public:

//constructor

Substitution(string msg, bool enc) :

input(msg), encipher(enc) {}


//the function for encryption/decryption (Substitution algorithm)

string substitution();

};

class Cipher

{

public:

virtual string encode(string) = 0;

virtual string decode(string) = 0;

};


///////////////////////////////////////////////////////////////////////


//the function for encryption/decryption (Substitution algorithm)

string Substitution::substitution()

{


do {

cout << "Enter key in 1..25>> ";

cin >> key;

cin.ignore();

//fflush(stdin);


if (key > 25 || key < 1)

cout << "KEY ERROR";

//check KEY: if encryption/decryption is possibly


} while (key > 25 || key < 1);



string output = "";



for (int i = 0; i < input.size(); ++i)

{

if (isalpha(input[i])) //if letter

{

//offset='A' for upper letter, offset='a' for lowel letter

char offset = isupper(input[i]) ? 'A' : 'a';


//find shift for current letter for encryption (key) or for decryption (26 - key)

int shift = encipher ? key : 26 - key;


//convert letter (shift letter)

char ch = (char)(((input[i] + shift - offset) % 26) + offset);

output += ch;

}

else

{

//non letter

output += input[i];


}

}


return output;

}



//the function for encryption/decryption (Substitution algorithm)

// Encryption

/**

* This class performs encryption and decryption as per caesar cipher scheme.

*/

/**

* Class definition that implements the encryption and decryption

* of plain texts/files using caesar cipher algorithm.

*/

class CaesarCipher {

public:

/**

* Class constructor that takes the value of shift

* as the parameter.

*/

CaesarCipher(int shift);


/**

* Encrypt a given plain text and return the encrypted

* cipher text.

*/

string encrypt(string plain);


/**

* Decrypt a given cipher text and return the decrypted

* plain text.

*/

string decrypt(string cipher);


/*

* Change the shift value for the encryption/decryption

*/

void setShift(const int& shift);


private:

int shift;

};

CaesarCipher::CaesarCipher(int shift)

{

this->shift = shift;

}


string CaesarCipher::encrypt(string plain)

{

string cipher = "";


for (int i = 0; i < plain.length(); i++)

{

if (isupper(plain[i]))

{

cipher += (char)((((int)plain[i] + shift - 65) % 26) + 65);

}

else if (islower(plain[i]))

{

cipher += (char)((((int)plain[i] + shift - 97) % 26) + 97);

}

else

{

cipher += plain[i];

}

}

return cipher;

}


string CaesarCipher::decrypt(string cipher)

{

string plain = "";

for (int i = 0; i < cipher.length(); i++)

{

if (isupper(cipher[i]))

{

plain += (char)((((int)cipher[i] + (26 - shift) - 65) % 26) + 65);

}

else if (islower(cipher[i]))

{

plain += (char)((((int)cipher[i] + (26 - shift) - 97) % 26) + 97);

}

else

{

plain += cipher[i];

}

}

return plain;

}


void CaesarCipher::setShift(const int& shift)

{

this->shift = shift;

}



int main() {

/*

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)

*/


string source = "";

int option;



do {


cout << "\nEnter the text>> " << flush;

getline(cin, source);

cin.ignore(256, '\n');




cout << "Make your choice" << endl;

cout << "\t1. Substitution cipher, encrypt" << endl;

cout << "\t2. Substitution cipher, decrypt" << endl;

cout << "\t3. Caesar cipher, encrypt" << endl;

cout << "\t4. Caesar cipher, decrypt" << endl;

cout << "\t5. Decrypt without knowing the encryption method (provide all possible outputs)" << endl;

cout << "\t6. Exit" << endl;

cout << ">> ";

cin >> option;

cin.ignore();



string result;


switch (option) {

case 1: {

Substitution s(source, true);

result = s.substitution();

cout << endl << "The encryption string:\n" << result << "\n";

break;

}

case 2: {

Substitution s(source, false);

result = s.substitution();

cout << endl << "The decryption string:\n" << result << "\n";

break;

}

case 3: {

CaesarCipher t(source, true);

result = c.CaesarCipher();

cout << endl << "The decryption string:\n" << result << "\n";

break;

}

case 4: {

CaesarCipher t(source, false);

result = d.CaesarCipher();


cout << endl << "The decryption string:\n" << result << "\n";

break;

}


case 5: {

CaesarCipher t(source, false);

result = d.CaesarCipher();

cout << endl << "The decryption (Caesar Cipher) string:\n" << result << "\n";


Substitution s(source, false);

result = s.substitution();

cout << endl << "The decryption (substitution) string:\n" << result << "\n";

break;

}


case 6:

cout << endl << "Goodbye!" << endl;

break;

}

} while (option != 6);


return 0;

}

1 Expert Answer

By:

Rize S. answered • 03/23/23

Tutor
New to Wyzant

Senior IT Certified Trainer, IT Developer & DBA Administrator

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.