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

File Encryption Filter File Encryption is the science of writing the contents of

ID: 3820788 • 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.*;

public class Encryption
{
public static void main(String[] args) throws IOException
{
encrypt(new File("test.txt"), new File("test2.txt"));
decrypt(new File("test2.txt"), new File("test.txt"));
}
public static void encrypt(File input, File output) throws IOException
{
// open I/O streams
DataInputStream in = new DataInputStream(new FileInputStream(input));
DataOutputStream out = new DataOutputStream(new FileOutputStream(output));

// read characters, encrypt them, and output to file
char c;

try
{
// read until EOF character is found
while((c=in.readChar())!=0)
{
out.writeChar((char)(c+10));
}
}
catch(Exception ex)
{
// stop reading if an exception is thrown
}

// close I/O streams
in.close();
out.close();

}
public static void decrypt(File input, File output) throws IOException
{
// open I/O streams
DataInputStream in = new DataInputStream(new FileInputStream(input));
DataOutputStream out = new DataOutputStream(new FileOutputStream(output));

// read characters, encrypt them, and output to file
char c;

try
{
// EOF character is found
while((c=in.readChar())!=0)
{
out.writeChar((char)(c-10));
}
}
catch(Exception ex)
{
// an exception is thrown
}

// close I/O streams
in.close();
out.close();

}
}

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