# I need help to write encrypt and decrypt 4 digit numbers.

• June 28th, 2013, 08:28 PM
shin777
I need help to write encrypt and decrypt 4 digit numbers.
So basically, I have to write a program that prompt 4 digit number in one dialog box to encrypt the numbers. encryptions are following.
Add 7 to each digit and then %10, swap 1st and 3rd digit, swap 2nd and 4th digit. display it in console box. Application should use loops to implement the algorithm
So here is my encrypt code.

Code :

```import javax.swing.JOptionPane;     public class Encryption { public static void main(String[] args) {   String inputString; int num, digit1, digit2, digit3, digit4, encrypt;   do { inputString = JOptionPane.showInputDialog("Please enter 4 digit numbers"); num = Integer.parseInt(inputString); } while(num/1000 == 0 || num/10000 != 0);   digit1 = num/1000; digit2 = num/100; digit3 = num/10; digit4 = num/1;   digit1 = (digit1+7)%10; digit2 = (digit2+7)%10; digit3 = (digit3+7)%10; digit4 = (digit4+7)%10;   encrypt = digit1; digit1 = digit3; digit3 = encrypt;   encrypt = digit2; digit2 = digit4; digit4 = encrypt;     System.out.println("Your encrypted codes are: " + digit1 + digit2 + digit3 + digit4);       } }```

so far it seems to be working but is there another way(perhaps simpler way) to do it? Did i make any mistake?

Now.. here are my decryption code but this one seems to have problem. It works as long as first digit i put isn't 0.
What did I do wrong here? Again, I need to know way to fix it and if there's better way to do it, let me know. Thanks.

Code :

```import javax.swing.JOptionPane;     public class Decryption { public static void main(String[] args) {   String inputString; int num, digit1, digit2, digit3, digit4, decrypt;   do { inputString = JOptionPane.showInputDialog("Please enter 4 digit numbers"); num = Integer.parseInt(inputString); } while(num/1000 == 0 || num/10000 != 0);   digit1 = num/1000; digit2 = num/100%10; digit3 = num/10%10; digit4 = num%10;   if(digit1 <= 6 && digit1 >= 0) digit1 = digit1 + 10; if(digit2 <= 6 && digit2 >= 0) digit2 = digit2 + 10; if(digit3 <= 6 && digit3 >= 0) digit3 = digit3 + 10; if(digit4 <= 6 && digit4 >= 0) digit4 = digit4 + 10;   digit1 = digit1 - 7; digit2 = digit2 - 7; digit3 = digit3 - 7; digit4 = digit4 - 7;   decrypt = digit1; digit1 = digit3; digit3 = decrypt;   decrypt = digit2; digit2 = digit4; digit4 = decrypt;     System.out.println("Your encrypted codes are: " + digit1 + digit2 + digit3 + digit4);   } }```
• June 28th, 2013, 10:16 PM
theonlydvr
Re: I need help to write encrypt and decrypt 4 digit numbers.
Change the do {} while loop line to this in both programs.

Code java:

`while(num/10000 != 0 || inputString.length() != 4);`
• June 29th, 2013, 02:08 AM
shin777
Re: I need help to write encrypt and decrypt 4 digit numbers.
Quote:

Originally Posted by theonlydvr
Change the do {} while loop line to this in both programs.

Code java:

`while(num/10000 != 0 || inputString.length() != 4);`

Thank you. It works now. Do rest of code looks ok though?
I still have hard time understanding how to undo modulus. Could you explain how it works or
is there good link to explain it? I got help on undoing modulus formula but I am still confused. :(
• June 29th, 2013, 01:07 PM
theonlydvr
Re: I need help to write encrypt and decrypt 4 digit numbers.
Your code looks good and it is the fastest way of completing the problem. Modulo gives you the remainder of the division operation as you probably know. Technically it is impossible to reverse modulo operations because the remainder can be the remainder of any operation. In your program you are not using the modulo operator for its normal purpose of getting the right-most digit. So what you are really doing for any numbers greater than 2 is adding 7 to the number and subtracting 10. So your code is singling out the values that need the reverse addition.

I know this is kind of cryptic but I hope it helps. The bottom line is you can technically not reverse a true modulo operation.