Thread: Exclusice-OR

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    22

    Exclusice-OR

    I have to write a stream cipher using the exclusive-OR (XOR), I have to read text from a file and XOR it to rand. How do I compare the two, I just want to know which one I need to convert, and convert to what, so that they both have the same number of bits.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I think the best way is to open the file in binary using fread(). Of course you'll need to use rand() to generate a random number (or an array if you want to make a longer *password*). Make sure you put the settings for fread() right, reading a specific number of blocks, each with sizeof(int) bytes. Then compare each two corresponding integers using the ^ operator and obviously save the result (probably in another file).

    I know two ways to compare the right two integers. One obvious way is to loop through the clear text and cypher, and test whether you reached the end of the password to reset it. Something like this:
    Code:
    for (i=0; i < cleartext_length; i++)
    {
    	if (j == password_length) j=0;
    	cyphertext[i] = cleartext[i] ^ password[j++];
    }
    I wrote this fast so don't depend on it

    Another way is to divide the clear text length by the password length, then loop between them like this (you'll need the remainder too):
    Code:
    div = cleartext_length / cypher_length;
    
    for (i=0; i < div; i++)
    	for (j=0; j < cypher_length; j++)
    		cyphertext[i*cypher_length+j] = cleartext[i*cypher_length+j] ^ cypher[j];
    
    mod = cleartext_length &#37; cypher_length;
    if (mod)
    	for (j=0; j < mod; j++)
    		cyphertext[cleartext_length-mod+j] = cleartext[cleartext_length-mod+j] ^ cypher[j];
    Again, I wrote these in a rush so don't depend on them.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    22
    um, I'm still unclear. I need to read text in and compare it with random numbers. So I have it compiling with one warning, but it seg faults right away and I cant see why.
    The warning is passing argument 1 of ‘atoi’ makes pointer from integer without a cast on line 95

    Code:
    		/*set seed value*/	
    		seed = atoi(argv[3]);	
    		srand(seed);		
    		
    		stream[i] = fgetc(infp);
    		++i;
    		printf (" line 91 \n ");
    		/*atoi converts to integer*/
    		for (i; (stream[i] != EOF); ++i)
    		{
    			s = atoi(stream[i]); /*line 95*/
    			s = s ^ rand();
    			putc(s, outfp);
    			stream[i] = fgetc(infp); 	
    		}
    Last edited by Brimak86; 01-30-2008 at 07:22 PM.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    for (i; (strncmp(stream[i], EOF) != 0); ++i)
    This looks highly suspicious. In fact, i don't even understand why you can compile it since strncmp takes 3 arguments and you only have 2. Maybe i'm missing something somewhere else. Or maybe you forgot to include string.h.

    Your code is a bit hard to understand since we don't know how your variables are declared.

    Anyway.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't need to convert characters to integers, they already are small integers.

    s = stream[i] ^ rand();

    You don't really need to store everything in a large array (how large should it be?)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed