/* Vignere Console Encryptor [VCE] Coded by Dylan Fleming [flemingd@gmail.com] 05DEC05 This program will take data from a file or data stream and encrypt it using the Vignere cipher. This was a requested program on www.rentacoder.com and was completed to fill the request. Dylan Fleming retains all rights to this program and the source code contained therein. Unauthorized use of this code or the program may result in a lawsuit. Please contact Dylan Fleming via e-mail if you have any question about this programming or wish to use the source code or application for yourself. */ #include #include #include /* show help information about this program */ void show_usage() { printf("Vignere Console Encryptor [VCE]\n"); printf("Coded by Dylan Fleming [flemingd@gmail.com]\n"); printf("Usage:\n" "vce [fotked] [arguments ...]\n" "Variables are passed in tar-style format.\n" " h or --help\tShow this message.\n" " f\t\tName of file to read data from.\n" " o\t\tName of file to output data to.\n" " t\t\tRead data from stdin.\n" " k\t\tKey to be used to process data.\n" " e\t\tEncrypt the input data.\n" " d\t\tDecrypt the input data.\n" ); printf("Examples:\n" " vce toke cipher_text.txt KEYSTRINGabc123\n" " vce dfok cipher_text.txt plain_text.txt KEYSTRINGabc123\n" ); exit(1); } /* encrypts data aquired from string and places it in output file variables: stream [char array] - input data count [integer] - length of input data ofile [FILE handle] - output file keystring [char array] - key for encryption addkey [toggle option] - 1 = add key, 0 = subtract key */ int cipher(unsigned char *stream, FILE *ofile, unsigned char *keystring, unsigned char addkey) { unsigned long n, j; unsigned long len, slen; unsigned char c; /* setup cipher for processing */ len = strlen(keystring); slen = strlen(stream); /* begin cipher itterations */ for ( n = 0, j= 0; n < slen; n++ ) { c = stream[n]; if ( addkey ) c += keystring[j]; else c -= keystring[j]; j = ( j++ ) % len; fputc(c, ofile); } /* cleanup and return */ fclose(ofile); return 1; } /* encrypts data aquired from input file and places it in output file variables: hfile [FILE handle] - input file ofile [FILE handle] - output file keystring [char array] - key for encryption addkey [toggle option] - 1 = add key, 0 = subtract key */ int fcipher(FILE *hfile, FILE *ofile, unsigned char *keystring, unsigned char addkey) { unsigned long n = 0, j = 0; unsigned long len; unsigned char c; /* setup cipher for processing */ len = strlen(keystring); /* begin cipher itterations */ while (1) { c = fgetc(hfile); if ( feof(hfile) ) break; if ( addkey ) c += keystring[j]; else c -= keystring[j]; j = ( j++ ) % len; fputc(c, ofile); } /* cleanup and return */ fclose(ofile); fclose(hfile); return 1; } /* main entry point for program */ int main(int argc, char *argv[]) { unsigned char addkey = 0; unsigned char f, n, a, c; unsigned short len; unsigned char iname[1024], oname[1024], keyring[1024], sdata[4096]; FILE *ifile, *ofile; /* initialize string data variables */ memset(oname, 0, sizeof(oname)); memset(iname, 0, sizeof(iname)); memset(keyring, 0, sizeof(keyring)); /* process arguments for program */ if ( argc < 2 ) show_usage(); if ( !strcmp(argv[1], "--help") || !strcmp(argv[0], "/?") ) show_usage(); len = strlen(argv[1]); for ( f = 1, n = 0, a = 2, c = 0; c < len; c++ ) { n = argv[1][c]; if ( n == 'h' ) show_usage(); /* show help */ else if ( n == 'f' ) { strcpy(iname, argv[a]); a++; } /* read input from file */ else if ( n == 'e' ) { addkey = 1; } /* add keystring to data */ else if ( n == 'd' ) { addkey = 0; } /* subtract keystring from data */ else if ( n == 't' ) { f = 0; } /* read input from stdin */ else if ( n == 'o' ) { strcpy(oname, argv[a]); a++; } /* set output file name */ else if ( n == 'k' ) { strcpy(keyring, argv[a]); a++; } /* set key */ else if ( n == '-' ) { a++; a--; } /* do nothing */ else { printf("Unknown option \"%c\", ignored.\n", n); } /* throw error for invalid options */ } /* check buffers for proper data */ if ( !strlen(oname) ) show_usage(); if ( f && !strlen(iname) ) show_usage(); if ( !strlen(keyring) ) show_usage(); if ( (ofile = fopen(oname, "wb")) == NULL ) { printf("Invalid output file name or file creation failed.\n"); return 1; } if ( f && ((ifile = fopen(iname, "rb")) == NULL) ) { printf("Invalid input file name or file not found.\n"); return 1; } /* process input file */ if ( f ) { if ( !fcipher(ifile, ofile, keyring, addkey) ) { printf("Processing of file \"%s\" failed.\n", iname); return 1; } printf("File \"%s\" finished processing successfully.\n", iname); return 0; } /* process typed data */ else { memset(sdata, 0, sizeof(sdata)); printf("Please start your writing your message to be encryted now. [Max. 4000 characters]\n\n"); if ( fgets(sdata, 4000, stdin) == NULL ) { printf("Error reading data for encryption.\n"); return 1; } if ( !cipher(sdata, ofile, keyring, addkey) ) { printf("Processing of typed data failed.\n"); return 1; } printf("Typed data finished processing successfully.\n"); return 0; } return 0; }