From a4a0b84f2765c19eae7d270e1627f4b00fa0a99a Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 2 Apr 2015 02:23:14 +0200 Subject: Simple implementation of vigenere cipher. (offset of 1 for desired results) --- vigenere.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 vigenere.c (limited to 'vigenere.c') diff --git a/vigenere.c b/vigenere.c new file mode 100644 index 0000000..8ed4a6c --- /dev/null +++ b/vigenere.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +#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 \n", argv[0]); + exit(EXIT_FAILURE); + } + + key = argv[1]; + offset = strtol(argv[2], NULL, 10); + ciphertext = 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'; +} -- cgit v1.2.3-70-g09d2 From f8b900badcc505af67575d17e6c59abc92961327 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 3 Apr 2015 12:00:50 +0200 Subject: foursquare: various cleanup. --- foursquare.c | 189 +++++++++++++++++++++++++++++------------------------------ vigenere.c | 8 +-- 2 files changed, 98 insertions(+), 99 deletions(-) (limited to 'vigenere.c') diff --git a/foursquare.c b/foursquare.c index c5ab68c..6fc894e 100644 --- a/foursquare.c +++ b/foursquare.c @@ -14,119 +14,118 @@ #include #include #include +#include #define Q ('Q'-'A') -typedef struct vec2 { - int x; - int y; -} Vec2; +#define CTOI(c) ((c) - 'A') +#define ITOC(c) ((c) + 'A') + +struct vec2 { + int x; + int y; +}; int increinc(int *); -void allupper(char *); +void preprocess(char *); void genkey(char *, char *); -Vec2 *newVec2(const int, const int); +struct vec2 *newVec2(const int, const int); int main(int argc, char **argv) { - int x, y, c=0; - - if(argc!=4){ - printf("Usage: %s ", *argv); - exit(1); - } - - printf("%s, %s ", *(argv+1), *(argv+2)); - - char *trkey = *(argv+1); - allupper(trkey); - char *trfullkey = malloc(26); - genkey(trkey, trfullkey); - - char *blkey = *(argv+2); - allupper(blkey); - char *blfullkey = malloc(26); - genkey(blkey, blfullkey); - - char *cipher = *(argv+3); - allupper(cipher); - - int i=0; - Vec2 *trtable[25]; - for(x=0; x<5; x++) - for(y=0; y<5; y++) - trtable[trfullkey[i++]-'A'] = newVec2(x, y); - free(trfullkey); - - i = 0; - Vec2 *bltable[25]; - for(x=0; x<5; x++) - for(y=0; y<5; y++) - bltable[blfullkey[i++]-'A'] = newVec2(x, y); - free(blfullkey); - - char basetable[5][5]; - for(x=0; x<5; x++) - for(y=0; y<5; y++){ - if(c==Q) - c++; - basetable[x][y] = 'A' + c++; - } - - for(i=0; ix][bltable[c2-'A']->y]); - putchar(basetable[bltable[c2-'A']->x][trtable[c1-'A']->y]); - } - putchar('\n'); - - for(i=0; i++; i<25){ - free(trtable[i]); - free(bltable[i]); - } - - return 0; + int x, y, c = 0, i; + char trfullkey[26], blfullkey[26], *trkey, *blkey, *cipher; + struct vec2 trtable[25], bltable[25]; + + if(argc!=4){ + printf("Usage: %s ", *argv); + exit(1); + } + + printf("%s, %s ", *(argv+1), *(argv+2)); + + trkey = argv[1]; + preprocess(trkey); + genkey(trkey, trfullkey); + + blkey = argv[2]; + preprocess(blkey); + genkey(blkey, blfullkey); + + cipher = argv[3]; + preprocess(cipher); + + i = 0; + for(x=0; x<5; x++) + for(y=0; y<5; y++) { + trtable[CTOI(trfullkey[i])].x = x; + trtable[CTOI(trfullkey[i])].y = y; + i++; + } + + i = 0; + for(x=0; x<5; x++) + for(y=0; y<5; y++) { + bltable[CTOI(blfullkey[i])].x = x; + bltable[CTOI(blfullkey[i])].y = y; + i++; + } + + + char basetable[5][5]; + for (x = 0; x < 5; x++) + for (y = 0; y < 5; y++){ + if (c == Q) + c++; + basetable[x][y] = 'A' + c++; + } + + for(size_t i = 0; i < strlen(cipher) / 2; i++){ + char c1 = cipher[2*i]; + char c2 = cipher[2*i+1]; + + putchar(basetable[trtable[CTOI(c1)].x][bltable[CTOI(c2)].y]); + putchar(basetable[bltable[CTOI(c2)].x][trtable[CTOI(c1)].y]); + } + putchar('\n'); + + return EXIT_SUCCESS; } -int increinc(int *num) +inline int increinc(int *num) { - (*num)++; - return (*num)++; + (*num)++; + return (*num)++; } -void allupper(char *input) +void preprocess(char *str) { - int i; - for(i=0; ix = x; - v->y = y; - return v; + for (size_t i = 0; i < strlen(input); i++) { + if (!dict[CTOI(input[i])]) { + output[ii++] = input[i]; + dict[CTOI(input[i])] = true; + } + } + + for (unsigned i = 0; i < 26; i++) + if (!dict[i] && i != Q) + output[ii++] = ITOC(i); + + output[ii]='\0'; } diff --git a/vigenere.c b/vigenere.c index 8ed4a6c..5db3e18 100644 --- a/vigenere.c +++ b/vigenere.c @@ -15,13 +15,13 @@ int main(int argc, char **argv) size_t keylength, textlength; if (argc != 4) { - fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } - key = argv[1]; - offset = strtol(argv[2], NULL, 10); - ciphertext = argv[3]; + offset = strtol(argv[1], NULL, 10); + ciphertext = argv[2]; + key = argv[3]; preprocess(key); preprocess(ciphertext); -- cgit v1.2.3-70-g09d2 From bec173d6f06f7a83ec0bb2300f7da4b6fcab6784 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 4 Apr 2015 20:55:41 +0200 Subject: Added more license headers. --- guesskeylength.c | 9 +++++++++ hangman.c | 9 +++++++++ ncurses_windows.c | 9 +++++++++ randtest.c | 9 +++++++++ vigenere.c | 9 +++++++++ 5 files changed, 45 insertions(+) (limited to 'vigenere.c') diff --git a/guesskeylength.c b/guesskeylength.c index 7941a93..8b4409c 100644 --- a/guesskeylength.c +++ b/guesskeylength.c @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski + * + * 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 #include #include diff --git a/hangman.c b/hangman.c index 4cf4c16..dfb75dd 100644 --- a/hangman.c +++ b/hangman.c @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski + * + * 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 #include #include diff --git a/ncurses_windows.c b/ncurses_windows.c index 702aa27..4a733ba 100644 --- a/ncurses_windows.c +++ b/ncurses_windows.c @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski + * + * 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/]. + */ // -*- compile-command: "make LDLIBS=-lncurses ncurses_windows" -*- #include #include diff --git a/randtest.c b/randtest.c index 710377f..fcd394d 100644 --- a/randtest.c +++ b/randtest.c @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski + * + * 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 #include #include diff --git a/vigenere.c b/vigenere.c index 5db3e18..9a64c3c 100644 --- a/vigenere.c +++ b/vigenere.c @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2015 Tomasz Kramkowski + * + * 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 #include #include -- cgit v1.2.3-70-g09d2