Question

In: Computer Science

C++. How do I reverse this encryption? Here is the question: "A company that wants to...

C++. How do I reverse this encryption? Here is the question:

"A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your program should read a four-digit integer in main() entered by the user and encrypt it as follows: 1. Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then display the encrypted integer. 2. After encryption, program will ask inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number and return the decrypted number."

I have everything done except the reversal of the encryption. I do not know how to reverse getting the remainder of the numbers by being divided by 10.

Here is my code:

#include

using namespace std;

int main()
{
   int array[5] = { '0' };
   int i = 1, temp = 0, temp2 = 0,x=1,j=1;

       // loop has user input 4 digits
       for (i = 1; i < 5; i++) {
           cout << "Enter a number: ";
           cin >> array[i];
       }

       //encrypting begins
       //each digit is added by 7 and divided by 10
       cout << endl;
       array[1]+7;
       cout << array[1] % 10;
       cout << endl;
       array[2] + 7;
       cout << array[2]%10;
       cout << endl;
       array[3] + 7;
       cout << array[3]%10;
       cout << endl;
       array[4] + 7;
       cout << array[4]%10;
       cout << endl;


       //array positions are swapped

       temp = array[1];
       array[1] = array[3];
       array[3] = temp;

       //test of the switch
       cout << "New first "<< array[1] << endl << "new third " << array[3];

       temp = array[2];
       array[2] = array[4];
       array[4] = temp;
       cout << endl;
       //print out encryption

       for (x = 1; x < 5; x++) {
           cout << array[x];

       }

}

Solutions

Expert Solution

Thanks for the question.


I have rewritten the whole program into functions to make it more readable. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.


Thank You !!


========================================================================================

#include <iostream>

using namespace std;

void getNumber(int array[4]){
   int num;
for(int i=0;i<4;i++){
    cout<<"Enter 0-9:";cin>>num;
    if(0<=num && num<=9) array[i]=num;
    else {
       cout<<"Invalid digit. Try again\n";
        i--;
   }
}
}

// encrypts a 4digit number logic here
void encrypt(int array[4]){
   // first encryption
   for(int i=0; i<4;i++){
       array[i] = (array[i]+7)%10;
   }
   // swap 1st and 3rd & 2nd and 4th
   int temp;
   temp=array[0];
   array[0]=array[2];
   array[2]=temp;
   temp=array[1];
   array[1]=array[3];
   array[3]=temp;
}


// decrypt the encrypted number logic here
void decrypt(int array[4]){
   // swap 1st and 3rd & 2nd and 4th
   int temp;
   temp=array[0];
   array[0]=array[2];
   array[2]=temp;
   temp=array[1];
   array[1]=array[3];
   array[3]=temp;
  
   for(int i=0;i<4;i++){
       array[i]=array[i]-7; // simply subtract the number
       if(array[i]<0)array[i]+=10; // if the number is negative add 10 to it
   }
}

int main()
{
int array[4] = { '0'};
getNumber(array);
encrypt(array);
cout<<"Encrypted number: "<<array[0]<<array[1]<<array[2]<<array[3]<<endl;
decrypt(array);
cout<<"Decrypted number: "<<array[0]<<array[1]<<array[2]<<array[3]<<endl;

}

=============================================================================================


Related Solutions

I DO NOT have a specific question, my question here is GENERAL HOW TO DETERMINE IF...
I DO NOT have a specific question, my question here is GENERAL HOW TO DETERMINE IF A COMPOUND IS POLAR OR NONPOLAR? I know how to figure about the number of valence electrons and I KNOW how to draw lewis structure, but then I just mess every thing up. I want to know how to determine if it's polar or not. for example: sicl2f2, co2, xef2, xeo4 ... I KNOW how to draw the lewis structure, but this I CANNOT...
I have looked at other answers on here in regards to this question. But I do...
I have looked at other answers on here in regards to this question. But I do not know what "In" stands for and those who answer are using different descriptions than I am used to. Here is the question. Number of Periods. How long will it take for $400 to grow to $1,000 at the interest rate specified? (LO1) a. 4% b. 8% c. 16%. Could someone please break this down a little further for me.
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
how do i start with this question
how do i start with this question
Here is the question. I understand what the answer is but need to know how you...
Here is the question. I understand what the answer is but need to know how you calculate the PV at 12% in year 5 to equal .567. You have worked in XYZ Corporation for the last five years and have a more optimistic view of the firm's future FCFs. In your personal/professional opinion, XYZ could generate $4 million FCF next year, $4.5 million in year two, $5 million in year three, $5.5 million in year four, $6 million in year...
1. A company that wants to send data over the Internet will use an encryption program...
1. A company that wants to send data over the Internet will use an encryption program to ensure data security. All data will be transmitted as four-digit integers. The application should read a four-digit integer entered by the user and encrypt it as follows:  Replace each digit with the remainder of the new value divided by 10 by adding 6 to the digit. Then replace the number in the first digit with the third, and the number in the...
1) A company that wants to send data over the Internet will use an encryption program...
1) A company that wants to send data over the Internet will use an encryption program to ensure data security. All data will be transmitted as four-digit integers. The application should read a four-digit integer entered by the user and encrypt it as follows:  Replace each digit with the remainder of the new value divided by 10 by adding 6 to the digit. Then replace the number in the first digit with the third, and the number in the...
1. A company that wants to send data over the Internet will use an encryption program...
1. A company that wants to send data over the Internet will use an encryption program to ensure data security. All data will be transmitted as four-digit integers. The application should read a four-digit integer entered by the user and encrypt it as follows:  Replace each digit with the remainder of the new value divided by 10 by adding 6 to the digit. Then replace the number in the first digit with the third, and the number in the...
how do i run 3 files on c++?
how do i run 3 files on c++?
I want to copy all the elements in f1() to f2() but in reverse order (C++)...
I want to copy all the elements in f1() to f2() but in reverse order (C++) this is my code: #include <iostream> #include <iomanip> using namespace std; const int R=10; const int C=10; int Array[R][C] ; ///////////////////////////////////// void f1(){ for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){ Array[i][j]= (rand () %100) + 1; } } for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){    cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<" ";    } cout<<endl; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT