Ask Question
6 June, 01:55

Write a Java code for the following requirement.

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

The encryption program should work like a filter, reading the contents of one file, modifying the data into code, and then writing the code 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.

+1
Answers (1)
  1. 6 June, 03:26
    0
    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[]) throws IOException, 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 = new FileInputStream (file);

    FileOutputStream fout = new FileOutputStream (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 = new FileInputStream (file);

    FileOutputStream fout = new FileOutputStream (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 ();

    }

    }

    }

    Explanation:

    Given the following conditions to follow in the question;

    => "The encryption program should work like a filter, reading the contents of one file, modifying the data into code, and then writing the code contents out to a second file. "

    => "The second file will be a version of the first file, but written in a secret code."

    Thus, the Java code is given below (one can just copy and run it to give a sample output of: Enter the value of code 1 2.

    The Java code is;

    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[]) throws IOException, 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 = new FileInputStream (file);

    FileOutputStream fout = new FileOutputStream (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 = new FileInputStream (file);

    FileOutputStream fout = new FileOutputStream (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 ();

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a Java code for the following requirement. File encryption is the science of writing the contents of a file in a secret code. The ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers