# Simple encryption/ decryption problem

• May 6th, 2012, 01:08 PM
searchformeaning
Simple encryption/ decryption problem
I have a question considering a problem I have worked on. Here is the problem statement:
Quote:

A company wants to transmit data over the telephone but is concerned that its phones may be tapped. It has asked you to write a program that will encrypt the data so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: 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 print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it to form the original number.
My code Encryption part:

Code :

```package java4_37; import java.util.Scanner;   public class Java4_37 { public static void main(String[] args) { int firstDigit, secondDigit, thirdDigit, fourthDigit, number, temp; Scanner input = new Scanner( System.in );   do{ System.out.print(" Enter Number: "); number = input.nextInt(); }while(number / 1000 == 0 || number / 10000 != 0 );   firstDigit = number / 1000; secondDigit = number / 100 % 10; thirdDigit = number / 10 % 10; fourthDigit = number % 10;   firstDigit = (firstDigit + 7) % 10; secondDigit = (secondDigit + 7) % 10; thirdDigit = (thirdDigit + 7) % 10; fourthDigit = (fourthDigit + 7) % 10;   temp = firstDigit; firstDigit = thirdDigit; thirdDigit = temp;   temp = secondDigit; secondDigit = fourthDigit; fourthDigit = temp;   System.out.printf("the encrypted number is %d%d%d%d\n", firstDigit, secondDigit, thirdDigit, fourthDigit); } }```

and the decryption part:

Code :

```package java4_37b;   import java.util.Scanner;   public class Java4_37b { public static void main(String[] args) { int firstDigit, secondDigit, thirdDigit, fourthDigit, number, temp; Scanner input = new Scanner( System.in );   do{ System.out.print(" Enter Number: "); number = input.nextInt(); }while(number / 1000 == 0 || number / 10000 != 0 );   firstDigit = number / 1000; secondDigit = number / 100 % 10; thirdDigit = number / 10 % 10; fourthDigit = number % 10;   if(firstDigit <= 6 && firstDigit >= 0) firstDigit = firstDigit + 10; if(secondDigit <= 6 && secondDigit >= 0) secondDigit = secondDigit + 10; if(thirdDigit <= 6 && thirdDigit >= 0) thirdDigit = thirdDigit + 10; if(fourthDigit <= 6 && fourthDigit >= 0) fourthDigit = fourthDigit + 10;   firstDigit = firstDigit - 7; secondDigit = secondDigit - 7; thirdDigit = thirdDigit - 7; fourthDigit = fourthDigit - 7;   temp = firstDigit; firstDigit = thirdDigit * 1000; thirdDigit = temp * 10;   temp = secondDigit; secondDigit = fourthDigit * 100; fourthDigit = temp;   System.out.printf("the encrypted number is %d\n", firstDigit + secondDigit + thirdDigit + fourthDigit); } }```

First, I tried that code myself and it was working fine (I don't know if it has any logical errors). Second thing and that's the reason I posted my question. One solution to this problem for decryption was to add 3 to the digit and then get the remainder of 10.

digit = (digit + 3) % 10;

and then swap digit to get the original number. But I don't know WHY we add 3. What is the logic of it. Actually, even if we converted it to an equation something like this:

(x + 7) % 10 = 4 where x represents the original digit before encryption

I don't remember that I used the remainder operator in any math classes :-? so I don't know who to get rid of it to solve the equation (we divide to get rid of multiplication and subtract to get rid of addition or vice versa).

So again what is the logic of adding 3 and using the remainder operator?
• May 6th, 2012, 03:18 PM
stdunbar
Re: Simple encryption/ decryption problem
The algorithm is expressed in quite a few more words than needed. In reality this is a simple look up table:

0 -> 7
1 -> 8
2 -> 9
3 -> 0
4 -> 1
5 -> 2
6 -> 3
7 -> 4
8 -> 5
9 -> 6

So, adding 3 to the "encoded" number yields the "real" number for the encoded values 0 through 6. For encoded values > 6 adding 3 and "modding" (% operator) with 10 gets you the correct number. Indeed, you can do that with values less than 7 also but the % operator doesn't actually do anything.
• May 7th, 2012, 01:34 AM
searchformeaning
Re: Simple encryption/ decryption problem
Thanks for your reply. That makes everything clear. I thought that it's more complicated than that :o . Anyway, what do you think of my code? Is it clear, well-written or it has problems?