JAVA PROGRAMMING : Alice recently read the news about Edward Snowden, and learne
ID: 3571178 • Letter: J
Question
JAVA PROGRAMMING : 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
import java.io.*;
import java.util.Scanner;
class Problem2
{
public static void main(String[] args)
{
int n,num;
char alpha;
Scanner read=new Scanner(System.in);
System.out.println("enter no. of secret keys u want...");
n=read.nextInt();//no. of secret keys
read.nextLine();//to print new line
int arr[]=new int[n];
System.out.println("Enter the Integers");//Each integer specifies the number of times the corresponding //message character should incremented
for(int i=0;i<n;i++)
{
arr[i]=read.nextInt();
if(arr[i]<1||arr[i]>100)
{
System.out.println("Invalid inputs...try again");
System.exit(0);
}
if(i==n-1)
read.nextLine();
}
char msg[]=new char[n];
System.out.println("Enter the message characters"); // actual message charaters
for(int i=0;i<n;i++)
{
msg[i]=read.next().charAt(0);
}
for(int i=0;i<n;i++)
{
num=arr[i];
alpha=msg[i];
while(num!=0)
{
if(msg[i]!='z')
{
msg[i]++;
num--;
}
else
{
msg[i]='a';
num--;
}
}
}
for(int i=0;i<n;i++) //output secret message
System.out.print(msg[i]);
}
}
sample output:
enter no. of secret keys u want...
3
Enter the Integers
1 2 3
Enter the message characters
b z y
cbb
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.