I have made a little program that works similar to cat, but it also decrypts/encrypts the stream, as well as printing to stdout. It does this by simple XOR encryption.
Above is a fragment of my code; it XORs a character read from a file with a "key" value, and then prints that value to stdout.
/* XOR each character in the stream, printing each XOR'ed char to stdout. */
while( (ch = getc(fp)) != EOF )
if( ++offset >= length )
offset = 0;
It has a very specific problem: it won't pipe data read from stdin to other programs, it will only pipe "normal" files successfully.
When I run a command like this:
xor [file] [key] | nc 192.168.0.1 1234
the listening nc will recieve the XOR encrypted [file], no hassle at all.
But, when I run this,
xor [key] | nc 192.168.0.1 1234
the listening nc doesn't recieve any data that I send (entered via keyboard).
When I test it with cat,
cat | nc 192.168.0.1 1234
the listening nc recieves any data that I send.
When I just run,
It works just fine; it prints the XOR'ed value of each char that I enter via the keyboard.
This is only a problem when I try to use stdin as input and pipe the XOR'ed output to another program. The program will pipe a "normal" file to other programs, no trouble at all.
Why does it appear to work for stdin by itself, but not when used in conjunction with other programs?
I have tried reading the source for cat, but it is too complex for me to understand fully.
I am simply assigning stdin to fp like this:
fp = stdin;