Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working in the implementation of the vigenere cipher for encryption and dec

ID: 3832711 • Letter: I

Question

I am working in the implementation of the vigenere cipher for encryption and decryption. I am using netbeans and java lenguage.

It will then ask for the input file, the output file, and finally it will have the user type the encryption key. If encrypting, the ciphertext should be written to the output file. If decrypting, the decrypted plaintext should be written to the output file.

Example:

Here is the code I already have. It just need some changes.

import java.util.Scanner;


public class Vigenere {
  
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) {

Scanner sc = new Scanner(System.in);
System.out.print("Input file name: ");
String message = sc.nextLine();
System.out.println("Please enter the encryption key:");
String key = sc.nextLine();
String encodedMsg = encrypt(message, key);
System.out.println("The encoded message is:");
System.out.println(encodedMsg);
String decodedMsg = decrypt(encodedMsg, key);
System.out.println("The decoded message is:");
System.out.println(decodedMsg);   

}

}

Sample output Below is an idea of what kind of output your program should have Are you encrypting or decrypting? 1 for encrypt, 2 for decrypt: 1 Input file name wordsSimple.txt output file name wordsSimpleEncoded.txt Please enter the encryption key: WAFFLES Encryption complete

Explanation / Answer

Hi, I have added the code to read from file and write to file.

Please let me know in case of any issue.

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Vigenere {

   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) throws FileNotFoundException {

       Scanner sc = new Scanner(System.in);

      

       System.out.println("Please enter the encryption/decreption key:");

       String key = sc.nextLine();

      

       System.out.print("Input file name: ");

       String inputFile = sc.next();

       System.out.print("Output file name: ");

       String outputFile = sc.next();

      

       System.out.println("Do you want to encrypt/decrypt(E/D) ? ");

       String op = sc.next().trim();

      

       Scanner fileScanner = new Scanner(new File(inputFile));

       PrintWriter out = new PrintWriter(new File(outputFile));

      

       while(fileScanner.hasNextLine()){

           String message = fileScanner.nextLine();

           String encodedMsg;

           if(op.equalsIgnoreCase("e"))

               encodedMsg= encrypt(message, key);

           else

               encodedMsg= decrypt(message, key);

          

           if(op.equalsIgnoreCase("e"))

               System.out.println("The encoded message is:");

           else

               System.out.println("The decoded message is:");

          

           System.out.println(encodedMsg);

           out.write(encodedMsg+" "); // writing to file

       }

      

       fileScanner.close();

       out.close();

       sc.close();

      

   }

}

/*

Sample run:

Please enter the encryption/decreption key:

pakka

Input file name: test.txt

Output file name: decy.txt

Do you want to encrypt/decrypt(E/D) ?

E

The encoded message is:

DQDECDIQDANKJXKNIQDTZXJEYXGDYTVZEAKIUHBOIKQDJKGHIKONHEABNJXKOUAUTXNQHHTIXQX

The encoded message is:

WAJJNVZIUKHYKDCDKBTEAUHIZMODWYNVQDTDTWCAGZYFRZRYDKNOIJNZXUQCVEJEPPYJWKOZXUTZDJ

The encoded message is:

XNQHGXZUHLMUCJNZODFAOHKVLZXYDPVBQEXNNEKRYOZKYOVBKMVCQOCDZXJNZYSQTIKHSRVYI

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote