Project File Encryption Problem Description Suppose an encry
Project: File Encryption
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 = \"abcdefghijklmnopqrstuvwxyz\";
String enc = \"kngcadsxbvfhjtiumylzqropwe\";
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 file 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?
Ask for input file name/ output file name (encrypted file). The encrypt using above encode/decode.
Ask for encrypted file and decoded to original input file.
Use try and catch only. Do not throw Exception at heading of method.
*USING JAVA*
Solution
Program to read a file and encode the file to an encrypted file:
import java.io.*;
public class HelloWorld{
public static void main(String []args) throws IOException{
try{
String letters = \"abcdefghijklmnopqrstuvwxyz\";
String enc = \"kngcadsxbvfhjtiumylzqropwe\";
FileInputStream fileInput = new FileInputStream(\"OriginalFile.txt\"); //open original file
FileOutputStream fileOutput = new FileOutputStream(\"EncryptedFile.txt\"); //open encryption file
int a,b;
char ch, ch_en;
while ((a = fileInput.read()) != -1) { //read characters from input file till empty
ch = (char) a;
if(Character.isLetter(ch)){ //check if it is a letter
b = letters.indexOf(Character.toLowerCase(ch)); //find its position in letters string
if (Character.isUpperCase(ch)) //check if it\'s uppercase
ch_en = Character.toUpperCase(enc.charAt(b)); //fetch the corressponding encoded char from enc
else //and convert it to uppercase
ch_en = enc.charAt(b); //else fetch the encoded character as it is
}
else
ch_en = ch; //read all other characters as they are
fileOutput.write(ch_en); //write the encoded character in encrypted file
}
fileInput.close();
fileOutput.close();
}
catch(IOException e) {
System.out.print(\"Exception\");
}
}
}
Program to read an encrypted file and decode the file to the original file:
import java.io.*;
public class HelloWorld{
public static void main(String []args) throws IOException{
try{
String letters = \"abcdefghijklmnopqrstuvwxyz\";
String enc = \"kngcadsxbvfhjtiumylzqropwe\";
FileInputStream fileInput = new FileInputStream(\"EncryptedFile.txt\");
FileOutputStream fileOutput = new FileOutputStream(\"OriginalFile.txt\");
int a,b;
char ch_en, ch;
while ((a = fileInput.read()) != -1) {
ch_en = (char) a;
if(Character.isLetter(ch_en)){
b = enc.indexOf(Character.toLowerCase(ch_en));
if (Character.isUpperCase(ch_en))
ch = Character.toUpperCase(letters.charAt(b));
else
ch = letters.charAt(b);
}
else
ch = ch_en;
fileOutput.write(ch);
}
fileInput.close();
fileOutput.close();
}
catch(IOException e) {
System.out.print(\"Exception\");
}
}
}

