File Encryption Filter File Encryption is the science of writing the contents of
ID: 3820647 • Letter: F
Question
File Encryption Filter File Encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, in this assignment, you will use a simple one: Read the first file one character at a time, and add ten to the character code of each character before it is written to the second file. Write a class that performs the file encryption described above and demonstrate the class in a program.
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Ceaser {
public Ceaser() {
// TODO Auto-generated constructor stub
}
public static String doEnccrypt(String str,int s)
{
StringBuffer sb=new StringBuffer();
for (int i = 0; i < str.length(); i++)
{char ch;
if(Character.isUpperCase(str.charAt(i)))
{
ch=(char)(((int)str.charAt(i)+s-65)%26+65);
}
else
{
ch=(char)(((int)str.charAt(i)+s-97)%26+97);
}
sb.append(ch);
}
return sb.toString();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Ceaser o=new Ceaser();
Scanner s = null;
PrintWriter pw=new PrintWriter(new FileWriter(new File("/home/akshay/Chegg/output.txt")));
try {
s = new Scanner(new BufferedReader(new FileReader("/home/akshay/Chegg/input.txt")));
while (s.hasNext())
{
String str = s.next();
str=o.doEnccrypt(str, 10);
pw.write(str);
char[] myChar = str.toCharArray();
// do something
}
} finally {
if (s != null) {
s.close();
pw.close();
System.out.println("Done encryption");
}
}
}
}
=======================================================================================
Output:
Done encryption
====================================================================================
input.txt
hello
====================================================================================
output.txt
rovvy
=====================================================================================
Please rate my answer that will be great help
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.