Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Stream Cipher

Name: Anonymous 2007-11-21 16:10

Help make this stream cipher better!


#include <stdio.h>
#include <stdlib.h>

#define bit(b,v) ((v&(1<<((b)%32)))>>((b)%32))

int rule[] = { 0, 1, 1, 1, 1, 0, 0, 0 };

int main(int argc, char **argv)
{
    unsigned int c, i, b, nkey, key, len;

    if(argc < 3) exit(1);
    key = atoi(argv[1]);
    len = atoi(argv[2]);

    for(i=0; i<len; i++) {

        c = getchar();
        c ^= key;
        putchar(c);

        for(b=0, nkey=0; b<32; b++)
            nkey |= rule[
                bit(b-1,key) * 4 +
                bit(b  ,key) * 2 +
                bit(b+1,key) * 1 ] << b;
        key = nkey;
    }

    return 0;
}

Name: Anonymous 2007-11-21 19:19

>>7

quoting gets man page

---------------
fgetc()  reads  the  next  character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

getc() is equivalent to fgetc() except that it may be implemented as a macro  which evaluates stream more than once.

getchar() is equivalent to getc(stdin).
---------------

You don't get an EOF unless the stream reaches EOF or errors. The cast to int allows for values outside of 0-255. EOF happens to be value -1 in my libc.

Fail less.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List