simple encyption program

M

Mark

can someone tell me why the following encrypts a file
ok, but does'nt work back the other way when you run
it on a encrypted file, like its designed to.

It decrypts some of input ok, but it seems to finish
early.

{
char c;
unsigned int count=0;
unsigned int keysize=0;

key = argv[1];
keysize = key.size();

filename = argv[2];
fileOut = argv[3];

input = new ifstream(filename.c_str());
output = new ofstream(fileOut.c_str());

input->get(c);
while ( (!input->eof()))
{
char charOut;

charOut = c^key[count];
count++;
*output << charOut;
if ( count > keysize )
{
count = 0;
}
input->get(c);
}

input->close();
output->close();
delete input;
delete output;
return 0;
}
 
K

Karl Heinz Buchegger

Mark said:
can someone tell me why the following encrypts a file
ok, but does'nt work back the other way when you run
it on a encrypted file, like its designed to.

It decrypts some of input ok, but it seems to finish
early.

Because your encrypted file no longer contains printable
characters only (is no longer a purely text file). Most
file systems have a special 'character' to represent
EOF within the file itself. Your encryption produced
such a character.

You need to work with binary files and forget
about using feof() (which you used incorrectly
anyway).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top