A company wants to transmit data over the telephone, but it
is concerned that its phones may be tapped. All of its data
is transmitted as four-digit integers. It has asked you to
write a program that will encrypt its data so that the data
may be transmitted more securely. Your script should read a
four digit integer entered by the user in a prompt dialog
and encrypt it as follows: Replace each digit by (the sum
of that digit plus 7) modulus 10. Then swap the first digit
with the third, and swap the second digit with the fourth.
Then output XHTML text that displays the encrypted
integer.

Answer Posted / hafiz waqas

import java.util.Scanner;


public class Main {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner=new Scanner(System.in);

boolean flag = true; //check value is 4 digit or not..
int beforeIncryption = 0;//take plain text
int temp; // for processing plain text
int incryptValue; // for storing after encryption process

//Check value is 4 digit or not
while(flag){
System.out.println("Please Enter exactly 4 digit enteger ");
beforeIncryption = myScanner.nextInt();

if( beforeIncryption-1000 >= 0 && beforeIncryption-1000
<= 8999)
flag=false;
}
temp = beforeIncryption;

int v1 , v2 , v3,v4;// v1 contain 1st digit,v2 contain 2nd
digit and so on...

//take separate digit and apply encryption process
v4=temp%10;
v4+=7;
v4%=10;

temp/=10;

v3=temp%10;
v3+=7;
v3%=10;

temp/=10;

v2=temp%10;
v2+=7;
v2%=10;

temp/=10;

v1=temp%10;
v1+=7;
v1%=10;

//swapping values 1st digit with 3rd digit and 2nd with
4th....
temp=v1;
v1=v3;
v3=temp;

temp=v2;
v2=v4;
v4=temp;


incryptValue = v1*1000 + v2*100 + v3*10 + v4*1;// combine
4 separate digit into 1 number
System.out.println("Encrypted data is
"+incryptValue);//display encryption result


//Program for decryption........

int myIncrypt;//take encrypted input from user
System.out.println("Enter encrypted data ");//prompt for user
myIncrypt=myScanner.nextInt();//take value for apply
decryption process

int dv1,dv2,dv3,dv4,decryptValue;

//separate 1 four digit number into 4 separate 1 digit number
dv4=myIncrypt%10;
myIncrypt/=10;

dv3=myIncrypt%10;
myIncrypt/=10;

dv2=myIncrypt%10;
myIncrypt/=10;

dv1=myIncrypt%10;
myIncrypt/=10;

//swap values
temp=dv1;
dv1=dv3;
dv3=temp;

temp=dv2;
dv2=dv4;
dv4=temp;

//apply decryption process
dv1+=10;
dv1-=7;
dv1%=10;

dv2+=10;
dv2-=7;
dv2%=10;

dv3+=10;
dv3-=7;
dv3%=10;

dv4+=10;
dv4-=7;
dv4%=10;

//combine result
decryptValue=dv1*1000+dv2*100+dv3*10+dv4*1;
//display result
System.out.println("decrypted data is"+decryptValue);

}

}

Is This Answer Correct ?    25 Yes 15 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is overflow and what are the conditions for which overflow possible?

2079


can you provide me uniken pvt ltd. technical interview question and answer?

2614


Please state briefly the reasons why you think you are an outstanding candidate for this job

12278


Tell me all about production of drought and its losses In the boiler?

1229


WRITE A C PROGRAM TO REVERSE THE LINK LIST WITHOUT CREATING NEW LIST?

3266






what is c dot

1498


Has anyone attended interviews in IITs for positions like Research fellowships/assistants? If yes, please share the experience about how complex the interview was, etc. etc.

1493


i am shortlisted in corporation bank for the post of computer officer the next phase is group discussion. i want to know how to prepare and what about the topics for preparing thanking you if you have any suggestion please give me prabhatmishra21@rediffmail.com

2123


what is the difference between VARCHAR and VARCHAR2?

21466


What are special features of Silk Test ? In what way it is advanced than QTP?

1660


how updates the data in target tables with tpump and mload? with low volume data and more duplicates... so in this case which perfomance is best? and write update query for tpump&mload?

1681


briefly explain about your project? please tell me about this answer . my current project is ERP domain web based application.please please help me

1615


what is the difference between error,exception,bug,defect,fault..?

1901


Let an denote the number of bit strings of length n that do not have two consecutive zeroes. Find a recurrence relation for the number of bit strings of length n that do not have two consecutive zeroes. Hence find a4.

1883


synchronous and asynchronous transmission

1484