From f21294b03b47e870990c10d76bf6ed999e6fb1f0 Mon Sep 17 00:00:00 2001 From: Bucchino Geoffrey Date: Thu, 21 Oct 2021 19:51:45 +0200 Subject: [PATCH] Initial commit --- cesar.c | 95 +++++++++++++++++++++++++++++++++ cesar.h | 13 +++++ exec.sh | 3 ++ functions.c | 103 ++++++++++++++++++++++++++++++++++++ functions.h | 16 ++++++ main | Bin 0 -> 17104 bytes main.c | 116 +++++++++++++++++++++++++++++++++++++++++ test.txt | 2 + test.txt.crypt | 0 test.txt.crypt.decrypt | 2 + vigenere.c | 9 ++++ vigenere.h | 8 +++ 12 files changed, 367 insertions(+) create mode 100755 cesar.c create mode 100755 cesar.h create mode 100755 exec.sh create mode 100755 functions.c create mode 100755 functions.h create mode 100755 main create mode 100755 main.c create mode 100755 test.txt create mode 100644 test.txt.crypt create mode 100644 test.txt.crypt.decrypt create mode 100644 vigenere.c create mode 100644 vigenere.h diff --git a/cesar.c b/cesar.c new file mode 100755 index 0000000..c7bf99e --- /dev/null +++ b/cesar.c @@ -0,0 +1,95 @@ +#include "cesar.h" + +/* + s -> path source + d -> path destination + key -> key of encryption + This function encrypt data +*/ +int cryptCesar(const char *s, const int key, const int countCharact, char *bufferDst) { + char *buffer; + int i; + int error = countCharact; + + if(error == -1) return error; + + /* Allocation dynamique */ + buffer = malloc(countCharact + 1); /* + 1 for '\0' */ + + /* Copy the data of file in the buffer[] */ + error = copyFile(s, buffer); + if(error != 0) return error; + + /********* Encryption **********/ + for(i = 0; i < countCharact; i++) { + int val = 0; + /* For characters of A to Z */ + if(buffer[i] >= 'A' && buffer[i] <= 'Z') { + if(buffer[i] + key > 'Z') + val = ((buffer[i] - 'A')-26) + key + 'A'; + else + val = (buffer[i] - 'A') + key + 'A'; + } + /* For characters of 'a' to 'z' */ + else if(buffer[i] >= 'a' && buffer[i] <= 'z') { + if(buffer[i] + key > 'z') + val = ((buffer[i] - 'a')- 26) + key + 'A'; + else + val = (buffer[i] - 'a') + key + 'A'; + } + /* For others characters */ + else val = buffer[i]; + + bufferDst[i] = val; + } + + bufferDst[i] = '\0'; + + /* Freedom the memory */ + free(buffer); + + return error; +} +/* + s -> path source + d -> path destination + key -> key of decryption + This function decryption by Cesar +*/ +int decryptCesar(const char *s, const int key, const int countCharact, char *bufferDst) { + int error = countCharact; + char *buffer; + int i; + + if(error == -1) + return error; + + /* Allocation dynamique */ + buffer = malloc(countCharact + 1); /* +1 for '\0' */ + + /* Copy the data from file to buffer */ + error = copyFile(s, buffer); + if(error != 0) return error; + + /********* Decryption **********/ + for(i = 0; i < countCharact; i++) { + int val = 0; + /* buffer[i] >= 'A' AND buffer[i] <= 'Z' */ + if(buffer[i] >= 65 && buffer[i] <= 90) { + val = buffer[i] - key; + /* val < 'A' */ + if(val < 65) + val = val + 26; + } + else + val = buffer[i]; + + bufferDst[i] = val; + } + bufferDst[i] = '\0'; + + /* Freedom the memory */ + free(buffer); + + return error; +} diff --git a/cesar.h b/cesar.h new file mode 100755 index 0000000..148f64d --- /dev/null +++ b/cesar.h @@ -0,0 +1,13 @@ +#ifndef H_INFLATE +#define H_INFLATE + +#define SIZE_MAX_FILE 8192 + +#include "functions.h" + +/* Functions */ +void inflate(); +int cryptCesar(const char *s, const int key, const int countCharact, char *bufferDst); +int decryptCesar(const char *s, const int key, const int countCharact, char *bufferDst); + +#endif diff --git a/exec.sh b/exec.sh new file mode 100755 index 0000000..4adf144 --- /dev/null +++ b/exec.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +gcc -W main.c functions.c cesar.c vigenere.c -o main && ./main $1 $2 $3 $4 diff --git a/functions.c b/functions.c new file mode 100755 index 0000000..9a90af4 --- /dev/null +++ b/functions.c @@ -0,0 +1,103 @@ +#include "functions.h" + +void usage() { + printf("Usage: [options] [crypt|decrypt] [file]\n"); + printf("Options:\n"); + printf("\t-c: chiffrement de Cesar\n"); + printf("\t-v: chiffrement de Vigenere\n"); + printf("\t-t: chiffrement par transposition\n"); +} +int fileExist(const char *path) { + FILE *f=NULL; + f=fopen(path,"r"); + + if(f==NULL){ + return -1; + } + fclose(f); + return 0; +} +/* + path -> path source + This function copy all characters of the file in the buffer +*/ +int copyFile(const char *path, char *buffer) { + FILE *f = fopen(path, "r+"); + int caract = 0; + int i = 0; + + if(f == NULL) return -1; + + /* Copy the characters */ + do { + caract = fgetc(f); + buffer[i] = caract; + i++; + }while(caract != EOF); + + /* add '\0' */ + buffer[i-1] = '\0'; + + fclose(f); + + return 0; +} +/* + s -> path source + This function count the number of characters in the file + return -1 -> error +*/ +int fileNumberCaract(const char *s) { + FILE *f = NULL; + int count = 0; + + f = fopen(s, "r+"); + if(f == NULL) return -1; + + /* Read character by character */ + while(fgetc(f) != EOF) + count++; + + fclose(f); + + return count; +} +/* + data -> data add in the file + path ->path of the file + This function add the data to file + error : -2 -> impossible to open the file dest +*/ +int addDataToFile(const char *data, const char *path) { + FILE *f = fopen(path, "w+"); + + if(f == NULL) return -2; + + fputs(data, f); + + fclose(f); + + return 0; +} +/* + This function display the error + Error : + 0 -> no error for encryption + 1 -> no error for decryption + -1 -> impossible to open the file source + -2 -> impossible to open the file dest +*/ +void err(const int error, const char *path) { + switch(error) { + case 0 : + printf("Vos donnees ont bien etait crypter ou decrypter dans le fichier %s.\n", path); + break; + case -1 : + printf("Une erreur s'est produite durant l'ouverture du fichier %s\n", path); + break; + case -2 : + printf("Une erreur s'est produite durant l'ouverture du fichier %s\n", path); + default: + break; + } +} diff --git a/functions.h b/functions.h new file mode 100755 index 0000000..526072f --- /dev/null +++ b/functions.h @@ -0,0 +1,16 @@ +#ifndef H_FUNCTIONS +#define H_FUNCTIONS + +#include +#include +#include + +/* Functions */ +void usage(); +int fileExist(const char *path); +int copyFile(const char *path, char *buffer); +int addDataToFile(const char *data, const char *path); +int fileNumberCaract(const char *s); +void err(const int error, const char *path); + +#endif diff --git a/main b/main new file mode 100755 index 0000000000000000000000000000000000000000..1ab9ee14842aca37a786195b87250de11ed7ddbd GIT binary patch literal 17104 zcmeHOe{@vUoxhV1K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zVH7_2siP zSf0@MztDt+v|bVMipC$*H@t6Z{HGGvjDx(R_0Ma4`K%1`^9A(1y5BC)3j+CU3;MG( zUaI{x=rZO5r#QRYwFtOR>p01mwIBC+hX&wtor;_fZHZRK;votpIFo9Uqe^EdP@kq$EG(alQ#I)^H|RF~ zP#am(T)n<}P0$RkU1j1xM6R2NFjw8Xwt7unZLUNf&H#kFsTnl12V>QGVct>SP*YuR zHmqKKS8#*5p}M9%NM7ZUj&L$18zc{O1o#-oA3d^RMM8E+A0NrFl26XFkGR#DlN<&2 zA(gCfu|BSn1*j78m`V;oWmy~L+4xH!{E;;g0B(e$$8{+gn1cAyu zo?=FlakDuTi;%3Y0d6AEn3+mixG>`}r7N$`)#Ug#%`M5KE?OR>Nztnk^t!g~vNUPlBAb7QcW`^WC^P!L* zEYn`C$mP4+PwNA+uhDwEKW57NX|ktVyW9Q=U=$PfPfI76H09?88kJ>np6DTbo%TL$ z$8?aC9LeQgd=E0GJ+G^np63S%+6UXS%fEBk^Lm*nKNn$rcmI7(+jIPdPit!0O_g&b zjvqrmM21w@p7%3Mcd(+S?($!8*_X{#icIOfuQR#J{~3hz?pY7}?7ouEPrLP*XZkDH zmOAWtA3ItJ5Q+5^H_kIY0s-|ME}!>*d>(#K*Pr@7CAQ=1cn%n;usxr*9MB8v1!Uw% zY|r!~$ei{fpp~l>?a(X}wqsfZW2b#bKxuYpd#MQlGWi>M6SJ1O@)r|MDdutE=Px)0G-mFvUv=VidS?oRuDy0IXA#U11};|o4{jF{cK`qY literal 0 HcmV?d00001 diff --git a/main.c b/main.c new file mode 100755 index 0000000..fc81fe5 --- /dev/null +++ b/main.c @@ -0,0 +1,116 @@ +#include "functions.h" +#include "cesar.h" +#include "vigenere.h" + +int main(int argc, char *argv[]) { + int error = 0; + int numberCharacters = 0; + char *options[3] = {"-c","-v","-t"}; + char *hook, *fileSrc, *fileDst, *buffer; + size_t sFile, i; + + if(argc != 4) { + usage(); + } + + // Check options + for(i = 0; i < 3; i++){ + if(strcmp(argv[1], options[i]) == 0){ + error++; + } + } + + if(error == 0) { + usage(); + return -1; + } + + // Check crypt|decrypt + if(strcmp(argv[2], "crypt") != 0 && strcmp(argv[2], "decrypt") != 0){ + usage(); + return -1; + } + + hook = argv[2]; + + // Check file exist + error = fileExist(argv[3]); + if(error == -1){ + printf("File doesn't exist\n"); + // usage(); + return -1; + } + + fileSrc = argv[3]; + + if(strcmp(hook, "crypt") == 0) sFile = sizeof(char) + strlen(fileSrc) + 5; + else sFile = sizeof(char) + strlen(fileSrc) + 7; + + fileDst = malloc(sFile); + + for(i = 0; i < strlen(fileSrc); i++){ + fileDst[i] = fileSrc[i]; + } + + fileDst[i++] = '.'; + if(strcmp(hook, "decrypt") == 0){ + fileDst[i++] = 'd'; + fileDst[i++] = 'e'; + } + fileDst[i++] = 'c'; + fileDst[i++] = 'r'; + fileDst[i++] = 'y'; + fileDst[i++] = 'p'; + fileDst[i++] = 't'; + fileDst[i++] = '\0'; + + // Get size of the file + numberCharacters = fileNumberCaract(fileSrc); + + // Malloc the dest buffer + buffer = malloc(sizeof(int) * numberCharacters); + memset(buffer, 0, sizeof(int) * numberCharacters); + + if(strcmp(argv[1], "-c") == 0){ + // Get key + int key; + + do{ + printf("Votre cle de chiffrement (entre 1 et 26): "); + scanf("%d",&key); + }while(key < 1 || key > 26); + + if(strcmp(hook, "crypt") == 0) + error = cryptCesar(argv[3], key, numberCharacters, buffer); + if(strcmp(hook, "decrypt") == 0) + error = decryptCesar(argv[3], key, numberCharacters, buffer); + + } + else if(strcmp(argv[1], "-v") == 0){ + // Get key + char *key = malloc(8); + + + if(strcmp(hook, "crypt") == 0) + error = cryptVigenere(key, argv[3], buffer); + + free(key); + } + else if(strcmp(argv[1], "-t") == 0){ + // Get key + } + + // Add data to file + addDataToFile(buffer, fileDst); + + /* Display error */ + /* + * if(error == 0) err(error, fileDst); + * else if(error == -1) err(error, fileSrc); + * else if(error == -2) err(error, fileDst); + */ + + free(fileDst); + free(buffer); + return 0; +} diff --git a/test.txt b/test.txt new file mode 100755 index 0000000..19afb5a --- /dev/null +++ b/test.txt @@ -0,0 +1,2 @@ +Hello world! +Je suis un message en clair !! diff --git a/test.txt.crypt b/test.txt.crypt new file mode 100644 index 0000000..e69de29 diff --git a/test.txt.crypt.decrypt b/test.txt.crypt.decrypt new file mode 100644 index 0000000..4ab6fa4 --- /dev/null +++ b/test.txt.crypt.decrypt @@ -0,0 +1,2 @@ +HELLO WORLD! +JE SUIS UN MESSAGE EN CLAIR !! diff --git a/vigenere.c b/vigenere.c new file mode 100644 index 0000000..388909f --- /dev/null +++ b/vigenere.c @@ -0,0 +1,9 @@ +#include "vigenere.h" + +int cryptVigenere(const char *key, const char *data, char *bufferDst) { + int error = 0; + + printf("Vigenere\n"); + + return error; +} diff --git a/vigenere.h b/vigenere.h new file mode 100644 index 0000000..b05034e --- /dev/null +++ b/vigenere.h @@ -0,0 +1,8 @@ +#ifndef H_VIGENERE +#define H_VIGENERE + +#include "functions.h" + +int cryptVigenere(const char *key, const char *data, char *bufferDst); + +#endif