Add polybius square

This commit is contained in:
geoffrey 2024-09-24 19:56:20 +02:00
parent 098a49429c
commit 0242ed0256
4 changed files with 104 additions and 2 deletions

@ -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

@ -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 */

78
src/polybius.c Normal file

@ -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]);
}

12
src/polybius.h Normal file

@ -0,0 +1,12 @@
#ifndef H_POLYBIUS
#define H_POLYBIUS
#include <stdio.h>
#include <string.h>
#define POLYBIUS_SIZE 5
void cryptPolybius(const char *, const char *);
void generateSquarePolybius(char polybius[5][5], const char *);
#endif