Hello everyone,

I am trying to write a program that gives you 3 options:

1 for encryption
2 for decryption
3 Exits the program

If option 1, my program will ask for a password, an input file name and an output file name. My program will then read the file and mesh it with the password resulting in an encrypted file. It will write this file out.

If option 2, my program will ask for the password and the encrypted file name. Read the file and perform a decryption, printing the decryption on the screen

If option 3, the program will stop.

This is what I have so far:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

void do_again(),direction(int ans);
void option_1(),option_2(),option_3();

int main()

{
     do_again();//calls do_again function

     return 0;
}//end function

void do_again()
{
     int ans;//this will be used every time
     do
     {   //User menu
          cout << "Hello and welcome.\n\n";
          cout << "Please choose one of the following.\n";
          cout << "1: Encryption\n";
          cout << "2: Decryption\n";
          cout << "3: Exit\n";
               cin  >> ans;
               cin.ignore(80,'\n'); //this reads what the user chose
          direction(ans);//sends the answer to direction
     }while(ans!=3);//program keeps going until the user chooses exit
}//end of this function

void direction(int ans)
{
     switch (ans)//Go through the options
     {
          case 1:
               option_1();
               break;
          case 2:
               option_2();
               break;
          case 3:
               option_3();
               break;
          default://If anything other than 1,2,3 is chosen
               cout << "The option you have chosen was not listed, please try again.\n";
               break;
     }
}//end of case functions


void option_1() //If option 1 is choosen, encryption begins
{
char a[5000];
char pw[15];
char buffer[256];
int x,y;
int i,j;
int choice=4;

ifstream examplefile ("c:\\proj3cpp.txt");
 
  if (! examplefile.is_open())
  {
     cout << "Error opening file";
     exit (1);
  }

  while (! examplefile.eof() )
  {
    examplefile.getline (buffer,100);
    cout << buffer << endl;
  }                    
           
cout<<endl<<"Please enter a password: "; //User inputs a password
cin>>pw;

x=strlen(a);
y=strlen(pw);
j=0;
for (i=0; i<x; i++)
    {
        if (j>=y) j=0;
        a[i]=a[i]+pw[j];
    }
cout<<endl<<a;
cout<<endl<<"Encryption is now complete and has been saved to a text document.\n\n";
}

void option_2()// If option 2 is chosen, decryption begins
{
}

void option_3()// If option 3 is chosen, program closes.
{
}

My problems are:
- how do I get it to actually encrypt the text file I have and save the encrypted text to a new text file?
-Is there a way to make the text file =a so it goes through the encryption process?
-How can I make it so it asks the user for a file name?

Thanks in advance!

You can encrypt the text file just as you would encrypt a string.

1) Use a string to read in all data inside the ifstream
2) Use that string to encrypt it self , for example you can use
key number to encrypt like so :

cout<<"Enter a key number : ";
int num;
cin >> num;

for(int i = 0; i < stringData.size(); i++) //stringData already contains all information from txt file
{
     stringData[i] += key;
}

3) To decrypt the data, you can just reverse the process, i.e
stringData -= key.

Hello firstPerson,

Thank you for your response.

How can I do step one using my form of encryption?

Edit: Also, I receive the following errors after I did what you suggested

error C2065: 'stringData' : undeclared identifier
error C2228: left of '.size' must have class/struct/union

Do you know how to read in a file? I want to give you the code, but I the law says I can :(
Although I can help you get there.

No, researching that right now

thanks for responding

Edit: your help in getting me there would be appreciated firstPerson :)

Here is an example :

#include<iostream>
#include<fstream>

using namespace std;

int main()
{ 
	char c;

	ifstream oFile("temp.txt"); //open a file name temp. Needs to be in the same directory as the file

	if(!oFile) //check for failure, i.e does the file exist?
	{
		cout<<"Failed to open\n";
		exit(1); //exit failure 
	}
	while(oFile.get(c)) //while there is stuff to read
       {
           cout<<c; //print that stuff
       }
}

Thanks for the example, checking on that right now.

Does what you posted make the text file in a string 'c'?

No, a char can only hold 1 character. The code reads
each character in the file, and prints out the character.

You will need to use the std::string instead of char c.
And instead of using iFile.get(c), you will need to use
iFile.getLine(...) to read a whole line;

Or you can do an alternative. You can use the code I gave you.
Instead out only printing out each character in the while loop,
you can use a string variable to add each character to the string.

Thanks for replying, checking up on that now

off-topic: have you received my pm?

Edit: I'm assuming you meant oFile.get(c) instead of iFile.get(c)

i change it to while(oFile.getline(std::string)) which I think is what you meant and get "std::string' : illegal use of this type as an expression"

You can't use " while(oFile.getline(std::string)) " it that way,

I mean to declare a variable std::string data; and use the variable
data inside of getline like so :

ifstream iFile("temp.txt");
	//error check
	
	string data;

	getline(iFile,data);

	cout<<s;

are iFile's supposed to be oFile's as you put earlier?

Its only a variable name.

ofstream oFile; //oFile is a variable name
ifstream iFile; //iFile is a variable name
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.