commit f21294b03b47e870990c10d76bf6ed999e6fb1f0 Author: Bucchino Geoffrey Date: Thu Oct 21 19:51:45 2021 +0200 Initial commit 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 0000000..1ab9ee1 Binary files /dev/null and b/main differ 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