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.
// 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(¤tByte, 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> ((unsignedchar) 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(¤tByte, 1);
// increments the pointer
file1.seekg(++index);
file2.seekp(index);
// reads the next value
file1.read(¤tByte, 1);
}
}
void Encryption::close()
{
file1.close();
file2.close();
}
The encryption is simple enough that encrypting an already encrypted file will decrypt it.
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...