#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#define BUFFER_SIZE 100
// ./a.out -d 20 de-e.txt normal.txt
//normal is the text in standard text
//de-e is the to be decrypted text (initail is blank)
#define OPTIONS "[-d] [-e] [-h] [-?] key output_file input_file"
void display_usage( char *, char * );
int cypher_encrypt(char *,char *,char *);
int cypher_decrypt(char *,char *,char *);
int main(int argc, char *argv[] ) {
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int count=0;
int i;
//int index;
int c;
/*
* MOD #1: Declare a const char * with the options you want to process
*/
static const char *optString = "deh:";
/******************************
Handles the arguments [-d][-e][-h][-?]
*****************************/
opterr = 0;
while ((c = getopt (argc, argv, optString)) != -1)
switch (c) {
case 'd':
aflag = 1;
// char * file_toOpen, char *key
//cypher_decrypt (argv[3], argv[2]);
cypher_decrypt (argv[4],argv[3], argv[2]);
break;
case 'e':
bflag = 1;
// cypher_encrypt (argv[2]);
//char * file_toOpen, char * file_toWrite, char *key
cypher_encrypt (argv[4],argv[3], argv[2]);
break;
case 'h':
cvalue = optarg;
printf ("Arugments\n (-d) decrypt file\n (-e) encrypt\n file to write to\n file to read from\n");
exit( EXIT_FAILURE );
break;
case '?':
display_usage(argv[0], OPTIONS);
exit( EXIT_FAILURE );
break; /* Will never execute */
default:
/* Will never execute */
perror ("open file");
exit( EXIT_FAILURE );
}
/*
* Check that you have 2 and only 2 reminding arguments
*/
if ( optind != 2 ) {
display_usage( argv[0], OPTIONS);
exit( EXIT_FAILURE );
}
return 0;
}
/*********************************************************************
* display_usuage
********************************************************************/
void display_usage( char * prog, char *opts ) {
fprintf(stderr, "usage: %s %s\n", prog, opts );
return;
}
int cypher_encrypt (char * file_toOpen, char * file_toWrite, char *key ){
int count =0;
int i;
char buffer [BUFFER_SIZE];
char buff[BUFFER_SIZE];
long int keyvalue = strtol (key,NULL,0);
printf ("key is %ld\n",keyvalue );
/////////////////////////////////////////////////////
int fin = open (file_toOpen, O_RDONLY);
int fout = open (file_toWrite, O_WRONLY,O_TRUNC);
int bytes_read = 0;
while ( (bytes_read = read ( fin, buffer, BUFFER_SIZE )) != 0 ) {
for (i=0; i<bytes_read;i++){
buff[i]=buffer[i] + keyvalue;
}
write( fout, buff,bytes_read );
}
//close the open file
close (fin);
close (fout);
printf ("\n");
return 0;
}
int cypher_decrypt (char * file_toOpen, char * file_toWrite, char *key ){
int count =0;
int i;
char buffer [BUFFER_SIZE];
char buff[BUFFER_SIZE];
long int keyvalue = strtol (key,NULL,0);
printf ("key is %ld\n",keyvalue );
/////////////////////////////////////////////////////
int fin = open (file_toOpen, O_RDONLY);
int fout = open (file_toWrite, O_WRONLY,O_TRUNC);
int bytes_read = 0;
while ( (bytes_read = read ( fin, buffer, BUFFER_SIZE )) != 0 ) {
for (i=0; i<bytes_read;i++){
buff[i]=buffer[i] - keyvalue;
}
write( fout, buff,bytes_read );
}
//close the open file
close (fin);
close (fout);
printf ("\n");
return 0;
}
|