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

Encryption algorithms can be achieved via some basic algorithms. For example, fo

ID: 673867 • Letter: E

Question

Encryption algorithms can be achieved via some basic algorithms.

For example, for an 8 digits number;

Every digit can be multiplied by 2 if it is even digit or multiplied by 3 if it is odd digit.

The original digits are summed up, and inserted tothe end of the new combination, as four digit sum, this will help in the decryption process.

Moreover, the new combination should also contain the number of even digits and odd digits to end as two digits sum.

Write a C program to read any integer, convert it to 8 digits then, encrypt it and decrypt it back to the original case.

The program should keep running until terminated by user using EOF character.

Your program should contain two user-defined functions Encrypt and Decrypt.

Explanation / Answer

#include <iostream>
int Encrypt (int value)
{
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 )
{
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 [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! ";
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 ";
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 ";
std::cin >> to_be_decrypted;
decrypted = Decrypt (to_be_decrypted);
std::cout <<"Decrypted: " << decrypted << std::endl;
}
std::cin.get();
std::cin.get();
return 0;
}