Skip to content

Instantly share code, notes, and snippets.

@Denton-L
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Denton-L/46141fb19003aa5d7de2 to your computer and use it in GitHub Desktop.
Save Denton-L/46141fb19003aa5d7de2 to your computer and use it in GitHub Desktop.
A Simple XOR Cipher
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *input, *key, *output;
int inchar, keychar;
if (argc != 4) {
printf("Usage: \"%s [input] [key] [output]\"", argv[0]);
return -1;
}
if ((input = fopen(argv[1], "rb")) == NULL) {
printf("Invalid input file.");
return -1;
}
if ((key = fopen(argv[2], "rb")) == NULL) {
printf("Invalid key file.");
return -1;
}
if ((output = fopen(argv[3], "wb")) == NULL) {
printf("Invalid output file.");
return -1;
}
while ((inchar = fgetc(input)) != EOF) {
if ((keychar = fgetc(key)) == EOF) {
rewind(key);
}
fputc(inchar ^ keychar, output);
}
fclose(input);
fclose(key);
fclose(output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment