A company wants to transmit data over the telephone, but is concerned that its p
ID: 3752014 • Letter: A
Question
A company wants to transmit data over the telephone, but is concerned that its phones could be tapped. All of the data are transmitted as four-digit integers. The company has asked you to write a program that encrypts the data so that it can be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then, swap the first digit with the third, swap the second digit with the fourth and print the encrypted integer. Your main duty for this assignment is creating an Encrypt class which includes Encrypt.h and Encrypt.cpp. After finishing the task you can use CISP400V10A2.cpp to test the Encrypt class.
The following is the Encrypt class specification.
1. The Encrypt class has an integer private data member 8 element array named digits. The first four
elements (0 ~ 3) are to store the original 4 digits integer and the next four (4 ~ 7) are to store the
encrypted data.
2. Encrypt class has several public member functions
a. An Encrypt constructor takes an integer of any digits and stores the last four digits. It encrypts the last
four digits, stores the encrypted information, displays a call to the constructor information, and shows the
original information and encrypted information. If the inputted number is less than or equal to 0 the
integer is set to 9436.
b. A displayOriginalData function does not accept and return any data. It displays the first four elements
of the private data member.
. A storeData function takes an integer and does not return anything. It stores the last four digits of the
passed in integer to the first 4 elements of the private data member, encrypts the data and store them in
the last 4 elements of the private data member.
d. A displayEncryptedData function does not accept and return any data. It displays the last four elements
of the private data member.
CISP400V10A2.cpp
// CISP400V10A2.cpp
// Test program for class Encrypt.
#include "Encrypt.h" // include definition of class Encrypt
#include
#include
using namespace std;
int main()
{
Encrypt app1(0), app2(40), app3(4560), app4(6145698),app5(-6); // create Encrypt objects
cout<
app1.storeData(100);// call app1's storeData function
app1.displayOriginalData();//display the app1's current original data.
app1.displayEncryptedData();// display the app1's current encrypted data.
cout << endl;//Jump to the next line
system("PAUSE");
return 0; // indicate successful termination
} // end main
Encrypt.h
#include
using namespace std;
class Encrypt{
private:
int digits[8];
public:
Encrypt(int data){
cout<<"** The default constructor is called "<
cout<<"and the passed in number is "<
cout<
if(data <= 0){
cout<<"XXX the input number is less than or equal to 0."<
cout<<"The number is reset to 9436. XXX"<
data = 9436;
}
for(int i=3; i>=0; i--){
digits[i] = data%10;
data = data/10;
}
for(int i=0; i<4; i++){
int digit = digits[i];
digits[i+4] = (digit + 7) % 10;
}
// Now swap 1st and 3rd and 2nd and fourthof encrypted digit
int temp = digits[4];
digits[4] = digits[6];
digits[6] = temp;
temp = digits[5];
digits[5] = digits[7];
digits[7] = temp;
cout<<"The original data is ";
for(int i=0; i<4; i++){
cout<
}
cout<<"."<
for(int i=4; i<8; i++){
cout<
}
cout<
}
void displayOriginalData(){
cout<<"The original data is ";
for(int i=0; i<4; i++){
cout<
}
cout<
}
void storeData(int data){
for(int i=3; i>=0; i--){
digits[i] = data%10;
data = data/10;
}
for(int i=0; i<4; i++){
int digit = digits[i];
digits[i+4] = (digit + 7) % 10;
}
// Now swap 1st and 3rd and 2nd and fourthof encrypted digit
int temp = digits[4];
digits[4] = digits[6];
digits[6] = temp;
temp = digits[5];
digits[5] = digits[7];
digits[7] = temp;
}
void displayEncryptedData (){
cout<<"The encrypted data is ";
for(int i=4; i<8; i++){
cout<
}
}
};
I have to leave the CISP400V10A2.cpp as is and then create an Encrypt.h which ive shown and still need an Encrypt.cpp which calls the values from CISP400V10A2.cpp and it should display a result like this.
Explanation / Answer
//encryprt.h
class Encrypt{
private:
int digits[8];
public:
Encrypt(int);
Encrypt(int * ,int);
void displayOriginalData();
void storeData(int);
void displayEncryptedData();
int getEncryptedData();
void swap(int & ,int &);
};
//encrypt.cpp
#include "encrypt.h"
#include<iostream>
using namespace std;
Encrypt::Encrypt(int n){
cout<<"** The default constructor is called "<<endl;
cout<<"and the passed in number is "<<n<<" ";
if(n<=0){
cout<<"XXX the input number is less than or equal to 0."<<endl;
cout<<"The number is reset to 9436. XXX"<<" ";
digits[0]=9;
digits[1]=4;
digits[2]=3;
digits[3]=6;
}
else{
digits[3]=n%10;
n/=10;
digits[2]=n%10;
n/=10;
digits[1]=n%10;
n/=10;
digits[0]=n%10;
}
for(int i=0;i<4;i++)
digits[i+4]=(digits[i]+7)%10;
swap(digits[4],digits[6]);
swap(digits[5],digits[7]);
}
Encrypt::Encrypt(int * arr ,int n){
if(n<4 ||(n>=4&&(arr[0]*arr[1]*arr[2]*arr[3])<0)||(n>=4&&arr[0]==0&&arr[1]==0&&arr[2]==0&&arr[3]==0)){
digits[0]=9;
digits[1]=4;
digits[2]=3;
digits[3]=6;
}
else{
for(int i=0 ;i<4 ;i++)
digits[i]=arr[i];}
for(int i=0;i<4;i++)
digits[i+4]=(digits[i]+7)%10;
swap(digits[4],digits[6]);
swap(digits[5],digits[7]);
}
void Encrypt::displayOriginalData(){
for(int i=0;i<4;i++)
cout<<digits[i];
cout<<" ";
}
void Encrypt::storeData(int n){
cout<<"** The default constructor is called "<<endl;
cout<<"and the passed in number is "<<n<<" ";
if(n<=0){
cout<<"XXX the input number is less than or equal to 0."<<endl;
cout<<"The number is reset to 9436. XXX"<<" ";
digits[0]=9;
digits[1]=4;
digits[2]=3;
digits[3]=6;
}
else{
digits[3]=n%10;
n/=10;
digits[2]=n%10;
n/=10;
digits[1]=n%10;
n/=10;
digits[0]=n%10;
}
for(int i=0;i<4;i++)
digits[i+4]=(digits[i]+7)%10;
swap(digits[4],digits[6]);
swap(digits[5],digits[7]);
}
void Encrypt::displayEncryptedData(){
for(int i=4;i<8;i++)
cout<<digits[i];
cout<<" ";
}
int Encrypt::getEncryptedData(){
int num=0;
for(int i=4;i<8;i++){
num*=10;
num+=digits[i];
}
return num;
}
void Encrypt::swap(int &a ,int &b){
int temp=a;
a=b;
b=temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.