(Enforcing Privacy with Cryptography) The explosive growth of Internet communica
ID: 3537507 • Letter: #
Question
(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%u2014with the most
advanced schemes%u2014impossible) for unauthorized users to read. In this exercise you%u2019ll 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.
Explanation / Answer
package demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Encryption {
public static int getEncryptedNumber(int input){
StringBuffer sb = new StringBuffer();
int result = 0;
for(int i=0; i<4; i++){
int a = input % 10;
a = a + 7;
a = a % 10;
input = input/10;
result = result+ (a*(int)Math.pow(10, i));
}
result = getSwappedResult(result);
return result;
}
private static int getSwappedResult(int result) {
String input = Integer.toString(result);
char[] charInput = input.toCharArray();
for(int i=0; i<2; i++){
int j=i+2;
char temp = charInput[i];
charInput[i] = charInput[j];
charInput[j] = temp;
}
StringBuffer sb = new StringBuffer();
for(int i=0; i<4; i++)
sb.append(charInput[i]);
return Integer.parseInt(sb.toString());
}
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Enter a 4 digit number: ");
int input = Integer.parseInt(br.readLine());
System.out.println("Encrypted number: "+getEncryptedNumber(input));
System.out.print("Do you want to continue(y/n): ");
String cont = br.readLine();
if(cont.charAt(0)=='y' || cont.charAt(0)=='Y')
continue;
else
System.exit(0);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.