Skip to content

Instantly share code, notes, and snippets.

@darkf
Created March 5, 2013 03:19
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 darkf/5087725 to your computer and use it in GitHub Desktop.
Save darkf/5087725 to your computer and use it in GitHub Desktop.
Cesar cipher in C
/* 2013/3/4 by darkf - licensed under WTFPL - cesar cipher program */
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int key, i, len;
char output[128];
if(argc != 3) {
fprintf(stderr, "usage: %s CIPHER KEY\n", argv[0]);
return 1;
}
if(sscanf(argv[2], "%d", &key) != 1) {
fprintf(stderr, "invalid key: %s\n", argv[2]);
return 2;
}
if(strcmp(argv[1], "cesar") == 0) {
int c;
i = 0;
while((c = fgetc(stdin)) != EOF)
output[i++] = ((unsigned char) c) + key;
len = i;
}
else {
fprintf(stderr, "invalid cipher: %s\n", argv[1]);
return 2;
}
printf("output: ");
for(i = 0; i < len; i++)
printf("%02X (%c) ", output[i], output[i]);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment