Seshadri R
Seshadri R

Reputation: 1212

Encrypt a file using one's complement

I have used C++ file streams to encrypt a file. In this, every character read is one's complemented and written into the output file:

ifstream input("Normal file");
ofstream output("encrypted file");
char ch;
while (input >> ch)
{
  ch = ~ch;
  output << ch;
}

I used the same program to recover normal file from encrypted file. However, I found that the unencrypted file did not have any blank spaces or return marks. So, I changed the while part to:

while (input >> ch)
{
  ch = isspace(ch) ? ch : ~ch;
  output << ch;
}

Till the results are not what I expected. Where did I go wrong?

Upvotes: 0

Views: 113

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490653

The problem is in how you're reading the file:

char ch;
while (input >> ch)

By default, the stream extractor skips all whitespace. To get it to stop that, you can do something like:

input >> std::noskipws;

...before you start reading from the stream.

Upvotes: 1

Related Questions