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.
2) 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.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
import java.util.Scanner;
class Encryption {
public static String encrypt(String number) {
int arr[] = new int[4];
for (int i = 0; i < 4; i++) {
char ch = number.charAt(i);
arr[i] = Character.getNumericValue(ch);
}
for (int i = 0; i < 4; i++) {
int temp = arr[i];
temp += 7;
temp = temp % 10;
arr[i] = temp;
}
int temp = arr[0];
arr[0] = arr[2];
arr[2] = temp;
temp = arr[1];
arr[1] = arr[3];
arr[3] = temp;
int newNumber = 0;
for (int i = 0; i < 4; i++) newNumber = newNumber * 10 + arr[i];
String output = Integer.toString(newNumber);
if (arr[0] == 0) output = "0" + output;
return output;
}
public static String decrypt(String number) {
int arr[] = new int[4];
for (int i = 0; i < 4; i++) {
char ch = number.charAt(i);
arr[i] = Character.getNumericValue(ch);
}
int temp = arr[0];
arr[0] = arr[2];
arr[2] = temp;
temp = arr[1];
arr[1] = arr[3];
arr[3] = temp;
for (int i = 0; i < 4; i++) {
int digit = arr[i];
switch (digit) {
case 0:
arr[i] = 3;
break;
case 1:
arr[i] = 4;
break;
case 2:
arr[i] = 5;
break;
case 3:
arr[i] = 6;
break;
case 4:
arr[i] = 7;
break;
case 5:
arr[i] = 8;
break;
case 6:
arr[i] = 9;
break;
case 7:
arr[i] = 0;
break;
case 8:
arr[i] = 1;
break;
case 9:
arr[i] = 2;
break;
}
}
int newNumber = 0;
for (int i = 0; i < 4; i++) newNumber = newNumber * 10 + arr[i];
String output = Integer.toString(newNumber);
if (arr[0] == 0) output = "0" + output;
return output;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a 4 digit integer:");
String number = sc.nextLine();
String encryptedNumber = encrypt(number);
System.out.println("The decrypted number is:" + encryptedNumber);
System.out.println("The original number is:" + decrypt(encryptedNumber));
}
}