Question

Vigenère Cipher. In the Vigenère Cipher, we use a special keyword to encrypt a message. We...

Vigenère Cipher. In the Vigenère Cipher, we use a special keyword to encrypt a message. We represent each letter of the alphabet as a number from 0-25. In order, we add each letter from the keyword to the message and mod by 26. If the keyword is shorter than the message, we simply repeat the keyword. For example, lets say that the message is HOWSTUFFWORKS and the keyword is CIPHER. The following table shows how we can find the final version of the ciphertext, borrowed from How Stuff Works. Plain Text: HOWSTUFFWORKS Keyword: CIPHER Cipher Text: JWLZXLHNLVVBU Your mission is to write a program that will read a message and a keyword and apply the Vigenère Cipher to it.

Your main() method should prompt the user for some plain text. Then, it should prompt the user for a keyword. You may assume that both the plain text and the keyword contain no spaces and are only made up of upper case letters. To perform the encryption, you need to loop through the plain text and add each letter from the keyword to it, mod 26. Unfortunately, neither the letters in the plain text nor in the keyword have values between 0 and 25. You will have to convert those letter values so that they are between 0 and 25, then do the add, then do the mod, then convert them back to the proper ASCII values

Must be in Java.

Homework Answers

Answer #1

The following code can be used to implement Vignere Cipher in Java.

// Java code to implement Vigenere Cipher 
import java.util.*;
class Vignere 
{ 

// This function generates the key in 
// a cyclic manner until it's length isi'nt 
// equal to the length of original text 
static String generateKey(String str, String key) 
{ 
        int x = str.length(); 

        for (int i = 0; ; i++) 
        { 
                if (x == i) 
                        i = 0; 
                if (key.length() == str.length()) 
                        break; 
                key+=(key.charAt(i)); 
        } 
        return key; 
} 

// This function returns the encrypted text 
// generated with the help of the key 
static String cipherText(String str, String key) 
{ 
        String cipher_text=""; 

        for (int i = 0; i < str.length(); i++) 
        { 
                // converting in range 0-25 
                int x = (str.charAt(i) + key.charAt(i)) %26; 

                // convert into alphabets(ASCII) 
                x += 'A'; 

                cipher_text+=(char)(x); 
        } 
        return cipher_text; 
} 

// Driver code 
public static void main(String[] args) 
{ 
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter the message to be encrypted : ");
        String str = sc.nextLine(); 
        
        System.out.print("Enter the keyword : ");
        String keyword = sc.nextLine(); 

        String key = generateKey(str, keyword); 
        String cipher_text = cipherText(str, key); 

        System.out.println("Ciphertext : " + cipher_text + "\n"); 

        System.out.println("Original Text : " + str); 
        } 
} 

Please upvote the answer if you find it helpful.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
clc; clear; % OneTimePad Cipher Encryption % Input: plainText % key, for encryption key as long...
clc; clear; % OneTimePad Cipher Encryption % Input: plainText % key, for encryption key as long as plain text % Output: cipherText % plainText=upper('Hello') key=upper('XMCKL'); % process the plain text and key k1=double(key)-65; p1=double(plainText)-65; % Encrypt C1= p1+k1; C1= mod(C1,26)+65; %diaplay the cipher text CipherText=char(C1) Does anyone no how I would go about making this encryption encrypt and decrypt using both uppercase and lower case letters?
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
In cryptography, we use encryption ciphers to encode (“encrypt”) a plaintext into a ciphertext. In theory...
In cryptography, we use encryption ciphers to encode (“encrypt”) a plaintext into a ciphertext. In theory the ciphertext is only readable by someone who knows how the plaintext was encrypted. If they have this information they can “decrypt” and recover the plaintext. There are some very simple ciphers, known as “substitution ciphers”. They’re so simple that they’re not used any more because they are easy to break... but they are great for us because they are easy to program! First...
Using One-Time Pad Encryption, encrypt the message "JELLO" with one time pad key "YMCKM". Use the...
Using One-Time Pad Encryption, encrypt the message "JELLO" with one time pad key "YMCKM". Use the resultant cipher (from above step) and the one time pad key (pre-shared "YMCKM") to get the plain text.
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES:...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES: Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES:...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES: Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put...
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the...
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the letters in a message are replaced by the letters of a “shifted” alphabet. A “Caesar Cipher” encodes a message by shifting each letter in a message by a constant amount of k. If k is 5, A becomes F, B becomes G, C becomes H and so on. Let's use Queue to encode and decode the encrypt message. Set k as -5 and decipher...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal In the first lab, we will develop a system program (called cipher-machine) which can encrypt or decrypt a code using C language. It must be an errorless program, in which user repeatedly executes pre-defined commands and quits when he or she wants to exit. For C beginners, this project will be a good initiator to learn a new programming language. Students who already know...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...