In: Computer Science
Binary file IO
Suppose a file has been encrypted using the Caesar cipher as described above, and you know the secret key. Write a program to decrypt (or decode) the file. Your program will prompt the user to enter an input file name for the encrypted file, an output file name for the unencrypted version of the input file, and the secret key.
Create a DataInputStream for the input file and a DataOutputStreams for the output file. Next, read the input file (the encrypted file) one byte at a time (use the method readByte), decode that byte, and then write the decoded byte to the output file (use the method writeByte). Don’t forget to close the output file (or output stream) if you didn’t use try-with-resources, when you are done writing to the file. Otherwise, your output file might get corrupted
Enter an input (or encrypted) file name: lab6E.txt Enter an output (or unencrypted) file name: lab6D.txt Enter the encryption key: 5
File is decrypted successfully! Encrypted file text: N%|nqq%jshw~uy%ymnx%knqj%zxnsl%Hfjxfw%hnumjw Hfjxfw%hnumjw%nx%sf rji%fkyjw%Ozqnzx%Hfjxfw1%|mt%zxji%ny%ns%mnx%uwn{fyj%htwwjxutsijs hj Ozqnzx%Hfjxfw%|fx%f%Wtrfs%ljsjwfq%|mt%uqf~ji%f%rfotw%wtqj%ns %jxyfgqnxmnsl%ymj%Wtrfs%Jrunwj Ymnx%nx%f%y~uj%tk%xzgxynyzynts%h
numjw Ns%xzgxynyzynts%hnumjw%f%hmfwfhyjw%kwtr%ymj%uqfnsyj}y%nx%w juqfhji%|nym%fstymjw%hmfwfhyjw3
Decrypted file text: I will encrypt this file using Caesar cipher Caesar cipher is named after Julius Caesar, who used it in his private correspondence
Julius Caesar was a Roman general who played a major role in establishing the Roman Empire
This is a type of substitution cipher In substitution cipher a character from the plaintext is replaced with another character.
Screenshot
Program
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
public class CaesarCypher {
public static void main(String[] args) {
//Scanner object
Scanner sc=new
Scanner(System.in);
//Prompt for input file name
System.out.print("Enter an input
(or encrypted) file name: ");
String
inputFile=sc.nextLine();
//Prompt for output file name
System.out.print("Enter an output
(or unencrypted) file name: ");
String outFile=sc.nextLine();
//Prompt for key
System.out.print("Enter the
encryption key: ");
int key=sc.nextInt();
//Read write
try {
//Input
stream
InputStream
input = new FileInputStream(inputFile);
DataInputStream inp = new DataInputStream(input);
//Output
stream
OutputStream output= new FileOutputStream(outFile);
DataOutputStream out = new DataOutputStream(output);
//Read
data as byte
int count
= input.available();
byte[] ary
= new byte[count];
inp.read(ary);
//Decrypt each
data
for (byte bt :
ary) {
char k = decrypt((char)
bt,key);
out.write((byte)k);
}
//Close
files
out.close();
inp.close();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
System.out.println("File is
decrypted successfully!");
}
//Decrypting method
public static char decrypt(char val,int key) {
if(val!='\n') {
return (char)
(((int) val -key));
}
return val;
}
}
Output
Enter an input (or encrypted) file name: lab6E.txt
Enter an output (or unencrypted) file name: lab6D.txt
Enter the encryption key: 5
File is decrypted successfully!