• 09-16-2012, 07:32 PM
dksellou
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
• 09-16-2012, 09:14 PM
DarrylBurke
Please go through BB Code List - Java Programming Forum

Quote:

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
• 09-16-2012, 10:21 PM
dksellou
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....
• 09-17-2012, 03:43 AM
dksellou
without trying to be sarcastic...your help is awesome....did you even thing to teach JAVA.....you explain so well...
• 09-17-2012, 06:27 AM
DarrylBurke