Question

In: Computer Science

Binary file IO Suppose a file has been encrypted using the Caesar cipher as described above,...

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.

Solutions

Expert Solution

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!


Related Solutions

A message has been encrypted by Bob using row transposition cipher to give the following ciphertext:...
A message has been encrypted by Bob using row transposition cipher to give the following ciphertext: TTNA APTM TSUO AODW COIX KNLY PETZ However when Bob shared the key with Alice, it got copied multiple times and looks like: …. 6 7 4 3 1 2 5 6 7 4 3 1 2 5 6 7 4 3 1 2 5 6 7 4 3 1 2 5 6 7 4 3 1 2 …… You see a series of...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
1. Monoalphabetic substitution (using the Caesar Cipher tool right) Encipher (convert plaintext into ciphertext): meal times...
1. Monoalphabetic substitution (using the Caesar Cipher tool right) Encipher (convert plaintext into ciphertext): meal times  Decipher (convert ciphertext in to plaintext):  JR PHDQ JUHHQ 2. Polyalphabetic substitution (using the Vigenere Square in the lecture slide) Encipher: fall  Decipher:  VPX TWOKM
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
Suppose an attacker obtained 128 bits of ciphertext that were encrypted using an encryption algorithm whose...
Suppose an attacker obtained 128 bits of ciphertext that were encrypted using an encryption algorithm whose keys are known to be 128 bits long. How effective is an exhaustive key search if the attacker does not know the encryption algorithm used? Provide an example of when data integrity is more important than data confidentiality? Suppose you have been asked at your new job to recommend the strength of encryption needed to protect the contents of a database. Draw up a...
Suppose Alice is using a block cipher to send the message THE ORDER IS KARL, ANDY,...
Suppose Alice is using a block cipher to send the message THE ORDER IS KARL, ANDY, FRED AND IAN. IAN AND ANDY HAVE LEFT. to Bob. Assume that the block cipher is used in ECB mode, the English is divided into plaintext blocks of 2 letters (ignore spaces and punctuation) the ciphertext blocks are denoted C1,C2,...,C23 (a) Write down the 23 plaintext blocks. (b) Will any of the ciphertext blocks be repeated? If so, which ones? (c) Suppose an attacker...
Decipher the following. They have been enciphered using a Shift Cipher with the given key. OMFMOAYNE,...
Decipher the following. They have been enciphered using a Shift Cipher with the given key. OMFMOAYNE, key = 12. TJNXWNVM, key = 19. LHAXEWJ, key = 22. DBSLEXO, key = 10. GDBPC ATVXDC, key =15. Decipher the following. They have been enciphered using a Shift Cipher with the given key. QSBHIFWCB, key = O. CQN ADKRLXW ARENA, key = J. ZKBYQD SQBUDTQH, key = Q. VTXLTKBTG LXVMBHG, key = T.
Decipher the following names. They have been enciphered using a Shift cipher with the given key....
Decipher the following names. They have been enciphered using a Shift cipher with the given key. GULWOM UOLYFCOM, key = U. IRGAJOAY, key = G. TYESBUJYQD, key = Q. HFXXNZX, key = F.   Encipher the following words using a Shift Cipher with the given key. Appian Way, key = F. Punic Wars, key = B. Carthage, key = N. Gladiator, key = W.
The following mechanism has been proposed for this catalyzed reaction: H2O2 + I– → IO– +...
The following mechanism has been proposed for this catalyzed reaction: H2O2 + I– → IO– + H2O H2O2 + IO– → I– + H2O + O2 a) Write the formula for any intermediate. b) Write the formula for any catalyst. c) Show how the steps in the mechanism add to the decomposition of H2O2.
The Scientific Method has been traditionally described as:
The Scientific Method has been traditionally described as: Stating a hypothesis; Collecting data using some sort of sampling plan; Rejecting or not rejecting the hypothesis. How would you describe the process of moving from a null hypothesis to a conclusion about such a hypothesis? What do you think is the advantage of using the scientific method?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT