diff options
author | EliteTK <tomasz.kramkowski@gmail.com> | 2015-06-19 19:12:12 +0100 |
---|---|---|
committer | EliteTK <tomasz.kramkowski@gmail.com> | 2015-06-19 19:12:12 +0100 |
commit | da87fcf25e0c94e57f00df84679cd6fadc56ed46 (patch) | |
tree | 3c53eea9db01039990455af870a2ca65e7e5a123 /vigenere.c | |
parent | 75d2e00662416224f4b745e0004f48f1fc1d9665 (diff) | |
parent | 7bf25fb8f0e4643a67894417a95d39e5901b1824 (diff) | |
download | c-stuff-da87fcf25e0c94e57f00df84679cd6fadc56ed46.tar.gz c-stuff-da87fcf25e0c94e57f00df84679cd6fadc56ed46.tar.xz c-stuff-da87fcf25e0c94e57f00df84679cd6fadc56ed46.zip |
Merge branch 'master' of https://github.com/EliteTK/c-stuff
Diffstat (limited to 'vigenere.c')
-rw-r--r-- | vigenere.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/vigenere.c b/vigenere.c new file mode 100644 index 0000000..9a64c3c --- /dev/null +++ b/vigenere.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski <tk@the-tk.com> + * + * This program is free software. It is licensed under version 3 of the + * GNU General Public License. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see [http://www.gnu.org/licenses/]. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#define CTOI(c) ((c) - 'A') +#define ITOC(c) ((c) + 'A') + +void preprocess(char *str); + +int main(int argc, char **argv) +{ + char *key, *ciphertext, *decodepos, *decodeneg; + long offset; + size_t keylength, textlength; + + if (argc != 4) { + fprintf(stderr, "Usage: %s <offset> <ciphertext> <key>\n", argv[0]); + exit(EXIT_FAILURE); + } + + offset = strtol(argv[1], NULL, 10); + ciphertext = argv[2]; + key = argv[3]; + + preprocess(key); + preprocess(ciphertext); + + keylength = strlen(key); + textlength = strlen(ciphertext); + + decodepos = malloc(textlength + 1); + decodeneg = malloc(textlength + 1); + + for (size_t i = 0; i < textlength; i++) { + decodepos[i] = ITOC((CTOI(ciphertext[i]) + offset + CTOI(key[i % keylength])) % 26); + decodeneg[i] = ITOC((CTOI(ciphertext[i]) + 52 - offset - CTOI(key[i % keylength])) % 26); + } + + decodepos[textlength] = '\0'; + decodeneg[textlength] = '\0'; + + printf("+ %s\n- %s\n", decodepos, decodeneg); + + free(decodepos); + free(decodeneg); + + return EXIT_SUCCESS; +} + +void preprocess(char *str) +{ + char *src = str, *dest = str; + + while (*src != '\0') { + if (isalpha(*src)) + *(dest++) = toupper(*src); + src++; + } + + *dest = '\0'; +} |