Problem: Your task is to write a Java program that encrypts or decrypts a line o
ID: 3680941 • Letter: P
Question
Problem: Your task is to write a Java program that encrypts or decrypts a line of text using the Vigenère cipher. You may already be familiar with the Caesar cipher, one of the earliest known and simplest encryption schemes. Unfortunately, the Caesar cipher is easy to crack because it encrypts text by simply shifting letters by a fixed amount. The so-called Vigenère cipher overcomes this problem by encoding a letter into one of several cipher letters, depending on its position in the input text. Choose a keyword, for example TIGER. Then encode the first letter of the input text like this: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z T U V W X Y Z A B C D E F G H I J K L M N O P Q R S That is, the encoded alphabet is just the regular alphabet shifted to start at T, the first letter of the keyword TIGER. The second letter is encoded according to this map: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z I J K L M N O P Q R S T U V W X Y Z A B C D E F G H The third letter is encoded according to this map: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z G H I J K L M N O P Q R S T U V W X Y Z A B C D E F The fourth letter is encoded according to this map: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z E F G H I J K L M N O P Q R S T U V W X Y Z A B C D The fifth letter is encoded according to this map: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z R S T U V W X Y Z A B C D E F G H I J K L M N O P Q Because the key is only five letters long, the sixth letter of the input text is encrypted the same way as the first. Run-time Requirements and Guidelines: The program begins by asking the user to choose between encrypting or decrypting a line of text. After obtaining the user’s choice (which must be validated), the program prompts the user to enter an alphabetic keyword (which must be validated). Once the keyword is validated, the program prompts the user to enter a line of text (which must be validated). The keyword/text validation is necessary because the keyword may consist only of letters, and the line of text may consist only of letters and spaces. (The Vigenère cipher only handles letters. You may assume case-insensitivity for both keyword and text.) The program continues to loop until the user responds “N” or “n” (i.e., no) to the program’s request to encrypt/decrypt another line of text. As usual, your program output should resemble the sample runs at the end of this document. Implementation Requirements and Guidelines: • Your program must support keywords consisting of from five to fifteen letters. (Note: The minimum/maximum number of letters is a guideline. Validating it in your program is optional.) • Your program must skip over spaces when encrypting/decrypting a line of text. It should preserve them, however. • Your program must consist of at least two classes, a driver class and an exception class. • Your program must implement a custom Exception class to handle invalid keyword and/or text input. I recommend using the DivideByZeroException.java class as a model. • Your program must use methods to carry out the primary tasks of the program. Some possible methods include: o public String encryptString(); // Encrypts a line of text o public String decryptString(); // Decrypts a line of text o public boolean isValidKey(String userKey); // Checks if key is valid etc. Important Note: Please minimize your use of static methods and “spaghetti code” (Google it.) unless you want to lose points. References: Savitch: Chapter 5 (Wrapper Classes); Chapter 9 (Exception Handling)
Explanation / Answer
public class VigenereCipher
{
public static String encrypt(String text, final String key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static String decrypt(String text, final String key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static void main(String[] args)
{
String key = "VIGENERECIPHER";
String message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
String encryptedMsg = encrypt(message, key);
System.out.println("String: " + message);
System.out.println("Encrypted message: " + encryptedMsg);
System.out.println("Decrypted message: " + decrypt(encryptedMsg, key));
}
}
note- by using or modifying the above code the given vigenere cipher problem can be solved.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.