# encryption / decryption program

• 07-22-2008
epidemic
encryption / decryption program
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; }```
• 07-22-2008
tabstop
The opposite of + is -, not %. And you're not unshuffling the numbers.

On a side note, this line
Code:

`if ( Third <= 0 ) { Third = 1; } // if third = zero we get a 3digit back`
makes it impossible to decrypt the number, since there are multiple inputs going to the same output.
• 07-22-2008
Daved
When doing the opposite of a series of operations, you not only have to do the opposite operation (like - instead of +) but you have to go in the opposite order.

Unfortunately, there's no direct opposite of &#37;. You have to think about your encryption and the possible values before and after the call to % during that encryption, then use that knowledge to figure out how to go back for that step.
• 07-22-2008
epidemic
oh man :( I cant get it figured out
• 07-22-2008
Daved
There are only 10 possible digits (0-9). Encrypt each digit on paper and find out what the result would be just for the digit encryption (don't rearrange the order like in your program).

Then, figure out how to go backwards. It isn't a simple exercise, although it is just logic.

Once you've figured that out, go back to your program. Your encryption encrypts each digit, then rearranges them. To decrypt, you need to rearrange the digits back into the correct order, then decrypt each digit individually like you figured out on paper.
• 07-23-2008
epidemic
ok man tnx i will try