Project: File Encryption Problem Description: Suppose an encrypted file was crea
ID: 640419 • Letter: P
Question
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?
Explanation / Answer
This should be right. If it's not done the way you need it to be done, the encrypting, decoding, and file opening/writing all works.
import java.util.*;
import java.io.*;
public class EncodeDecode
{
private static String encode;
private static String letters;
public static void main(String[] args) throws IOException
{
letters = "abcdefghijklmnopqrstuvwxyz";
encode = "kngcadsxbvfhjtiumylzqropwe";
System.out.println("Enter name of input file:");
Scanner scan = new Scanner(System.in);
String fileName = scan.next();
System.out.println("Enter name of output file:");
String outName = scan.next();
Scanner file = new Scanner(new File(fileName));
File fout = new File(outName);
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while(file.hasNextLine())
{
String line = file.nextLine();
String encodedLine = encode(line);
bw.write(encodedLine);
bw.newLine();
}
bw.close();
System.out.println("Enter name of encrypted file:");
String encodedFile = scan.next();
file = new Scanner(new File(encodedFile));
fout = new File(fileName);
fos = new FileOutputStream(fout);
bw = new BufferedWriter(new OutputStreamWriter(fos));
while(file.hasNextLine())
{
String line = file.nextLine();
String decodedLine = decode(line);
bw.write(decodedLine);
bw.newLine();
}
bw.close();
}
public static String encode(String str)
{
String out = "";
for(int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
int index = letters.indexOf(Character.toLowerCase(c));
boolean upper = false;
if(index == -1)
{
out = out + c;
}
else if(Character.isUpperCase(c)){
upper = true;
out = out + Character.toUpperCase(encode.charAt(index));
}
else{
out = out + encode.charAt(index);
}
}
return out;
}
public static String decode(String str)
{
String out = "";
for(int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
int index = encode.indexOf(Character.toLowerCase(c));
boolean upper = false;
if(index == -1)
{
out = out + c;
}
else if(Character.isUpperCase(c)){
upper = true;
out = out + Character.toUpperCase(letters.charAt(index));
}
else{
out = out + letters.charAt(index);
}
}
return out;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.