Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- int main (int argc, char *argv[])
- {
- int choice; //command line element 'i', 'o' or 's' interpreted by getopt
- char *ifilename;
- char *ofilename;
- int seed; //stores seed for rand()
- extern char *optarg;
- extern int optind, optopt;
- while((choice = getopt(argc, argv, ":i:o:s:")) != -1)
- //sets variables according to command line arguments
- {
- switch(choice)
- {
- case 'i':
- ifilename = optarg;
- break;
- case 'o':
- ofilename = optarg;
- break;
- case 's':
- seed = atoi(optarg);
- break;
- case ':': //missing operands
- printf("-%c missing operand\n", optopt);
- case '?': //invalid argument
- printf("Invalid argument: -%c\n", optopt);
- }
- }
- FILE *ifile; //file to be encrypted
- FILE *ofile; //storage of encrypted file
- ifile = fopen(ifilename, "r");
- ofile = fopen(ofilename, "w");
- srand(seed);
- int key = rand();
- int c;
- while((c = fgetc(ifile)) != EOF)
- {
- if(ferror(ifile))
- {
- printf("Read error\n");
- clearerr(ifile);
- break;
- }
- else
- fputc(c ^ key, ofile);
- if (ferror(ofile))
- {
- printf("Write error\n");
- clearerr(ofile);
- break;
- }
- }
- fclose(ifile);
- fclose(ofile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement