Here's what I'm supposed to do:
Write a program (in C), which enciphers or deciphers an input file.
Assume that the input file is any type of file, at all. So you must read it in
using binary mode. Also, the output file will be written using binary mode.
The program will encipher the input file by doing the following:
1. Initialize N to the key value from the command line.
2. While not EOF:
a. Read in a byte, B, of the file.
b. Set B to (B+N) mod 256.
c. Write out B.
d. Increment N mod 256.
Notice that this is the familiar “filter” type of program. A file is deciphered by following exactly the
same procedure, but the key is subtracted from the byte in step c.
Details: The key will be a value from 0 to 255. If it is not, write out an error message and exit. Use
command line parameters for the seed and for the file names.
C:\> cipher E 221 myImage.jpg myCipheredImage.bin
C:\> cipher D -221 myCipheredImage.bin originalImage.jpg
Compute (B+N) mod 256 by doing ordinary addition (B+N) and then doing modular division % 256.
So numbers will always wrap around into the range 0..255. Compute (B-N) mod 256 by computing
(B+256-N) % 256. Test thoroughly that this part of your program is working.
Expect that input files can be anything at all, including ordinary text files, MS word files, image files, or
executables. Of course, the result after deciphering should be identical to the original file. Use
strcmp() to compare the first parameter argv[1] with “E” or “D”. Use atoi(argv[2]) for
converting the second parameter into an int. You will need to use fopen(), fgetc(),
fputc(),and fclose().
Although this cipher will leave your files unreadable by most casual interlopers, it is still what is called
a “simple cipher.” It is nowhere near commercial quality and would easily be broken by any
cryptanalyst.
Debugging: Create some test files and use them to test the operation of your program. The most
obvious test file is a file of all zero-bytes. The output of your program with this file should be an
ascending sequence of bytes, mode 256. The test file can be created by a program you write. Inspect
the output of your program using Irfanview or some other hex dump program. Also test your program
on some image files and some other files.
Here's what I have:
Code:
#include <stdlib.h>
#include <stdio.h>
/* File copy -- copy a source to a destination.
If the destination file exists it will be copied over.
*/
int main ( int argc, char** argv )
{ int N;
printf("enter the key value(0 to 255)");
scanf("%d", &N);
if(N>255 || N<0)
exit(EXIT_FAILURE);
FILE *in, *out;
int ch;
if ( argc != 'E' || argc != 'e' || argc != 'D' || argc != 'E' )
{
fprintf( stderr, "copy source dest\n");
return( EXIT_FAILURE );
}
/* Open the source file for "read binary" so that */
/* end of line characters are not translated */
if ( (in = fopen( argv[1], "rb")) == NULL )
{
perror( argv[1] );
return( EXIT_FAILURE );
}
/* Open the destination file for "write binary" */
if ( (out = fopen( argv[2], "wb")) == NULL )
{
perror( argv[2] );
return( EXIT_FAILURE );
}
/* Copy characters from input to output until end of file */
while ( (ch = fgetc(in)) != EOF )
{
if(argc == 'e' || argc == 'E')
{
ch = (ch + N) % 256;
fputc( ch, out );
N++ % 256;
}
if(argc == 'D' || argc == 'd')
{
ch = (ch - N) % 256;
fputc( ch, out );
N++ % 256;
}
}
/* Close the input and the output files */
if ( fclose( in ) == EOF )
{
perror( argv[1] );
return( EXIT_FAILURE );
}
if ( fclose( out ) == EOF )
{
perror( argv[2] );
return( EXIT_FAILURE );
}
return( EXIT_SUCCESS );
}
what am I doing wrong?