Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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.

    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);
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I need help to write encrypt and decrypt 4 digit numbers.

    Change the do {} while loop line to this in both programs.

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

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help to write encrypt and decrypt 4 digit numbers.

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

    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.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. [SOLVED] Encrypt Decrypt
    By dianac in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 14th, 2013, 11:01 AM
  2. Creating a 8 digit password mixture of numbers and letters
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2012, 02:53 PM
  3. Adding 100 single digit numbers to a Text Field
    By CodyReuille in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2012, 03:58 PM
  4. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  5. Need Help for encrypt/decrypt.
    By superdhebz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2010, 12:17 PM