In: Computer Science
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];
}
}
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;
}
=============================================================================================