Background If two people wish to communicate privately over an insecure medium,
ID: 3583422 • Letter: B
Question
Background
If two people wish to communicate privately over an insecure medium, the sender can use a shared
secret, called a cipher, to encrypt a message. The recipient can use the same cipher to decrypt it
to reveal the original message.
One of the oldest (and least complicated) ciphers is known as the Caesar cipher (after Julius Caesar).
To use the Caesar cipher, the two people must agree upon a shift value. Each letter in the
original message will be shifted by this value. So, if they agree upon 7, the letter “a” would become
“h”.
Here is an example of a quote encrypted with a Caesar cipher:
Y jxyda secfkjuh isyudsu, ro qdt bqhwu, yi ijybb ijksa yd jxu Cetuhd qwu.
Task
Your task is to create an Android app that will allow the user to enter a message and a shift value.
The app should then display the encrypted message. Create a main activity with the following
three widgets (views):
• an EditText field to allow the user to enter the text to be encrypted
• an EditText field to allow the user to enter the cipher value (an integer between 0 and 25)
– Alternatively, you can use a SeekBar to get the integer value.
• a Button to allow the user to submit the information
When the user presses the submit button, the encrypted message should appear in a new activity.
Feel free to design the two activity layouts any way that you see fit.
Extra Credit
Imagine you are an outsider (perhaps an adversary) who would like to know the content of the
encrypted messages. You know that the messages are encrypted using a Caesar cipher, but do
not know the key (shift value). Write a Java program to decrypt the text encrypted with a Caesar
cipher (such as the quote above). The program does not need to be an Android app
Explanation / Answer
import java.util.Scanner;
public class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText = "";
for (int i = 0; i < plainText.length(); i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println(encrypt(message, 3));
System.out.println(decrypt(encrypt(message, 3), 3));
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.