//XOR Encrypt 0.1.0 //by Sean Kane - http://celtickane.com #include #include //malloc #include //strlen #define MAX_SIZE 80 //change this if you want larger buffers int main() { char bufFileIn[MAX_SIZE], bufKey[MAX_SIZE], bufFileOut[MAX_SIZE]; //Buffers FILE *fpIn, *fpOut; //File pointers char *mp; //Pointer for malloc char chrPrompt, chrXOR; long fSize; int iKey, iFile; //Incremental variables for loops for (;;) { printf("(O)pen file, (I)nformation, (Q)uit: "); chrPrompt = getchar(); switch ( chrPrompt ) { case 'q': case 'Q': //Quit the program exit(1); //Exit with a return code of 1 break; case 'o': case 'O': //Open a file //Read file for input fflush(stdin); //Clear out garbage from stdin printf("\nEnter filename for input: "); gets(bufFileIn); //Similar to scanf, but will include spaces fpIn = fopen(bufFileIn, "rb"); if (fpIn == NULL) //file pointers return NULL if they errored { printf("\nUnable to access input file.\n\n"); break; } //Read encryption key printf("Enter encryption key: "); gets(bufKey); //Prepare file for output printf("Enter filename for output: "); gets(bufFileOut); fpOut = fopen(bufFileOut, "wb"); if (fpOut == NULL) { printf("\nUnable to write to output file.\n\n"); break; } //Actually read the input file fseek(fpIn, 0, SEEK_END); //Seek to the end fSize = ftell(fpIn); //Sets fSize to total size of input file rewind(fpIn); //Start back at the beginning mp = (char*) malloc (fSize); //Allocate memory -- set location as mp (memory pointer) if (mp == NULL) //Couldn't allocate memory { printf("Unable to allocate necessary memory to open file."); exit(2); } fread (mp, 1, fSize, fpIn); //Display data and encrypt/decrypt (same thing) printf("\nFile input data:\n"); for (iFile = 0; iFile < fSize; iFile++) { printf("%c",mp[iFile]); } iKey = 0; printf("\n\nFile output data:\n"); for (iFile = 0; iFile < fSize; iFile++) { chrXOR = mp[iFile] ^ bufKey[iKey]; printf("%c", chrXOR); fputc(chrXOR, fpOut); (iKey == (strlen(bufKey) -1)) ? iKey = 0 : iKey++; } //Close everything up fclose(fpIn); fclose(fpOut); free(mp); printf("\n\nFile successfully written.\n\n"); break; case 'i': case 'I': fflush(stdin); //Again, we need to clear the stdin buffer or it'll mess up printf("\nXOR Encrypt version 0.0.2 by Sean Kane\nhttp://celtickane.com/programming\n\n"); break; default: printf("Please type O, I, or Q.\n"); break; } //End switch } //End for return 0; } //End main