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

Problem Description: Suppose an encrypted file was created using the encoding/de

ID: 654510 • Letter: P

Question

Problem Description: Suppose an encrypted file was created using the encoding/decoding scheme. Each letter is substituted by some other letter according to a given mapping as shown below. String letters = abcdefghijklrnnopqrstuvwxyz; String ene = kngcadsxbvthjtiumylzqropwe; For example, every ?a? becomes a ?k? when encoding a text, and every ?k? becomes an ?a? when decoding. You will write a program, encode or decode a File, and then encodes or decodes the File using the mapping above. Capital letters are mapped the same way as the lower case letters above, but remain capitalized. For example. every ?A? becomes ?K? when encoding a file, and every ?K? becomes an ?A? when decoding. Numbers and other characters are not encoded and remain the same. Write a program to read a tile and encode the file to an encrypted file. And write a program to get an encrypted file and decode to original file. Your program should prompt the user to enter an input file name and an output file name What should you do? 1. Ask for input flic name! output flic name (encrypted file). The encrypt using above encode/decode. 2. Ask for encrypted file and decoded to original input tile. 3. Use try and catch only. Do not throw Exception at heading of method.

Explanation / Answer

/**
* The java program that prompts the input file that contains letters, symbols
* and text as input data. Then prompts the output file name to write the input
* file to the encoded text . Then prompt for the encoded text file name from user
* and decode the encoded file to its original file.
*
* */

//EncryptDecryptProgram.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class EncryptDecryptProgram
{

   //encode and decode strings to be used in encode and decode methods
   private static String letters="abcdefghijklmnopqrstuvwxyz";      
   private static String enc="kngcadsxbvfhjtiumylzqropwe";

   public static void main(String[] args)
   {
       Scanner scanner=new Scanner(System.in);
       //To read input and output file nams
       String inputFileName;
       String outputFileName;


       System.out.println("Enter Input File Name");
       //read file name
       inputFileName=scanner.nextLine();


       try
       {
           File file=new File(inputFileName);
           //create a scanner class to read input file
           Scanner fileReader=new Scanner(file);


           System.out.println("Enter output File Name");
           //read output file name
           outputFileName=scanner.nextLine();

           //call encodFile metod to encode file to output file
           encodeFile(fileReader,outputFileName);

           System.out.println("Encryption is successful");

           //read a file name to decode to a normal file
           System.out.println("Enter encoded file name to normal file");
           String encryptedFile=scanner.next();


           file=new File(encryptedFile);
           fileReader=new Scanner(file);

           //call decodeFile method to decode the encoded file as its argumet
           decodeFile(fileReader);

           System.out.println("Decryption of file is successful");

       }
       catch (FileNotFoundException e)
       {
           System.out.println("Input File : "+inputFileName+"does not exist");
       }              
   }

   //The method encodeFile that takes input file scanner object and output file name to
   //encode the file
   private static void encodeFile(Scanner fileReader, String outputFileName)
   {  
       try
       {
           //Create a ouput file stream to encode the input text data
           PrintWriter writer=new PrintWriter(outputFileName);  

           //Read words from file hasNext checks if there are words in file or not
           while(fileReader.hasNext())
           {
               //Read a word from file
               String word=fileReader.next();

               for (int index = 0; index < word.length(); index++)
               {
                   if(Character.isAlphabetic(word.charAt(index)))
                   {
                       //get position of character at index location in letters
                       int pos=letters.indexOf(word.charAt(index));
                       //get the corresponding letter from the enc letter string
                       char encodeCharacter=enc.charAt(pos);
                       writer.print(encodeCharacter);
                   }
                   else
                       writer.print(word.charAt(index));
               }
               //write a space for each word gap
               writer.print(" ");
           }

           //close writer output file stream
           writer.close();
       }
       catch (Exception e)
       {
           System.out.println(e.getMessage());
       }  
   }

   //The method decodeFile that takes input encoded file scanner object and
   //prompts for file name to store the encrypt file to its original file
  
   private static void decodeFile(Scanner fileReader)
   {
       Scanner scanner =new Scanner(System.in);
       System.out.println("Enter file name to original file");
       String originalFile=scanner.nextLine();

       try
       {          
           //create a PrintWriter object to write the decoded file with originalFile
           PrintWriter writer=new PrintWriter(originalFile);  

           while(fileReader.hasNext())
           {
               String word=fileReader.next();

               for (int index = 0; index < word.length(); index++)
               {
                   //checking if the character at index position is
                   if(Character.isAlphabetic(word.charAt(index)))
                   {
                       int pos=enc.indexOf(word.charAt(index));
                       char encodeCharacter=letters.charAt(pos);
                       writer.print(encodeCharacter);
                   }
                   else
                       writer.print(word.charAt(index));
               }

               writer.print(" ");
           }

           //close writer output file stream
           writer.close();
       }
       catch (Exception e)
       {
           System.out.println(e.getMessage());
       }

   }

}
-------------------------------------------------------------------------------------------------------------------------------------------


Sample input file name : sample.txt

12345 !@# hello world

------------------------------------------------------------------------------------------------------------------------------------------------------
sample output:

Enter Input File Name
sample.txt
Enter output File Name
encrypt.txt
Encryption is successful
Enter encoded file name to normal file
encrypt.txt
Enter file name to original file
decrypt.txt
Decryption of file is successful
-----------------------------------------------------------------------------------------------------------------------------------------------
output files generated by the program
encrypt.txt
12345 !@# xahhi oiyhc

decrypt.txt
12345 !@# hello world

Hope this helps you.

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