From ca47a6a637aa45310f0db8915274ca5d374cdde1 Mon Sep 17 00:00:00 2001 From: Bucchino Geoffrey Date: Mon, 20 Jun 2022 21:41:33 +0200 Subject: [PATCH] Update caesar chipher --- cesar.c => caesar.c | 33 +++++++++++++++------------------ caesar.h | 12 ++++++++++++ cesar.h | 13 ------------- exec.sh | 2 +- main | Bin 21240 -> 21248 bytes main.c | 17 +++++++++-------- test.txt.crypt | 2 ++ test.txt.crypt.decrypt | 2 -- 8 files changed, 39 insertions(+), 42 deletions(-) rename cesar.c => caesar.c (67%) create mode 100755 caesar.h delete mode 100755 cesar.h delete mode 100644 test.txt.crypt.decrypt diff --git a/cesar.c b/caesar.c similarity index 67% rename from cesar.c rename to caesar.c index c7bf99e..173ed26 100755 --- a/cesar.c +++ b/caesar.c @@ -1,4 +1,4 @@ -#include "cesar.h" +#include "caesar.h" /* s -> path source @@ -6,17 +6,18 @@ key -> key of encryption This function encrypt data */ -int cryptCesar(const char *s, const int key, const int countCharact, char *bufferDst) { +int cryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst) { char *buffer; int i; int error = countCharact; + char c = 0; if(error == -1) return error; /* Allocation dynamique */ buffer = malloc(countCharact + 1); /* + 1 for '\0' */ - /* Copy the data of file in the buffer[] */ + /* Copy the data of file into the buffer[] */ error = copyFile(s, buffer); if(error != 0) return error; @@ -25,29 +26,25 @@ int cryptCesar(const char *s, const int key, const int countCharact, char *buffe 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'; + c = buffer[i] - 'A'; + val = ((c + key) % 26) + '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'; + c = buffer[i] - 'a'; + val = ((c + key) % 26) + 'A'; } /* For others characters */ else val = buffer[i]; bufferDst[i] = val; } - + bufferDst[i] = '\0'; /* Freedom the memory */ free(buffer); - + return error; } /* @@ -56,22 +53,22 @@ int cryptCesar(const char *s, const int key, const int countCharact, char *buffe key -> key of decryption This function decryption by Cesar */ -int decryptCesar(const char *s, const int key, const int countCharact, char *bufferDst) { +int decryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst) { int error = countCharact; char *buffer; int i; - if(error == -1) + if(error == -1) return error; /* Allocation dynamique */ buffer = malloc(countCharact + 1); /* +1 for '\0' */ - /* Copy the data from file to buffer */ + /* Copy the data from file into buffer */ error = copyFile(s, buffer); if(error != 0) return error; - /********* Decryption **********/ + /********* Decryption **********/ for(i = 0; i < countCharact; i++) { int val = 0; /* buffer[i] >= 'A' AND buffer[i] <= 'Z' */ @@ -87,7 +84,7 @@ int decryptCesar(const char *s, const int key, const int countCharact, char *buf bufferDst[i] = val; } bufferDst[i] = '\0'; - + /* Freedom the memory */ free(buffer); diff --git a/caesar.h b/caesar.h new file mode 100755 index 0000000..c20cf7f --- /dev/null +++ b/caesar.h @@ -0,0 +1,12 @@ +#ifndef H_INFLATE +#define H_INFLATE + +#define SIZE_MAX_FILE 8192 + +#include "functions.h" + +/* Functions */ +int cryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst); +int decryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst); + +#endif diff --git a/cesar.h b/cesar.h deleted file mode 100755 index 148f64d..0000000 --- a/cesar.h +++ /dev/null @@ -1,13 +0,0 @@ -#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 index 4adf144..b3e4b82 100755 --- a/exec.sh +++ b/exec.sh @@ -1,3 +1,3 @@ #!/bin/sh -gcc -W main.c functions.c cesar.c vigenere.c -o main && ./main $1 $2 $3 $4 +gcc -W main.c functions.c caesar.c vigenere.c -o main && ./main $1 $2 $3 $4 diff --git a/main b/main index 9baa1ae32f5362643fd6a11fc9a4f43cc9ceba1c..25bfc580ca74e1dd662dd3ba031246234e028bd5 100755 GIT binary patch delta 1503 zcmb7DYfKzf6ux(Mx3Z?NEDIfmePVocBNSGCKx8o;mRV*i6iW*}(k2iulwEHnbzScs&twvphYS!8XYK-@Nv@y-KN>@Uxcd+zwHiZ@-v`H=@S6|G)@R`+ykH7+B=?;Xs1m85nhq)w=XG~w9 zV4UsemQ3H&Erq!`JkB?b^Q~hwiSzsUSlks$>V(du~Fjqlmw} zqA>Sr%Y1`*2L?Q&HTfe59}s0X*0yf#orwnxcf&l`J%$)Ir$63&n`<+ zklVn)&JCt3$`2U6RHN~yl|YgNl1$fNBr-km>>iU-o-RU>Rg>d3`()KX*_C{R*!qBQ ziLDC=>+B&`*r?+N@-GnhV3m854<=IL7jKZ;$2N6sPR|vSvqyZ<^_B?=jfH0mllZ=) zi;{GeyomlkC252B%)cbbMj_OEU6Q=S&s`UD9@&TOQ4&LJTKXH5FLj6R6TI`D6l5dN zVS)?LTja!jaH`0eyG(SrZQQ*Iw}NoLCb$b0iYg6!LXr$(%=MKK;$ z1BG@Xr2TJO952brKxychOGOM?pi09>9V(#oLbiv2_0fi-N(WKOf&*~eN8?o)Wdwqz zqihs;LKD$kM2>n+Yl`>F<0AGZxshys8K+FB!=)VmU7~UpKCi6v@(TUFY^;Z&3L4YO z>Q>fDh!NzFvVRU%DriGHQFeiRyFSR}XwMKio8`s}RdKym;{$@ry(57(1Vl;nq87cSMSszx=U{=<{aK54&}z~ z)2X*IWvghb>QE-Iej4Y)aX;-TCLA+$_oV;Ol%HbMB;~b|tq)H5Y37Kgd`VM&T~ls^ zS^rhsD1KULr&60+nohiWtj*Wh@>1gom=2VQmVIY1j8QwpbB{OZa62$H1;(w9)gd0J Ssnp@u;Jw;n+zhj|5B&p)GS55! delta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4F@oS>%bj}9Yit^9qkUAyGdf<(YWoIl zLR;X3hs6SfJS=xm*Uak4Z|j;@;0Mo5`QthV19M(i)~hYeZKqn>-7Rk(X_Irek7Idy h$6*5=gQAAg%+>cbA-~vAXTV($YO2IXVXo<^{{Z-Z;Y0uc diff --git a/main.c b/main.c index ec3f9f5..6c079f6 100755 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ #include "functions.h" -#include "cesar.h" +#include "caesar.h" #include "vigenere.h" int main(int argc, char *argv[]) { @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) { if(strcmp(argv[1], options[i]) == 0){ error++; } - } + } if(error == 0) { usage(); @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { fileSrc = argv[3]; - if(strcmp(hook, "crypt") == 0) sFile = sizeof(char) + strlen(fileSrc) + 5; + if(strcmp(hook, "crypt") == 0) sFile = sizeof(char) + strlen(fileSrc) + 5; else sFile = sizeof(char) + strlen(fileSrc) + 7; fileDst = malloc(sFile); @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) { fileDst[i++] = '.'; if(strcmp(hook, "decrypt") == 0){ fileDst[i++] = 'd'; - fileDst[i++] = 'e'; + fileDst[i++] = 'e'; } fileDst[i++] = 'c'; fileDst[i++] = 'r'; @@ -71,6 +71,7 @@ int main(int argc, char *argv[]) { buffer = malloc(sizeof(int) * numberCharacters); memset(buffer, 0, sizeof(int) * numberCharacters); + // Caesar cipher if(strcmp(argv[1], "-c") == 0){ // Get key int key; @@ -81,17 +82,17 @@ int main(int argc, char *argv[]) { }while(key < 1 || key > 26); if(strcmp(hook, "crypt") == 0) - error = cryptCesar(fileSrc, key, numberCharacters, buffer); + error = cryptCaesar(fileSrc, key, numberCharacters, buffer); if(strcmp(hook, "decrypt") == 0) - error = decryptCesar(fileSrc, key, numberCharacters, buffer); + error = decryptCaesar(fileSrc, key, numberCharacters, buffer); } else if(strcmp(argv[1], "-v") == 0){ // Get key char *key = malloc(8); printf("Your key: "); scanf("%s", key); - - + + if(strcmp(hook, "crypt") == 0) error = cryptVigenere(key, fileSrc, numberCharacters, buffer); diff --git a/test.txt.crypt b/test.txt.crypt index e69de29..4ff5606 100644 --- a/test.txt.crypt +++ b/test.txt.crypt @@ -0,0 +1,2 @@ +JGNNQ YQTNF! +LG UWKU WP OGUUCIG GP ENCKT !! diff --git a/test.txt.crypt.decrypt b/test.txt.crypt.decrypt deleted file mode 100644 index 4ab6fa4..0000000 --- a/test.txt.crypt.decrypt +++ /dev/null @@ -1,2 +0,0 @@ -HELLO WORLD! -JE SUIS UN MESSAGE EN CLAIR !!