I am following this book and an asignment but I just cant figure the decryption part out! can anyone help?
Code:
/*
(Cryptography) 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.
Write a separate program that inputs an encrypted
fourdigit integer and decrypts it to form the original number.
*/
#include <iostream>
int Encrypt (int value)
{
// compute seperate digits.
// 1234
int First,Second,Third,Forth;
First = value/1000; // 1
Second = (value%1000) / 100; // 2
Third = (value%100) / 10; // 3
Forth = value % 10; // 4
First = (First+7) % 10; // 8
Second = (Second+7) % 10; // 9
Third = (Third+7) % 10; // 0
Forth = (Forth+7) % 10; // 1
if ( Third <= 0 ) { Third = 1; } // if third = zero we get a 3digit back
int Encrypted_number = (Third*1000)+(Forth*100)+(First*10)+(Second); // 1189
return (Encrypted_number);
}
int Decrypt ( int value )
{
// 1189
int First,Second,Third,Forth;
First = value/1000; // 1
Second = (value%1000) / 100; // 1
Third = (value%100) / 10; // 8
Forth = value % 10; // 9
First = (First%7) % 10; // 1
Second = (Second%7) % 10; //
Third = (Third%7) % 10; //
Forth = (Forth%7) % 10; //
if ( Third <= 0 ) { Third = 1; }
int Decrypted_number = ((First*1000)+(Second*100)+(Third*10)+(Forth));
return (Decrypted_number);
}
void Printmenu()
{
std::cout <<"[1] To encrypt a 4 digit number\n[2] To decrypt a 4 digit number" << std::endl;
}
int GetChoise ()
{
int choise;
std::cin >> choise;
while (choise != 1 && choise != 2)
{
std::cout <<"Please enter a menu item!\n";
Printmenu();
std::cin >> choise;
}
return choise;
}
int main ()
{
Printmenu();
int Choise;
Choise = GetChoise ();
if (Choise == 1)
{
int to_be_encrypted,encrypted;
std::cout <<"Please enter a 4 digit integer number for encryption\n";
std::cin >> to_be_encrypted;
encrypted = Encrypt(to_be_encrypted);
std::cout <<"The original number: " << to_be_encrypted << std::endl;
std::cout <<"The encrypted number: " << encrypted << std::endl;
}
else if (Choise == 2)
{
int to_be_decrypted,decrypted;
std::cout <<"Please enter a 4 digit integer number for Decryption\n";
std::cin >> to_be_decrypted;
decrypted = Decrypt (to_be_decrypted);
std::cout <<"Decrypted: " << decrypted << std::endl;
}
std::cin.get();
std::cin.get();
return 0;
}