Encrypting A File Into An Output File

I an a beginner, and I need to make a C++ program that will go through the first step of encrypting an input file and writing the encrypted contents to an output file. But, every time I try to run it, it says error. This is my code, please help. I have 0 idea what I'm doing.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string line;
fstream input("October20.txt");

fstream output("EncryptOct20.txt");

if (input.is_open())
{
cout << "Encryption Level 1";
while (getline(input, line))
{
cout << "Opening input file....";
cout << "Encrypting....";
cout << line << endl;
cout << "Encryption complete.";
}
input.close();
}

return 0;

}
An OLD encrypt/decrypt program I found on my HD in my development files from years ago:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// A text data encryption program

#include <iostream>
#include <fstream>
#include <string>

class Encryption
{
   std::fstream file1; // source file
   std::fstream file2; // destination file

public:
   Encryption(std::string filename1, std::string filename2);

   // encrypts the input file
   void Encrypt();

   // closes both of the files
   void close();
};

int main()
{
   std::cout << "Welcome to the S.A.S encryption program.\n";
   Encryption delta("../input.txt", "../output1.txt");
   delta.Encrypt();
   delta.close();

   Encryption gamma("../output1.txt", "../output2.txt");
   gamma.Encrypt();
   delta.close();
}

Encryption::Encryption(std::string filename1, std::string filename2)
{
   file1.open(filename1, std::ios::in | std::ios::out | std::ios::binary);
   file2.open(filename2, std::ios::out | std::ios::binary);
}

void Encryption::Encrypt()
{
   char currentByte;
   bool currentBit;

   int index = 0;

   // sets the pointers to the beginning of the file
   file1.seekg(0, std::ios::beg);
   file2.seekp(0, std::ios::beg);

   // reads the first value
   file1.read(&currentByte, 1);
   while (file1.good())
   {
      // loop for four bits
      for (int c = 0; c < 4; c++)
      {
         // finds out if the first bit is a one
         currentBit = static_cast<bool> ((unsigned char) currentByte / 128);

         // shifts the byte over
         currentByte <<= 1;

         // if the first bit was a one then we add it to the end
         if (currentBit)
         {
            currentByte += 1;
         }
      }

      // writes the character
      file2.write(&currentByte, 1);

      // increments the pointer
      file1.seekg(++index);
      file2.seekp(index);

      // reads the next value
      file1.read(&currentByte, 1);
   }
}

void Encryption::close()
{
   file1.close();
   file2.close();
}

The encryption is simple enough that encrypting an already encrypted file will decrypt it.
But, every time I try to run it, it says error.


Do you mean compile - as there's no statement in the program that would display error.

If the program doesn't compile then there's probably an issue with your compiler. what os/compiler/version are you using?

You're probably better off haveing input as an ifstream and output as an ofstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string line;
	ifstream input("October20.txt");
	ofstream output("EncryptOct20.txt");

	if (input.is_open())
	{
		cout << "Encryption Level 1";

		while (getline(input, line))
		{
			cout << "Opening input file....";
			cout << "Encrypting....";
			cout << line << endl;
			cout << "Encryption complete.";
		}
		input.close();
	}

	return 0;

}


which compiles OK with VS.


you can do it in about 10-15 lines...
read file stuff
<random> -- seed it with a numeric password or allow any password and do a hash function to make the password into a number
xor each byte with next random byte
write output file

but this is a binary encrypted file, not text, so watch out for that -- treat both in and out files as binary is easy to do...
Topic archived. No new replies allowed.