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

Hello, I\'m having a hard time doing this assignment. I need to create an Encryp

ID: 3564746 • Letter: H

Question

Hello, I'm having a hard time doing this assignment.

I need to create an Encryptor class that implements a simple encryption and decryption algorithm. The encryption algorithm you will be implementing is a key-based algorithm, meaning the encryption and decryption process requires a special string known as a key. I do know that I need an XOR operator.

Some specifications:

- A constructor for the Encryptor class that takes a string key and creates an Encryptor object. The key should be stored as a private member variable.

- An encrypt function that accepts a plaintext string as an argument and returns the ciphertext string. Note that the encrypt function should not need the key to be passed in as an argument because the key is a member variable.

- A decrypt function that accepts the ciphertext string as an argument and returns the plaintext string. The decrypt function should not need the key to be passed in as an argument because the key is stored as a member variable.

- Write test functions to make sure the Encryptor class correctly encrypts and decrypts messages when given the correct key. Make sure you test the following three cases: 1) the plaintext is the same length as the key, 2) the plaintext is shorter than the key and 3) the plaintext is longer than the key.

- Write a test function to make sure the Encryptor class cannot correctly decrypt a message if the key is not the correct one. You do not need to check all three cases above -- just pick one of the cases and show that you cannot recover the plaintext with an incorrect key.

Explanation / Answer

#include<iostream>
using namespace std;
using std::string;
class Encryptor {

private:
string key;

public:
Encryptor (string k){
key=k;
}
//Enryption of plaintext
string encrypt(string value)
{
string retval(value);
int k=0;

for(int v=0;v<value.length();v++)
{
retval[v]=value[v]^key[k];
k=(++k<key.length()?k:0);
}
return retval;
}

// Decryption of ciphertext
string decrypt(string str)
{
string dec(str);
int k=0;
for (unsigned int i=0; i < str.length(); i++)
{dec[i]= str[i] ^ key[k];
k=(++k<key.length()?k:0);
}
return dec;
}

//Test 1

//class end
};

void test1(string key,string plaintext){
Encryptor en(key);
if(key.length()<plaintext.length())
cout<<"The plaintext is longer than the key ";
else if(key.length()==plaintext.length())
cout<<"The plaintext is the same length as the key ";
else
cout<<"The plaintext is shorter than the key ";
//Encryption
string ciphertext= en.encrypt(plaintext);
cout<<" Ciphertext:"<<ciphertext<<endl;
//Decrption
string text=en.decrypt(ciphertext);
cout<<" Decrpted text:"<<text<<endl;
if(text==plaintext)
cout<<" Encrypts and decrypts messages are correct ";
else
cout<<"Encrypts and decrypts messages are not correct ";

}
void test2(string key,string plaintext){
Encryptor en(key);
Encryptor en2("newKey");
//Encryption
string ciphertext= en.encrypt(plaintext);
cout<<" Ciphertext:"<<ciphertext<<endl;

string text=en2.decrypt(ciphertext);
cout<<" Decrpted text:"<<text<<endl;
if(text==plaintext)
cout<<" Encrypts and decrypts messages are correct ";
else
cout<<"Encrypts and decrypts messages are not correct ";

}

int main(){
string key,plaintext,ciphertext,text;
//Test1
cout<<"Test1:"<<endl;
for(int i=0;i<3;i++){
cout<<"Input key: ";
cin>>key;
cout<<"Input plaintext to encrypt: ";
cin>>plaintext;
test1(key,plaintext);
}
//Test2
cout<<"Test2:"<<endl;
cout<<"Input key: ";
cin>>key;
cout<<"Input plaintext to encrypt: ";
cin>>plaintext;
test2(key,plaintext);

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