/************************************

* Name: ASCII file encryption *

* Author: Musawir Ali *

************************************/

#include 

#include 

#include 

using namespace std;

void main()

    {

     ifstream fin;

     ofstream fout;

     fin.open("data.txt");

     fout.open("encrypted.txt");

     if(fin.fail())

         {

         cout << "File data.txt not found!" << endl;

         exit(1);

         }

        

         string key;

         cout << "Encrypting File: data.txt" << endl;

         cout << "Enter encryption key: ";

         cin >> key;

         char next;

         fin.get(next);

         int i=0;

         while(! fin.eof())

             {

             fout << char(int(next) + int(key[i]));

             if(i = key.size() -1)

             i = 0;

             fin.get(next);

             }

             cout << "Encryption complete! The encrypted file is stored as: encrypted.txt" << endl;

             fin.close();

             fout.close();

             char choice = 'n';

             cout << "\nDecrypt file? : ";

             cin >> choice;

             if(choice == 'Y' || choice == 'y')

                 {

                 cout << "\nDecrypting File: encrypted.txt" << endl;

                 cout << "Enter key that you used for encryption: ";

                 cin >> key;

                 ifstream ifin;

                 ofstream ifout;

                 ifin.open("encrypted.txt");

                 ifout.open("decrypted.txt");

                

                 ifin.get(next);

                 i=0;

                 while(! ifin.eof())

                     {

                     ifout.put(char(int(next) - int(key[i])));

                     if(i = key.size() -1)

                     i = 0;

                     ifin.get(next);

                     }

                     ifin.close();

                     ifout.close();

                     cout << "File decrypted! It is stored as: decrypted.txt" << endl;

                     cout << "Node: if the decryption key provided does not match the encryption key, then the decrypted file would not make sense." << endl;

                    

                     }

                     cout << "\nEnd of demonstration." << endl;

                }