# simple encryption problem

This topic is 5188 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just looking for feedback on how I solved this. This is from Exercise 2.37 of the book "C++ How to Program 4e". Functions and Arrays have not yet been covered so I don't use those here. :) The exercise reads as follows: "A company wants to transmit data over a 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 ecrypt 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 ecrypted integer. Write a separate program that inputs an encrypted four digit integer and decrypts it to form the original number." I've combined the exercise into one program for simplicity. It seems to work fine but I would like to know if anyone has any tips on making it better without using any advanced functionality not yet covered. :) my code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int input;
int n1, n2, n3, n4;
int copy;

cout << "Enter a 4 digit number: " << endl;
cin >> input;

// break number down
copy = input;
n4 = copy % 10;
copy /= 10;
n3 = copy % 10;
copy /= 10;
n2 = copy % 10;
copy /= 10;
n1 = copy;

// encrypt number
n1 = (n1 + 7) % 10;
n2 = (n2 + 7) % 10;
n3 = (n3 + 7) % 10;
n4 = (n4 + 7) % 10;

// move numbers around
int temp;

temp = n1;
n1 = n3;
n3 = temp;

temp = n2;
n2 = n4;
n4 = temp;

cout << "Encrypted number: ";
cout << n1 << n2 << n3 << n4 << endl << endl;  // display encrypted number

// decrypt to original
n1 = n1 - 7;
if ( n1 < 0 )
n1 += 10;

n2 = n2 - 7;
if ( n2 < 0 )
n2 += 10;

n3 = n3 - 7;
if ( n3 < 0 )
n3 += 10;

n4 = n4 - 7;
if ( n4 < 0 )
n4 += 10;

// move numbers back to original location

temp = n1;
n1 = n3;
n3 = temp;

temp = n2;
n2 = n4;
n4 = temp;

cout << "Decrypted number: ";
cout << n1 << n2 << n3 << n4 << endl << endl;	// display decrypted number

return 0;
}



##### Share on other sites
It looks ok except for the temp variables and the decryption. The temps are not needed. Just switch the order of the output. The decryption is almost the same as encryption. Just change the 7 to a 3 (10 - 7 = 3).

Also never use this method of encryption for a real program. It can be broken in seconds.

##### Share on other sites
Yea, I see what you mean about the temp not being needed here. I guess I could have just output:

cout << n3 << n4 << n1 << n2;

I guess both work equally well, your way definately is less work though.

I'm not sure your statement about (10-7=3) because the 10 only comes into play if n - 7 < 0. If I always add 3 to n than I think I will get incorrect results. Maybe I'm just not reading you right and thinking about that wrong.

Anyway, thanks for taking a look. It's always appreciated. I like to get as much insight into this stuff as I can. :)

##### Share on other sites
Heres why the 3 works:

t = 4 (Number to encode)
c = t + 7 mod 10 = 4 + 7 mod 10 = 11 mod 10 = 1 (Encodeing)
c + 3 mod 10 = 1 + 3 mod 10 = 4 = t (Decodeing)

Or:

(t + 7 mod 10) + 3 mod 10 = t + 7 + 3 mod 10 = t + 10 mod 10 = t mod 10

##### Share on other sites
ok that works with the number 1 and 4 but what if we make t any other number 1-9 besides 1 and 4?

By applying t = 2 to your example expression:

t = 2 (Number to encode)
c = t + 7 mod 10 = 2 + 7 mod 10 = 9 mod 10 = 9 (Encodeing)
c + 3 mod 10 = 9 + 3 mod 10 = 1 = t (Decodeing)

Now applying t = 9 to your example expression:

t = 9 (Number to encode)
c = t + 7 mod 10 = 9 + 7 mod 10 = 16 mod 10 = 1 (Encodeing)
c + 3 mod 10 = 1 + 3 mod 10 = 4 = t (Decodeing)

Am I missing something here?

##### Share on other sites
Ive underlined the correction

t = 2 (Number to encode)
c = t + 7 mod 10 = 2 + 7 mod 10 = 9 mod 10 = 9 (Encodeing)
c + 3 mod 10 = 9 + 3 mod 10 = 12 mod 10 = 2 = t (Decodeing)

##### Share on other sites
right, i was dividing by 10 not mod...brainfart :) I probably need to take a break and get refreshed. Getting myself mixed up :P

Thanks for the correction.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 30
• 9
• 16
• 12