Results 1 to 5 of 5
  1. #1
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

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

  4. #4
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: A question about encryption-decryption

    Quote Originally Posted by dksellou View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Encryption & Decryption
    By raj.mscking@gmail.com in forum New To Java
    Replies: 1
    Last Post: 02-01-2012, 02:21 PM
  2. Encryption and Decryption
    By jatinkansagara in forum Advanced Java
    Replies: 7
    Last Post: 06-27-2011, 03:08 PM
  3. PGP Encryption&Decryption
    By Deepa in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 06:22 AM
  4. Encryption/Decryption
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 11:58 AM
  5. Encryption/Decryption Through AOP
    By SirRawlins in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:22 PM

Posting Permissions

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