Question

In: Computer Science

in java File encryption is the science of writing the contents of a file in a...

in java

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.

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

Solutions

Expert Solution

Program:

input.txt:

Sample output:

EncryptedFile.txt

DecryptedFile.txt

Code to copy:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Scanner;

public class EncryptFileData

{

     public static void main(String args[]) throwsIOException, FileNotFoundException

     {

          String inputFileName="input.txt";

          String outputFileName="EncryptedFile.txt";

          String decryptFileName="DecryptedFile.txt";

          byte codeValue;

          Scanner sc=new Scanner(System.in);

          //Accept code value from user

          System.out.println("Enter the value of code");

          codeValue=sc.nextByte();

          //Encrypt the input file

          encryptFileContent(inputFileName, codeValue,outputFileName);

          //Decrypt the input file

          decryptFileContent(outputFileName, codeValue,decryptFileName);

         

     }

     //function to encrypt the input file

     private static void encryptFileContent(String inputFileName,

              byte encodeValue, String outputFileName) throws IOException, FileNotFoundException{

          File file = new File(inputFileName);

         

          FileInputStream fin = newFileInputStream(file);

          FileOutputStream fout = newFileOutputStream(outputFileName);

         

          try{

              while(fin.available() != 0){

                   int inData = fin.read();

                   //For every character in input add code value and

                   //write result to file "EncryptedFile.txt"

                   fout.write(inData+encodeValue);

              }

          }finally{

              fin.close();

              fout.close();

          }

         

     }

     //function to decrypt the encrypted file

     private static void decryptFileContent(String inputFileName,

              byte encodeValue, String outputFileName) throws IOException, FileNotFoundException{

          File file = new File(inputFileName);

          FileInputStream fin = newFileInputStream(file);

          FileOutputStream fout = newFileOutputStream(outputFileName);

          try{

              while(fin.available() != 0){

                   int inData = fin.read();

                   //For every character in encrypted file

                   //subtract the code value from each character and

                   //write it to file "DecryptedFile.txt"

                   fout.write(inData-encodeValue);

              }

          }finally{         

              fin.close();

              fout.close();

          }

         

     }

}


Related Solutions

Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means you need to scramble the words of a file to become secure. Decryption means you need to apply some rules to an encrypted file in order to find the original file with its original content. An example of encryption is to replace each letter in a file with its consecutive letter in the alphabet. Therefore, ‘a’ is replaced by ‘b’, ‘b’ is replaced by...
1.What standard methods can you use when handling file-reading and file-writing operations? a. Java b. Fragment-based...
1.What standard methods can you use when handling file-reading and file-writing operations? a. Java b. Fragment-based methods c. onClick d. DDMS 2. What would this query return? SELECT * FROM Items WHERE OrderDate >= Date('Now') a. All items in the database. b. Nothing c. Items that were ordered yesterday d. Items that were ordered today. 3. What can you use to manage files within a desired directory and create subdirectories? a. Subdirectory objects b. Directory class c. File objects d....
The C++ program below simply copies the contents of one file to another file using standard...
The C++ program below simply copies the contents of one file to another file using standard in and standard out. Do a line-by-line conversion of the complete program into a closely equivalent C program that produces the same output given the same input. Note that the underscores are used solely to force indentation in Canvas. #include using namespace std; int main(void) { char symbol; cin.get(symbol); while (!cin.eof()) ___ { ___ cout.put(symbol); ___ cin.get(symbol); ___ } return 0; }
How can I contribute to science / the science community? It should be stream-of-conciousness writing from...
How can I contribute to science / the science community? It should be stream-of-conciousness writing from the heart. 5 minutes of reflective journaling should result in an essay of about 150-200 words.
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT