In order to send a secret message, some form of encryption has been used since a
ID: 3879928 • Letter: I
Question
In order to send a secret message, some form of encryption has been used since ancient times. In a symmetric cipher, the sender encrypts the message with a private key that is only shared with the recipient. The recipient can then decrypt the message by using the exact same key. Since every character in the computer is represented as a stream of bits, encrypting a character means transforming its bit sequence. One way to transform it is to apply the XOR operator bitwise between the original character and another character, the secret key. The result of the operation is the encrypted character. The XOR operator is well suited for symmetric encryption, because the recipient can apply bitwise XOR between the encrypted character and the key and retrieve the original character. In short, if character c is encrypted with key k, then: d=eXOR k where e is the encrypted character and d is the decrypted character. By the definition of XOR, d should be equal to c Write a program that asks the user for a character to encrypt and another character that will be used as the key. Then, the program computes the encrypted character and the decrypted character as explained. Sample execution, where the user provides r and k Character to Encrypt: r Key: k Original in Binary: 1110018 Key in Binary: 1101011 Encrypted in Binary: 11001 Decrypted in Binary: 1110010 Sample execution, where the user provides # and 9: Character to Encrypt: # Key: 9 Original in Binary: 190011 Key in Binary: 111001 Encrypted in Binary: 11010 Decrypted in Binary: 100011 Name your class XORCharacter.Explanation / Answer
/*The whole answer is well commented, so please create a java file in you ide and
* create a class with name "XORCharacter" and copy whole answer
*
* To Find XOR in java, we can use just ^ operator
* I am going to use Integer.toBinaryString(x) to print binary format of number in java
* ex: System.out.printf("Original in Binary: %s ",x) => It will print int x in binary format
* Sample Input:Character: r
*Key: k
* Sample Output:
* Character to Encrypt: r
*Key: k
*Original in Binary: 1110010
*Key in Binary: 1101011
*Encrypted in Binary: 11001
*Decrypted in Binary: 1110010
*
* Sample Output:
* Character to Encrypt: #
Key: 9
Original in Binary: 100011
Key in Binary: 111001
Encrypted in Binary: 11010
Decrypted in Binary: 100011
*
*
* */
//Java Program for encryption and Decryption
import java.util.*;
public class XORCharacter {
public static void main(String[] args) {
//Instantiating Scanner to read input from user
Scanner scan=new Scanner(System.in);
//Reading character from console
System.out.println("Enter Character: ");
char c=scan.next().charAt(0);
//Reading key from console
System.out.println("Enter Key: ");
char k=scan.next().charAt(0);
//Getting ascii values of character and key
int cASCII=(int)c;
int kASCII=(int)k;
//Encrypting using XOR(^)
int e=cASCII ^ kASCII;
//Decrypting using XOR(^)
int d=e ^ kASCII;
//Printing out all values
System.out.printf("Character to Encrypt: %c ",c);
System.out.printf("Key: %c ",k);
System.out.printf("Original in Binary: %s ",Integer.toBinaryString(cASCII));
System.out.printf("Key in Binary: %s ",Integer.toBinaryString(kASCII));
System.out.printf("Encrypted in Binary: %s ",Integer.toBinaryString(e));
System.out.printf("Decrypted in Binary: %s ",Integer.toBinaryString(d));
}
}