Question

Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...

Using java: Store the files in .txt format and use the readByte() and writeByte() methods.

File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works 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, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to the character code of each character before it is written to the second file.

Write a second program that decrypts the file produced by the program. The decryption program should read the contents of the coded file, restore the data to its original state, and write it to another file.

Please have readByte() and writeByte() in the answer. Every answer I've looked at on this site including one that I've received for the same question has chosen to ignore this.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Encryption.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Encyption {

   public static void main(String[] args) {
       ArrayList<Character> encrypts = new ArrayList<>();// for holding encrypted characters

       // reading from the file
       try {
           // change the name and path of file accordingly
           DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
           while (true) {
               try {
                   // reading using readByte
                   int encrypted = (int) dis.readByte() + 10;// adding 10 to char
                   encrypts.add((char) encrypted);// adding to the arraylist
               } catch (EOFException e) {
                   break;
               }
           }
           dis.close();
       } catch (IOException e) {
           System.out.println(e);
       }

       // writing to the file
       try {
           DataOutputStream dos = new DataOutputStream(new FileOutputStream("encrypt.txt"));

           for (Character ch : encrypts) {
               dos.writeByte(ch);// writing using write byte
           }

           dos.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

   }

}

Decryption.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Decrption {

   public static void main(String[] args) {
       ArrayList<Character> decrypts = new ArrayList<>();//for holding decrpted characters

       try {
           //change the name and path of the file accordingly
           DataInputStream dis = new DataInputStream(new FileInputStream("encrypt.txt"));
           while (true) {
               try {
                   //reading using readByte
                   int encrypted = (int) dis.readByte() - 10;//decrypting
                   decrypts.add((char) encrypted);
                   System.out.println(decrypts);
               } catch (EOFException e) {
                   break;
               }
           }
           dis.close();
       } catch (IOException e) {
           System.out.println(e);
       }

       try {
           //change the name and path of the file accordingly
           DataOutputStream dos = new DataOutputStream(new FileOutputStream("decrypt.txt"));

           //writing to the file
           for (Character ch : decrypts) {
               dos.writeByte(ch);//wrting using writeByte
           }

           dos.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

   }

}

Input

data.txt - Notepad File Edit Format View Help Somethings not right

Output

encrypt.txt - Notepad File Edit Format View Help ||ywoºrsxq1}*xy^* | sqrx

decrypt.txt - Notepad File Edit Format View Help Somethings not right

Add a comment
Know the answer?
Add Answer to:
Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Part 1 File encryption is the science of writing the contents of a file...

    Java Programming Part 1 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, you should come up with a...

  • File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to...

    File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to successfully complete chapter 9 programming exercise #3. File Encryption and Decryption Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = { ‘A’ : ‘%’, ‘a’ : ‘9’, ‘B’ : ‘@’, ‘b’ : ‘#’, etc . . .} Using this example, the letter A would be assigned the symbol %, the letter a would...

  • program must be written in python and have commentary. thank you 3. File Encryption and Decryption...

    program must be written in python and have commentary. thank you 3. File Encryption and Decryption Write a program that uses a dictionary to assign "codes" to each letter of the alphabet. For example: codes-{A':'8','a' :'9', 'B' : e','b' :'#,, etc ) Using this example, the letter A would be assigned the symbol %, the letter a would be assigned the number 9, the letter B would be assigned the symbol e, and so forth. The program should open a...

  • JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following...

    JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following text: MWTaahyiebt_e,c__hnyaoontuc;'e_rste_r_aynr_oert_e_gasoduoipdnp_got_shoeandtl__yty_oot_uhrree__apTdrH_oItgRhrDia_sml__eowtnotere.kr_ss_. Inside puzzle2.txt, write the following text: WTTohhriikssi__niigss,___ttbhhueet___wryrioogunh'gtr__emm_eessshssoaawggieen__gff_rrtoohmme___sswaarmmoppnllgee_22o..nttexxstt Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can...

  • using java create hash set that can for the file use a txt file: Hi my...

    using java create hash set that can for the file use a txt file: Hi my name is rick. (a) Read one word from the file. (b) Remove all non-alphanumeric characters from the word. A non-alphanumeric character is any character other than the lowercase and uppercase English letters, and the numerals 0 through 9. (c) Add the modified word to the hash set.

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

  • In Python, do a basic encryption of a text file in the following manner. The program...

    In Python, do a basic encryption of a text file in the following manner. The program encrypt.py will read in the following text file and rearrange the lines in the file randomly and save the rearranged lines of txt to another file called encrypted.txt. It will also save another file called key.txt that will contain the index of the lines that were rearranged in the encrypted file, so for example if the 4th line from the original file is now...

  • There is a file called mat2.txt in our files area under the Assignments folder. Download it...

    There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...

  • In C++ Design a format for storing graphs in files. This will store your graph into a file with t...

    in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT