# Enforcing Privacy with Cryptography in Java

(Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data.

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. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]

## Enforcing Privacy with Cryptography in Java

``````/*
*       Filename:  EncryptDecrypt.java
*
*    Description:  Exercise 4.38 - Enforcing Privacy with Cryptography
*
*  @Author:  Bilal Tahir Khan Meo
*  Website: https://codeblah.com
* =====================================================================================
*/
public class EncryptDecrypt{
// ensure number is of length 4
public boolean validate(int value){
return (Integer.toString(value).length() == 4) ? true : false;
}
// 1. Replace each digit with the result of adding 7 to it and getting the
//    remainder after dividing the new value by 10
public int encrypt(int value){
int arrValue[] = new int;

// split int into array
// countdown so as to not reverse the original order
for(int i=3; i>=0; i--){
arrValue[i] = value % 10;
value /= 10;
}

arrValue = swapDigits(arrValue);

for(int i=0; i<4; i++){
arrValue[i] = (arrValue[i] + 7) % 10;
}

}
public int decrypt(int value){
int arrValue[] = new int;

// split int into array
// countdown so as to not reverse the original order
for(int i=3; i>=0; i--){
arrValue[i] = value % 10;
value /= 10;
}

// swapping returns to original order
arrValue = swapDigits(arrValue);

// decrypt originally ordered values
for(int i=0; i<4; i++){
arrValue[i] = (((arrValue[i] + 10) - 7) % 10);
}

}
// 2. swap the first and third, second and fourth digits
private int[] swapDigits(int[] arrValue){
// swap first with third
int tempValue = arrValue;
arrValue = arrValue;
arrValue = tempValue;

// swap second with fourth
tempValue = arrValue;
arrValue = arrValue;
arrValue = tempValue;

return arrValue;
}
// 3. convert int[] to int for easy printing
private int toInt(int[] arrValue){
// convert int[] to int
StringBuilder temp = new StringBuilder();

for(int i=0; i<arrValue.length; i++){
temp.append(arrValue[i]);
}

return Integer.parseInt(temp.toString());
}
}``````

EncryptDecryptTest.java

``````/*
*       Filename:  EncryptDecryptTest.java
*
*    Description:  Exercise 4.38 - Enforcing Privacy with Cryptography
*
*  @Author:  Bilal Tahir Khan Meo
*  Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;

public class EncryptDecryptTest{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
EncryptDecrypt ed = new EncryptDecrypt();

int value = requestInput("Enter a 4 digit number for encryption: ", sc);

while(ed.validate(value) != true){
value = requestInput("Enter a 4 digit number for encryption: ", sc);
}
int encrypted = ed.encrypt(value);
int decrypted = ed.decrypt(encrypted);

System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
}
public static int requestInput(String s, Scanner sc){
System.out.print(s);
return sc.nextInt();
}
}``````