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. Java version!
Thanks for the question, here are the two pogram that encrypt a number and another program that takes in the encrypted number and then decrypt back to the original number.
Here are the two classes -
=====================================================================
public class Encrypt {
public static void
encrypt(int number) {
int
firstDigit = number % 10;
number = number /
10;
int
secondDigit = number % 10;
number /= 10;
int
thirdDigit = number % 10;
int
fourthDigit = number / 10;
int
firstDigitRemainder = (firstDigit + 7) % 10;
int
secondDigitRemainder = (secondDigit + 7) % 10;
int
thirdDigitRemainder = (thirdDigit + 7) % 10;
int
fourthDigitRemainder = (fourthDigit + 7) % 10;
System.out.println();
int
encryptedNumber = secondDigitRemainder * 1000 + firstDigitRemainder
* 100 + fourthDigitRemainder * 10 + thirdDigitRemainder;
System.out.println("Encrypted Number
" + encryptedNumber);
}
public static void
main(String[] args) {
encrypt(1248);
}
}
=====================================================================
public class Decrypt {
public static void
decrypt(int encrypted){
int
firstDigit = encrypted % 10;
encrypted = encrypted /
10;
int
secondDigit = encrypted % 10;
encrypted /= 10;
int
thirdDigit = encrypted % 10;
int
fourthDigit = encrypted / 10;
int
firstDigitDecrypted =
firstDigit>=7?firstDigit-7:3+firstDigit;
int
secondDigitDecrypted =
secondDigit>=7?secondDigit-7:3+secondDigit;
int
thirdDigitDecrypted =
thirdDigit>=7?thirdDigit-7:3+thirdDigit;
int
fourthDigitDecrypted =
fourthDigit>=7?fourthDigit-7:3+fourthDigit;
int
decryptedNumber =
secondDigitDecrypted*1000+firstDigitDecrypted*100+fourthDigitDecrypted*10+thirdDigitDecrypted;
System.out.println("Decrypted Number:
"+decryptedNumber);
}
public static void
main(String[] args) {
decrypt(1589);
}
}
=====================================================================