Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

File Encryption Filter and file Decryption Filter using C++

File Encryption Filter
File encryption is the science of writing the contents of a file in a secret code. Your
encryption C++ 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 simple
one of your own. For example, you could read the first file one character at a time,
and add 10 to the ASCII code of each character before it is written to the second file.

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

Answer:


//  File Encryption Filter
#include <iostream>
#include <fstream>
using namespace std;

// Constant for array size
const int SIZE = 81;

int main()
{
   ifstream inFile;      // Input file object
   ofstream outFile;     // Output file object
   char inName[SIZE];    // Input file name
   char outName[SIZE];   // Output file name
   char input;           // To hold a character of input

   // Get the input file name.
   cout << "Enter the input file name: ";
   cin.getline(inName, SIZE);
   
   // Open the input file.
   inFile.open(inName);

   // Test for errors.
   if (!inFile)
   {
      // There was an error so display an error
      // message and end the program.
      cout << "Error opening " << inFile << endl;
      exit(EXIT_FAILURE);
   }

   // Get the output file name.
   cout << "Enter the output file name: ";
   cin.getline(outName, SIZE);
   
   // Open the output file.
   outFile.open(outName);
   
   // Test for errors.
   if (!outFile)
   {
      // There was an error so display an error
      // message and end the program.
      cout << "Error opening " << outFile << endl;
      exit(EXIT_FAILURE);
   }
   
   // Read the contents of the input file, one
   // character at a time, encrypt it, and store
   // it in the output file.
   while (!inFile.eof())
   {
      // Get a character from inFile.
      inFile.get(input);
      
      // Encrypt the character by adding 10 to it.
      input += 10;
      
      // Write the character to outFile.
      outFile.put(input);
   }
   
   cout << "The file has been encrypted.\n";
   
   // Close both files.
   inFile.close();
   outFile.close();

   return 0;
}



file Decryption Filter


//  File Dencryption Filter
#include <iostream>
#include <fstream>
using namespace std;

// Constant for array size
const int SIZE = 81;

int main()
{
   ifstream inFile;      // Input file object
   ofstream outFile;     // Output file object
   char inName[SIZE];    // Input file name
   char outName[SIZE];   // Output file name
   char input;           // To hold a character of input

   // Get the input file name.
   cout << "Enter the input file name: ";
   cin.getline(inName, SIZE);
   
   // Open the input file.
   inFile.open(inName);

   // Test for errors.
   if (!inFile)
   {
      // There was an error so display an error
      // message and end the program.
      cout << "Error opening " << inFile << endl;
      exit(EXIT_FAILURE);
   }

   // Get the output file name.
   cout << "Enter the output file name: ";
   cin.getline(outName, SIZE);
   
   // Open the output file.
   outFile.open(outName);
   
   // Test for errors.
   if (!outFile)
   {
      // There was an error so display an error
      // message and end the program.
      cout << "Error opening " << outFile << endl;
      exit(EXIT_FAILURE);
   }
   
   // Read the contents of the input file, one
   // character at a time, decrypt it, and store
   // it in the output file.
   while (!inFile.eof())
   {
      // Get a character from inFile.
      inFile.get(input);
      
      // Dencrypt the character by subtracting
      // 10 from it.
      input -= 10;
      
      // Write the character to outFile.
      outFile.put(input);
   }
   
   cout << "The file has been decrypted.\n";
   
   // Close both files.
   inFile.close();
   outFile.close();

   return 0;
}


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!