Knowee
Questions
Features
Study Tools

Implement a Vigenère cipher in Java to encrypt a message of any length. A Vigenère cipher is an example of a polyalphabetic substitution cipher. To encrypt a message, you first choose a keyword to use and then trim or repeat it until it is the same length as the message you wish to encode. For example, if you choose the keyword LEMON and the message ATTACKATDAWN, you would have:Message: A T T A C K A T D A W NKey: L E M O N L E M O N L ENext, each letter of the repeated keyword corresponds to the cipher alphabet (i.e. row) used to code each letter of the message based on the Vigenère square below. The letter in the original text is then replaced by the letter in the corresponding index of the cipher alphabet. That is, for the first two letters of the message example above, 'A' is coded as 'L' i.e. the 1st index of the cipher alphabet 'L', and 'T' is coded as 'X' i.e. the 20th index of cipher alphabet 'E' etc. Repeating these steps the fully coded message is - LXFOPVEFRNHR.Vigenère square: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 ZB C D E F G H I J K L M N O P Q R S T U V W X Y Z AC D E F G H I J K L M N O P Q R S T U V W X Y Z A BD E F G H I J K L M N O P Q R S T U V W X Y Z A B CE F G H I J K L M N O P Q R S T U V W X Y Z A B C DF G H I J K L M N O P Q R S T U V W X Y Z A B C D EG H I J K L M N O P Q R S T U V W X Y Z A B C D E FH I J K L M N O P Q R S T U V W X Y Z A B C D E F GI J K L M N O P Q R S T U V W X Y Z A B C D E F G HJ K L M N O P Q R S T U V W X Y Z A B C D E F G H IK L M N O P Q R S T U V W X Y Z A B C D E F G H I JL M N O P Q R S T U V W X Y Z A B C D E F G H I J KM N O P Q R S T U V W X Y Z A B C D E F G H I J K LN O P Q R S T U V W X Y Z A B C D E F G H I J K L MO P Q R S T U V W X Y Z A B C D E F G H I J K L M NP Q R S T U V W X Y Z A B C D E F G H I J K L M N OQ R S T U V W X Y Z A B C D E F G H I J K L M N O PR S T U V W X Y Z A B C D E F G H I J K L M N O P QS T U V W X Y Z A B C D E F G H I J K L M N O P Q RT U V W X Y Z A B C D E F G H I J K L M N O P Q R SU V W X Y Z A B C D E F G H I J K L M N O P Q R S TV W X Y Z A B C D E F G H I J K L M N O P Q R S T UW X Y Z A B C D E F G H I J K L M N O P Q R S T U VX Y Z A B C D E F G H I J K L M N O P Q R S T U V WY Z A B C D E F G H I J K L M N O P Q R S T U V W XZ A B C D E F G H I J K L M N O P Q R S T U V W X YRules:Your class should be called VigenereCipher and should use the Cipher interface (available here)You should use the Vigenère square above to encrypt and decrypt the messages using a given key (both retrieved from a file).Encrypted messages should be in capital letters to obfuscate the message from anyone intercepting themDecrypted messages should also be in capital lettersIf the character is a letter of the alphabet it should be encrypted based on the aboveIf the character is not in the alphabet then it should remain unchangedHere are some test files to test your code with (used in the prechecks):encrypt_check.txtdecrypt_check.txtkey_check.txt

Question

Implement a Vigenère cipher in Java to encrypt a message of any length. A Vigenère cipher is an example of a polyalphabetic substitution cipher. To encrypt a message, you first choose a keyword to use and then trim or repeat it until it is the same length as the message you wish to encode. For example, if you choose the keyword LEMON and the message ATTACKATDAWN, you would have:Message: A T T A C K A T D A W NKey: L E M O N L E M O N L ENext, each letter of the repeated keyword corresponds to the cipher alphabet (i.e. row) used to code each letter of the message based on the Vigenère square below. The letter in the original text is then replaced by the letter in the corresponding index of the cipher alphabet. That is, for the first two letters of the message example above, 'A' is coded as 'L' i.e. the 1st index of the cipher alphabet 'L', and 'T' is coded as 'X' i.e. the 20th index of cipher alphabet 'E' etc. Repeating these steps the fully coded message is - LXFOPVEFRNHR.Vigenère square: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 ZB C D E F G H I J K L M N O P Q R S T U V W X Y Z AC D E F G H I J K L M N O P Q R S T U V W X Y Z A BD E F G H I J K L M N O P Q R S T U V W X Y Z A B CE F G H I J K L M N O P Q R S T U V W X Y Z A B C DF G H I J K L M N O P Q R S T U V W X Y Z A B C D EG H I J K L M N O P Q R S T U V W X Y Z A B C D E FH I J K L M N O P Q R S T U V W X Y Z A B C D E F GI J K L M N O P Q R S T U V W X Y Z A B C D E F G HJ K L M N O P Q R S T U V W X Y Z A B C D E F G H IK L M N O P Q R S T U V W X Y Z A B C D E F G H I JL M N O P Q R S T U V W X Y Z A B C D E F G H I J KM N O P Q R S T U V W X Y Z A B C D E F G H I J K LN O P Q R S T U V W X Y Z A B C D E F G H I J K L MO P Q R S T U V W X Y Z A B C D E F G H I J K L M NP Q R S T U V W X Y Z A B C D E F G H I J K L M N OQ R S T U V W X Y Z A B C D E F G H I J K L M N O PR S T U V W X Y Z A B C D E F G H I J K L M N O P QS T U V W X Y Z A B C D E F G H I J K L M N O P Q RT U V W X Y Z A B C D E F G H I J K L M N O P Q R SU V W X Y Z A B C D E F G H I J K L M N O P Q R S TV W X Y Z A B C D E F G H I J K L M N O P Q R S T UW X Y Z A B C D E F G H I J K L M N O P Q R S T U VX Y Z A B C D E F G H I J K L M N O P Q R S T U V WY Z A B C D E F G H I J K L M N O P Q R S T U V W XZ A B C D E F G H I J K L M N O P Q R S T U V W X YRules:Your class should be called VigenereCipher and should use the Cipher interface (available here)You should use the Vigenère square above to encrypt and decrypt the messages using a given key (both retrieved from a file).Encrypted messages should be in capital letters to obfuscate the message from anyone intercepting themDecrypted messages should also be in capital lettersIf the character is a letter of the alphabet it should be encrypted based on the aboveIf the character is not in the alphabet then it should remain unchangedHere are some test files to test your code with (used in the prechecks):encrypt_check.txtdecrypt_check.txtkey_check.txt

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The task requires implementing a Vigenère cipher in Java. Here's a simple implementation:

public class VigenereCipher {
    private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String encrypt(String text, String key) {
        text = text.toUpperCase();
        key = key.toUpperCase();
        String res = "";

        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, String key) {
        text = text.toUpperCase();
        key = key.toUpperCase();
        String res = "";

        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;
    }
}

This code defines a class VigenereCipher with two static methods: encrypt and decrypt. Both methods iterate over the input text and apply the Vigenère cipher algorithm to each character. If the character is not a letter, it is skipped. The key is repeated or trimmed to match the length of the text.

Please note that this is a simple implementation and does not include reading from or writing to files, and does not implement the Cipher interface as mentioned in the task. You would need to add those parts according to your specific requirements.

This problem has been solved

Similar Questions

You are given a string s and an integer k. Encrypt the string using the following algorithm:For each character c in s, replace c with the kth character after c in the string (in a cyclic manner).Return the encrypted string. Example 1:Input: s = "dart", k = 3Output: "tdar"Explanation:For i = 0, the 3rd character after 'd' is 't'.For i = 1, the 3rd character after 'a' is 'd'.For i = 2, the 3rd character after 'r' is 'a'.For i = 3, the 3rd character after 't' is 'r'.Example 2:Input: s = "aaa", k = 1Output: "aaa"Explanation:As all the characters are the same, the encrypted string will also be the same. Constraints:1 <= s.length <= 1001 <= k <= 104s consists only of lowercase English letters.

Make a function that encrypts a given input with these steps:Input: "apple"Step 1: Reverse the input: "elppa"Step 2: Replace all vowels using the following chart:a => 0e => 1i => 2o => 2u => 3# "1lpp0"Step 3: Add "vit" to the end of the word: "1lpp0vit"Output: "1lpp0vit"NotesAll inputs are strings, no uppercases and all output must be strings.i/p:bananao/p:0n0n0bvit

Alice and Bob want to communicate securely over an insecure channel where Evecould be listening. To ensure their message remains confidential, they decide to usea simple substitution cipher for encryption and decryption, where each letter in thealphabet is shifted by a fixed number of places.For this exercise, use a shift of 3 places to the right (a Caesar Cipher). For example,'A' becomes 'D', 'B' becomes 'E', 'Z' becomes 'C', and so on. Spaces and punctuationare not encoded; they remain unchanged.Alice wants to send the following message to Bob: "HELLO WORLD".a. Encrypt Alice's message using the specified shift. Provide the encrypted text.………………………………………………………………………………………………[1]b. Discuss the security of using the Caesar Cipher for encrypting messages.Consider scenarios where Eve knows or does not know the method ofencryption.……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………[1]

Write a program for Hill Cipher Method (Use 2 x 2 Matrix and 3 x 3Matrix)Note: (Optionsshould be given to user for Key matrix. it is either 2 x 2 matrix or 3 x 3 matrix)Hint: https://www.javatpoint.com/hill-cipher-program-in-javaBelow change is required in your submission:1. You have to take input as key matrix2. Entered matrix is square matrix or not, if not pls re-enter the matrix3.  Take input of plaintext is also in square matrix form for ex if 2 X 2 matrix is entered than plaintext should be 4 character, if matrix is 3X3 than plaintext should be 9 character like wise...Note: use padding if necessary

Write a program to perform encryption and decryption using wrapper class Encryption phase with shift n = En (x) = (x+n)mod 26Decryption phase with shift n = Dn (x) = (x-n)mod 26ExamplesText : ABCDEFGHIJKLMNOPQRSTUVWXYZShift : 23Cipher : XYZABCDEFGHIJKLMNOPQRSTUVWinput-output format Text : ATTACKATONCE(input)Shift : 4(input)Cipher : EXXEGOEXSRGI(output)ATTACKATONCE

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.