1. Member Join Date
Sep 2012
Posts
13
Rep Power
0

## A question about encryption-decryption

Forum,
below is the program questions and the solution following it.

A company wants to transmit data over the telephone, but is concerned that its phones may
be tapped. It has asked you code an application to help in the security matter. write a program that will encrypt its data so that the data may be transmitted more securely. All of its data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user in an input dialog 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.

// Program encrypts a four digit number.
import java.awt.*;
import javax.swing.JOptionPane;

public class Encrypt {

public static void main( String args[] )
{
int number, // original number
digit1, // first digit
digit2, // second digit
digit3, // third digit
digit4, // fourth digit
encryptedNumber; // encrypted number

// enter four digit number to be encrypted
number = Integer.parseInt( JOptionPane.showInputDialog(
"Enter a four digit number: " ) );

// encrypt
digit1 = ( number / 1000 + 7 ) % 10;
digit2 = ( number % 1000 / 100 + 7 ) % 10;
digit3 = ( number % 100 / 10 + 7 ) % 10;
digit4 = ( number % 10 + 7 ) % 10;

encryptedNumber = digit1 * 10 + digit2 +
digit3 * 1000 + digit4 * 100;

JOptionPane.showMessageDialog( null,
"Encrypted number is " + encryptedNumber,
"Encryptor", JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );

} // end method main

} // end class Encrypt

I need someone to explain the code:

// encrypt
digit1 = ( number / 1000 + 7 ) % 10;
digit2 = ( number % 1000 / 100 + 7 ) % 10;
digit3 = ( number % 100 / 10 + 7 ) % 10;
digit4 = ( number % 10 + 7 ) % 10;

I am so confused!!! for example if the user enters 3527 how is this processed with the code above?
Thanks  Reply With Quote

2. ## Re: A question about encryption-decryption

Please go through BB Code List - Java Programming Forum

I need someone to explain the code:
Weren't you supposed to write it yourself? What, specifically, do you not understand? The / operator? the % operator? the = operator? use of parentheses?

db  Reply With Quote

3. Member Join Date
Sep 2012
Posts
13
Rep Power
0

## Re: A question about encryption-decryption

1)For example the digit2 = ( number % 1000 / 100 + 7 ) % 10;
why do we have to use the %1000 and then the %10

2)No, I am not supposed to write this code myself because I am not taking any class. I am a self learner....  Reply With Quote

4. Member Join Date
Sep 2012
Posts
13
Rep Power
0

## Re: A question about encryption-decryption

without trying to be sarcastic...your help is awesome....did you even thing to teach JAVA.....you explain so well...  Reply With Quote

5. ## Re: A question about encryption-decryption Originally Posted by dksellou I am a self learner....
You can find answers to many of your needs in The Java Tutorials

FOr this question, specifically: Operators (The Java Tutorials > Learning the Java Language > Language Basics)

db  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•