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 ) ;