In: Computer Science
A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user 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 (by reversing the encryption scheme) to form the original number.
Code: java Encryption
import java.util.*;
class Encryption{
public static void main(String args[]){
Scanner s=new
Scanner(System.in);
System.out.print("Enter number: ");
//asking integer from user.
int n=s.nextInt(),e=0,i,r;
for(i=1;i<=4;i++){
r=(n%10)+7;
e=(e*10)+(r%10); //calculating
encrypted integer
n=n/10;
}
n=0;
while(e>0){
r=e%10;
n=(n*10)+r;
e=e/10;
}
System.out.println("The encrypted
integer is "+n); //printing encrypted integer.
}
}
Output:
Code: java Decryption
import java.util.*;
class Decryption{
public static void main(String args[]){
Scanner s=new
Scanner(System.in);
System.out.print("Enter encrypted
integer: "); //asking encrypted integer from user.
int n=s.nextInt(),e=0,i,r;
for(i=1;i<=4;i++){
r=(n%10)-7;
e=(e*10)+(r+20)%10; //calculating
decrypted integer
n=n/10;
}
n=0;
while(e>0){
r=e%10;
n=(n*10)+r;
e=e/10;
}
System.out.println("The original
number is "+n); //printing original number.
}
}
Output: