diff --git a/exec.sh b/exec.sh index 0117371..3b17929 100755 --- a/exec.sh +++ b/exec.sh @@ -1,3 +1,3 @@ #!/bin/sh -gcc -Wall src/main.c src/functions.c src/caesar.c src/vigenere.c src/polybius.c -o output/main && ./output/main $1 $2 $3 $4 +gcc -Wall src/main.c src/functions.c src/caesar.c src/vigenere.c src/polybius.c -o main && ./main $1 $2 $3 $4 diff --git a/src/polybius.c b/src/polybius.c index e65c5cf..6ffd2da 100644 --- a/src/polybius.c +++ b/src/polybius.c @@ -15,46 +15,30 @@ void cryptPolybius(const char *filename, const char *key){ /* This function fill the polybius square */ -static void fillSquare(char c, int pos, char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE]){ - for (int i = 0; i < POLYBIUS_SIZE; i++) - polybius[pos][i] = c + i; +static void fillSquare(int pos, int *posPolybius, char *p, char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE]){ + for (int i = 0; i < 5; i++){ + polybius[pos][i] = p[*(posPolybius)]; + *posPolybius = *posPolybius + 1; + } } static int findKeyInSquare(const char *polybius, char c){ - for (int i = 0; i < 26; i++) + // We exclude the 'J' character + for (int i = 0; i < ALPHABET_SIZE - 1; i++) if (c == polybius[i]) return 1; - return 0; } /* This function generate the polybius square */ void generateSquarePolybius(char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE], const char *key){ - char a[26] = {0}; - fillSquare('A', 0, polybius); - polybius[1][0] = 'F'; - polybius[1][1] = 'G'; - polybius[1][2] = 'H'; - polybius[1][3] = 'I'; - polybius[1][4] = 'K'; - fillSquare('L', 2, polybius); - fillSquare('Q', 3, polybius); - fillSquare('V', 4, polybius); + char a[ALPHABET_SIZE - 1] = {0}; // The size is 25, we exclude the J character - /* - char a[26]; - a[0] = 'A'; a[1] = 'B'; .... a[25] = 'Z'; - // We add the key into the a array and with a function - // and with the function, we check if the character already exist in the array - // If yes, we do not add it - - // After that, we convert to the polybius[5][5] - */ int pos = 0; for (int i = 0; i < strlen(key); i++){ char k; if (key[i] >= 'a' && key[i] <= 'z') - k = key[i] - 32; // 32 = 26 + some characters + k = key[i] - 32; // 32 = 26 + special characters else k = key[i]; int res = findKeyInSquare(a, k); if (res == 0) @@ -62,17 +46,21 @@ void generateSquarePolybius(char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE], const c } int n = 0; int t = pos; - for (int i = pos; i < 26 - pos; i++){ + for (int i = pos; i < ALPHABET_SIZE + pos; i++){ char c = 'A' + n; + if (c != 'J'){ + int res = findKeyInSquare(a, c); - if (res == 0){ + if (res == 0) a[t++] = c; - n++; } - else printf("Find key: %c\n", c); - //printf("%d\n", n); + n++; } - for (int i = 0; i < 26; i++) - printf("%c\n", a[i]); + pos = 0; + fillSquare(0, &pos, a, polybius); + fillSquare(1, &pos, a, polybius); + fillSquare(2, &pos, a, polybius); + fillSquare(3, &pos, a, polybius); + fillSquare(4, &pos, a, polybius); } diff --git a/src/polybius.h b/src/polybius.h index c6b6eb4..63b807e 100644 --- a/src/polybius.h +++ b/src/polybius.h @@ -5,6 +5,7 @@ #include #define POLYBIUS_SIZE 5 +#define ALPHABET_SIZE 26 void cryptPolybius(const char *, const char *); void generateSquarePolybius(char polybius[5][5], const char *);