On 19 avr, 14:23, philmasterplus <philmasterp...@gmail.com> wrote:
> I am trying to create a simple file encoder (obfuscator) class
> using the XOR(^) operator.
> I am using objects of the class ifstream and ofstream to
> input/output to the file. Right now, I am experimenting with
> xor-encoding a file with a given C-string.
> The problem is, the obfuscated file contains the EOF character
> for my system, and my program cannot proceed to reencode
> (decode) the rest of the obfuscated file.
More to the point, the "obfuscated" file isn't text, so cannot
be written (nor read) if the file is opened in text mode. You
have two choices: open in binary, or use something like rot-13
for obfuscation, which ensures that the obfuscated data is text.
(Rot-13 is very minimal obfuscation, but throw in a little
shuffling, and it should be as good or better than xor'ing.)
> The code can be simplified as follows:
>
> <code>
> //Assuming all necessary includes are in place
> int main()
> {
> ifstream ifs("test.txt");
If this is to read your obfuscated text:
std::ifstream ifs( "test.txt", std::ios::binary ) ;
And of course, in real code, you'll want to check that the open
succeeded.
> char cbuf;
> string sbuf;
> //Other code blah blah blah...
> cbuf = ifs.get();
> while (!ifs.eof()) //Recognizes the EOF-marker-in-the-middle-of-the-
> file
In theory, this test may cause you to miss the last character.
There are two "classical" solutions:
while ( ifs.get( cbuf ) ) { ...
or declare cbuf as an int, and:
while ( cbuf != EOF ) { ...
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|