Alice recently read the news about Edward Snowden, and learned that encryption c
ID: 3906672 • Letter: A
Question
Alice recently read the news about Edward Snowden, and learned that encryption could be used to protect personal communication effectively. She decided to construct a simple encryption scheme to protect her own communications as follows. First, she generates a set of randomly chosen "secret keys" K1, K2, K3. Next, using these keys, she encrypts a set of messages M1, M2, M3, obtaining ciphertexts C1= K1 + M1, C2= K2+ M2, C3 =K3+M3. In implementing her scheme, she decides to choose the secret keys from the set A={1, 2, ..., 100}, and the messages from the sequence B=(a, b, c, ..., z). She is adamant that her ciphertexts should also be elements of the sequence B; to this end, she defines the "+" operator in expressions such as C1=K1+M1 to shift element M1 in B to the right by K1 positions. For example, if K1=1 and M1= b, then the corresponding ciphertext is C1= c. Similarly, if K1=2 and M1=b, then the ciphertext is C1=d, if K1=24 and M1=b, the ciphertext is C1=z, and if K1=25 and M1=b, then the ciphertext is C1=a. Requirements: 1. the file name must named as Problem3.java 2. a static method: public static String encryptMessage(int[] secretKeys, String message) Input The input consists of four rows. a. The first row is an integer n satisfying 1 <= n <= 100 specifying the number of secret keys. b. The second row is a sequence of n integers, each from the set A={1,2,..,100}, corresponding to the secret keys. c. The third row is a sequence of symbols from the set B={a,b,..,z} corresponding to the messaged to be encrypted. d. The fourth row is a special value -1 to indicate the end of the input phase. Output Output a single row of symbols from set B corresponding to each of the ciphertexts. Sample Input 3 1 2 3 bzy -1 Sample Output cbb
Explanation / Answer
/*****************************Problem3.java******************************/
import java.util.Scanner;
/**
* The Class Problem3.
*/
public class Problem3 {
/**
* Encrypt message.
*
* @param secretKeys
* the secret keys
* @param message
* the message
* @return the encrypted message
*/
public static String encryptMessage(int[] secretKeys, String message) {
// It is used for appending
StringBuilder cipherText = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
// get the ascii value of character
int ascii = message.charAt(i);
// add char ascii and corresponding secretkey
int result = ascii + secretKeys[i];
// checking total result valeu greater then 122 or 'z' or not
int c = result > 122 ? (result % 122) + 96 : result % 122;
// converting int to char
char cipherChar = (char) c;
// building ciphertext
cipherText.append(cipherChar + "");
}
// Returning the cipher text
return cipherText.toString();
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Variable declarations
int noOfSecretKey;
int[] secretKeys;
String[] keys;
String message;
System.out.println("Input");
while (true) {
noOfSecretKey = Integer.parseInt(input.nextLine());
secretKeys = new int[noOfSecretKey];
keys = input.nextLine().split(" ");
for (int i = 0; i < keys.length; i++) {
secretKeys[i] = Integer.parseInt(keys[i]);
}
message = input.nextLine();
if ("-1".equals(input.nextLine())) {
System.out.println("Output");
System.out.println(encryptMessage(secretKeys, message));
break;
} else {
System.out.println("Output");
System.out.println(encryptMessage(secretKeys, message));
}
}
// closing the scanner stream
if (input != null) {
input.close();
}
}
}
/********************************output*********************************/
Input
3
1 2 3
bzy
-1
Output
cbb
Thanks a lot. Please let me know if you have any doubts. HIT LIKE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.