×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Create Vigènere Cipher in Java Assignment Solution

July 15, 2024
Alex Thompson
Alex Thompson
🇺🇸 United States
Java
Alex Thompson is a skilled Java Assignment Expert with 8 years of experience. He completed his Master's degree at Stanford University, USA.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use the REPL (Read-Eval-Print Loop) to test your code interactively, and practice writing concise, readable expressions to harness Scala’s powerful syntax effectively.
News
In 2024, JavaScript's Redux saw updates for efficient state management, and Tailwind CSS gained traction for responsive design. Meanwhile, C++ libraries like Boost.

Instructions

Objective

Write a java assignment program to create Vigènere Cipher in java language. The Vigènere Cipher is a classical method of encrypting alphabetic text by using a simple form of polyalphabetic substitution. It uses a keyword to determine the shift value for each letter in the plaintext. The encrypted text produced by the Vigènere Cipher is more secure than the traditional Caesar cipher as it employs multiple shift values. In this java assignment, you will implement the Vigènere Cipher algorithm, allowing users to input a keyword and a message for encryption. Your program should then produce the encrypted text as the output. This java assignment will help you practice string manipulation, loops, and conditional statements in the context of encryption algorithms.

Requirements and Specifications

program to create Vigènere Cipher in java

Source Code

/*============================================================================= | Assignment: pa01 - Encrypting a plaintext file using the Vigenere cipher | | Author: Your name here | Language: Java | | To Compile: javac pa01.java | | To Execute: java -> java pa01 kX.txt pX.txt | where kX.txt is the keytext file | and pX.txt is plaintext file | | Note: All input files are simple 8 bit ASCII input | | Class: CIS3360 - Security in Computing - Summer 2021 | Instructor: McAlpin | Due Date: per assignment | +=============================================================================*/ import java.io.BufferedReader; import java.io.FileReader; public class pa01 { public static void main(String[] args) throws Exception { // check number of argumnts if(args.length < 2) { System.out.println("You must provide at least two arguments."); System.exit(1); } // Get name of file with encryption key String file1 = args[0]; // Get name of file to encrypt String file2 = args[1]; // Read files String keyword = ""; String plain_text = ""; int i; try { BufferedReader reader = new BufferedReader(new FileReader(file1)); while((i = reader.read()) != -1) { char ch = (char)i; // convert to lowercase ch = Character.toLowerCase(ch); // only use characters that are in the alphabet a-z if((int)ch >= (int)('a') && (int)ch <= (int)('z')) keyword += ch; } reader.close(); } catch(Exception e) { System.out.println("Could not load keyword file " + file1 + "."); e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new FileReader(file2)); while((i = reader.read()) != -1) plain_text += (char)i; reader.close(); } catch(Exception e) { System.out.println("Could not load text file " + file2 + "."); e.printStackTrace(); } char[][] table = create_table(); String key = generate_key(keyword, plain_text); String encrypted = encrypt(plain_text, key, table); String decrypted = decrypt(encrypted, key, table); System.out.println("Encrypted text: " + encrypted); System.out.println(""); System.out.println("Original/Decrypted text: " + decrypted); } static String encrypt(String text, String key, char[][] table) { // index of the starting letter int start = 'a'; // cpy the text and convert to lower String text_copy = text.toLowerCase(); // variable to store the encrypted text String encrypted = ""; int row, col; char encrypted_ch; int i = 0; for(char ch: text_copy.toCharArray()) { // check if the character is in the alphabet if((int)ch < (int)('a') || (int)ch > (int)('z')) { encrypted += ch; continue; } // get index of row in table row = ch - start; col = key.charAt(i) - start; encrypted_ch = table[row][col]; encrypted += encrypted_ch; i++; } return encrypted; } static String decrypt(String text, String key, char[][] table) { // index of the starting letter int start = 'a'; // cpy the text and convert to lower String text_copy = text.toLowerCase(); // variable to store the decrypted text String decrypted = ""; int row, col = 0; char decrypted_ch; int i = 0; for(char ch: text_copy.toCharArray()) { // check if the character is in the alphabet if((int)ch < (int)('a') || (int)ch > (int)('z')) { decrypted += ch; continue; } // get index of row in table row = key.charAt(i) - start; // now, in this row, check in which column the letter of the encrypted text appears for(int j = 0; j < 26; j++) { if(table[row][j] == ch) { col = j; break; } } decrypted_ch = (char)(col+start); decrypted += decrypted_ch; i++; } return decrypted; } static String generate_key(String keyword, String text) { // get length of text to encrypt int N = text.length(); // Generate the key by repeating keyword until its length is higher than N int keyword_N = keyword.length(); String key = keyword; while(keyword_N < N) { key += keyword; keyword_N = key.length(); } // Now, cut the given key so the length is equal to N key = key.substring(0, N); return key.toLowerCase(); } static char[][] create_table() { /* This function will create the Vigenere Table */ char[][] table = new char[26][26]; int start = 'a'; int ch = start; for(int i = 0; i < 26; i++) { ch = start+i; for(int j = 0; j < 26; j++) { table[i][j] = (char)ch; ch++; if(ch > (int)('z')) ch = 'a'; } } return table; } } /*============================================================================= | I [your name] ([your NID]) affirm that this program is | entirely my own work and that I have neither developed my code together with | any another person, nor copied any code from any other person, nor permitted | my code to be copied or otherwise used by any other person, nor have I | copied, modified, or otherwise used programs created by others. I acknowledge | that any violation of the above terms will be treated as academic dishonesty. +=============================================================================*/

Related Samples

Explore our collection of free Java assignment samples designed to provide clarity and insight. These samples offer detailed solutions and practical examples, helping you grasp Java concepts effectively. Dive into these resources to gain a deeper understanding of Java programming and excel in your assignments effortlessly.