MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:
MUST WRITE IN C++
Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms.
Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A private member variable of the string type to hold the encrypted message. A constructor that receives the file name as a parameter and reads the text into the object. A pure virtual function decode that will be implemented in the derived classes. A function that prints the message on the screen.
Define two derived classes: CypherA and CypherB. Each one should implement its own version of decode according to the following algorithms:
CypherA should use the following key to decode the message: input character: abcdefghijklmnopqrstuvwxyz decoded character: iztohndbeqrkglmacsvwfuypjx Each 'a' in the input text should be replaced with an 'i', each 'b' with a 'z' and so forth.
CypherB should implement the "rotational cypher" algorithm. In this encryption method, a key is added to each letter of the original text. In order to decode you need to subtract 4. For example:
Cleartext: A P P L E Key: 4 4 4 4 4 Ciphertext: E T T P I
Main Program here
#include
#include
#include
#include "CypherA.h"
#include "CypherB.h"
using namespace std;
int main(int argc, const char * argv[]) {
string filename;
cout << "Enter file name to be decoded with algorithm A:
";
cin >> filename;
class CypherA messageA (filename);
messageA.decode();
cout << "Decoded message: \n";
messageA.print();
cout << endl << endl;
cout << "Enter file name to be decoded with algorithm B:
";
cin >> filename;
class CypherB messageB (filename);
messageB.decode();
cout << "Decoded message: \n";
messageB.print();
cout << endl;
return 0;
}
// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
#include "bits/stdc++.h"
using namespace std;
class BaseClass{
string cipher;
public:
// constructor
BaseClass(string filename){
// open file
ifstream f(filename);
// now read and store in
cipher
f>>cipher;
f.close();
}
string getCipher(){
return cipher;
}
// virtual function
virtual void decode() = 0;
};
// CypherA
class CypherA: public BaseClass{
public:
string message;
// constructor
CypherA(string filename):BaseClass(filename){
message ="";
}
void decode(){
string key =
"iztohndbeqrkglmacsvwfuypjx";
// now decode by substracting
4
message ="";
string c = getCipher();
for(int
i=0;i<c.length();i++){
message +=
key[(c[i]-'a')];
}
}
void print(){
cout<<message;
}
};
// CypherB
class CypherB: public BaseClass{
public:
string message;
// constructor
CypherB(string filename):BaseClass(filename){
message = "";
}
void decode(){
// now decode by substracting
4
message ="";
string c = getCipher();
for(int
i=0;i<c.length();i++){
message +=
(c[i]-4);
}
}
void print(){
cout<<message;
}
};
int main(int argc, const char * argv[]){
string filename;
cout << "Enter file name to be decoded with
algorithm A: ";
cin >> filename;
CypherA messageA (filename);
messageA.decode();
cout << "Decoded message: \n";
messageA.print();
cout << endl << endl;
cout << "Enter file name to be decoded with
algorithm B: ";
cin >> filename;
CypherB messageB (filename);
messageB.decode();
cout << "Decoded message: \n";
messageB.print();
cout << endl;
return 0;
}
// SAMPLE OUTPUT
Get Answers For Free
Most questions answered within 1 hours.