JAVA PROGRAMMING : ALSO NEED TESTER CODES - Alice recently read the news about E
ID: 3573869 • Letter: J
Question
JAVA PROGRAMMING : ALSO NEED TESTER CODES - 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 Problem2.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
Problem2.java
public class Problem2 {
public static String encryptMessage(int[] secretKeys, String message){
int msgLen = message.length();
String encrptText = "";
for(int i = 0;i<msgLen;i++){
int charValue = message.charAt(i)+secretKeys[i];
while(charValue<97 || charValue>122){
charValue -= 26;
}
encrptText += (char) charValue;
}
return encrptText;
}
}
TestClass.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestClass {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
int[] secretKeys = null;
String plainText= null;;
try {
System.out.print("Enter the value of n");
n = Integer.parseInt(br.readLine());
secretKeys = new int[n];
System.out.println("Enter the secret keys:");
String[] strKeys = br.readLine().split(" ");
for(int i = 0; i<n;i++){
secretKeys[i] = Integer.parseInt(strKeys[i]);
}
System.out.print("Enter the message:");
plainText = br.readLine();
System.out.print("Enter end of input delimeter:");
while(true){
String delimeter = br.readLine();
if(Integer.parseInt(delimeter) == -1){
break;
}
System.out.println("Please enter the valid end of input delimeter");
}
} catch (NumberFormatException e) {
System.out.println("Invalid Value");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Pleaes enter correct values");
e.printStackTrace();
}
System.out.println("Encrypted Output:");
System.out.println(Problem2.encryptMessage(secretKeys, plainText));
}
}
Note: We can remove system.out statement while taking input if we don't want
Test results:
Enter the value of n:5
Enter the secret keys:
1 2 3 1 52
Enter the message:abczb
Enter end of input delimeter:-1
Encrypted Output:
bdfab
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.