From 0242ed0256cde6934f45b21c8e81c77ff9680cfe Mon Sep 17 00:00:00 2001 From: geoffrey Date: Tue, 24 Sep 2024 19:56:20 +0200 Subject: [PATCH] Add polybius square --- exec.sh | 2 +- src/main.c | 14 ++++++++- src/polybius.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/polybius.h | 12 ++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/polybius.c create mode 100644 src/polybius.h diff --git a/exec.sh b/exec.sh index f493fee..0117371 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 -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 output/main && ./output/main $1 $2 $3 $4 diff --git a/src/main.c b/src/main.c index 5bfc837..0153b1d 100755 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include "functions.h" #include "caesar.h" #include "vigenere.h" +#include "polybius.h" int main(int argc, char *argv[]) { int error = 0; @@ -14,7 +15,7 @@ int main(int argc, char *argv[]) { } // Check kind of encryption - for(i = 0; i < 3; i++){ + for(i = 0; i < 4; i++){ if(strcmp(argv[1], options[i]) == 0) error++; } @@ -100,7 +101,18 @@ int main(int argc, char *argv[]) { } // Polybius square else if(strcmp(argv[1], "-s") == 0){ + char key[26]; + printf("Your key: "); + scanf("%s", key); + + if (strlen(key) > 26){ + printf("Your key is too big. Choose a small one\n"); + return -1; + } + + if(strcmp(hook, "crypt") == 0) + cryptPolybius(fileSrc, key); } /* Display error */ diff --git a/src/polybius.c b/src/polybius.c new file mode 100644 index 0000000..e65c5cf --- /dev/null +++ b/src/polybius.c @@ -0,0 +1,78 @@ +#include "polybius.h" + + +void cryptPolybius(const char *filename, const char *key){ + char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE]; + + generateSquarePolybius(polybius, key); + + for (int i = 0; i < POLYBIUS_SIZE; i++){ + for (int j = 0; j < POLYBIUS_SIZE; j++) + printf("%c ", polybius[i][j]); + printf("\n"); + } +} +/* + 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 int findKeyInSquare(const char *polybius, char c){ + for (int i = 0; i < 26; 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[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 + else k = key[i]; + int res = findKeyInSquare(a, k); + if (res == 0) + a[pos++] = k; + } + int n = 0; + int t = pos; + for (int i = pos; i < 26 - pos; i++){ + char c = 'A' + n; + int res = findKeyInSquare(a, c); + + if (res == 0){ + a[t++] = c; + n++; + } + else printf("Find key: %c\n", c); + //printf("%d\n", n); + } + for (int i = 0; i < 26; i++) + printf("%c\n", a[i]); +} diff --git a/src/polybius.h b/src/polybius.h new file mode 100644 index 0000000..c6b6eb4 --- /dev/null +++ b/src/polybius.h @@ -0,0 +1,12 @@ +#ifndef H_POLYBIUS +#define H_POLYBIUS + +#include +#include + +#define POLYBIUS_SIZE 5 + +void cryptPolybius(const char *, const char *); +void generateSquarePolybius(char polybius[5][5], const char *); + +#endif