Thread: Problem with Cipher Program; not reading every bit?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Connecticut
    Posts
    1

    Problem with Cipher Program; not reading every bit?

    In a nutshell, this program should take every individual bit from a file and increment it by a given (and increasing) amount, or decrement it by the same amount depending on the instructions given in the command prompt.

    I tested the code on a txt file consisting of 100 bits (each valued at 0) and printing each conversion as [0--->1], [0--->2] and so on, all the way to [0--->100]. The first bit is incremented by one, the second by two, and so on.

    When I ran the program to decrypt the file by decrementing the same amount, it started as I expected, with [1--->0], [2--->0], and so on, but stopped at the 25th conversion, [25--->0] and I can't figure out why.

    Here's the segment of the code that makes the conversion. If argv[1] is "E", it encrypts by incrementing the bytes, and if otherwise (in this case it would be "D") it will decrypt by decrementing the bytes.

    Code:
    /*SETS THE INPUT AND OUTPUT MODE TO BINARY*/
        setmode( fileno(stdin), O_BINARY ) ;
        setmode( fileno(stdout), O_BINARY ) ;
        
        int B, newB ;
        FILE *input, *output ;
        input = fopen( argv[3], "r" ) ; /*OPEN A FILE STREAM*/
        output = fopen( argv[4], "w" ) ; /*OPEN A FILE STREAM*/
        
        while( ( B = fgetc( input ) ) != EOF ) /*FOR EACH BYTE B OF THE FILE*/
        {
               /*ENCRYPTS OR DECRYPTS DEPENDING ON INSTRUCTIONS*/
               if( strcmp( argv[1], "E" ) == 0 )
                   newB = ( B + N ) % 256 ;
               else
                   newB = ( B + 256 - N ) % 256 ;
               
               fputc( newB, output ) ; /*WRITES OUT B*/
               
               N = ( N + 1 ) % 256 ;
               printf( "[%d--->%d]\n", B, newB ) ;
        }
        
        /*CLOSES THE TWO FILE STREAMS*/
        fclose( input ) ;
        fclose( output ) ;
        
        /*EXITS THE PROGRAM*/
        exit( EXIT_SUCCESS ) ;

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    For starters you aren't encryting a bit but every byte, but that's just semantics.
    Variable "N" isn't defined anywhere so it will contain garbage and perhaps the reason for your program stall.
    Lastly you don't need any logic in the else part while decryting since every byte read is just zeroed out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM

Tags for this Thread