In: Computer Science
1. A company that wants to send data over the Internet will use an encryption program to ensure data security. All data will be transmitted as four-digit integers. The application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the remainder of the new value divided by 10 by adding 6 to the digit. Then replace the number in the first digit with the third, and the number in the second digit with the fourth. Print the encrypted integer on the screen. Write a separate application where an encrypted four-digit integer is entered and decrypted (reversing the encryption scheme) and finds the original number.
Note: code should be written in Java Language.
Summary:
The program wriiten here is very simple, just go through the comments and print statements for better understanding.
Note:- This program works perfectly as per your requirement but there is no proper way to reverse the encryption method given in question to decrypt the value.
---------------------------------------------Encryption.java----------------------------------------
import java.util.Scanner;
public class Encryption {
public static int encrypt(int number) {
int[] digits = new int[4];
for (int i = 0; i < 4; i++) {
digits[i] = ((number % 10) + 6) % 10; // replace each digit with remainder of new value divided by 10 by adding 6 to the digit.
number = number / 10;
}
// Replacing digit 1 with digit 3 and digit 2 with digit 4
String str = "" + digits[1] + digits[0] + digits[3] + digits[2];
return Integer.parseInt(str);
}
public static int decrypt(int encryptedNumber) {
int[] digits = new int[4];
int temp = 0;
for (int i = 0; i < 4; i++) {
temp = (encryptedNumber % 10);
if (temp == 0) {
digits[i] = 10 - 6;
}
else if (temp == 2) {
digits[i] = 6;
} else if (temp == 5) {
digits[i] = 9;
} else if (temp == 4) {
digits[i] = 8;
} else if (temp == 3) {
digits[i] = 7;
} else if (temp == 6) {
digits[i] = 0;
} else if (temp == 1) {
digits[i] = 5;
} else
digits[i] = temp - 6;
encryptedNumber = encryptedNumber / 10;
}
System.out.println("" + digits[3] + digits[2] + digits[1] + digits[0]);
String str = "" + digits[1] + digits[0] + digits[3] + digits[2];
return Integer.parseInt(str);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 4 digit number to encrypt");
int number = scanner.nextInt();
int encryptedNumber = encrypt(number);
System.out.println("After Encryption: " + encryptedNumber);
int decriptedNumber = decrypt(encryptedNumber);
String result = String.valueOf(decriptedNumber);
if (result.length()!=4)
System.out.println("After Decryption: 0" + decriptedNumber);
else
System.out.println("After Decryption: " + decriptedNumber);
}
}
Both encrypt and decrypt methods are implemented in the same class Encryption.
Code Screenshot:
Output:
Its one of the most difficult problem to revert the modulos of two number as their is no direct answers for that.
I tried by best to solve this problem to work perfectly correct.
Please rate thumbs up if you like the answer.
Thanks you!!!!