Update caesar chipher

This commit is contained in:
Bucchino Geoffrey 2022-06-20 21:41:33 +02:00
parent e58fb770ad
commit ca47a6a637
8 changed files with 39 additions and 42 deletions

@ -1,4 +1,4 @@
#include "cesar.h" #include "caesar.h"
/* /*
s -> path source s -> path source
@ -6,17 +6,18 @@
key -> key of encryption key -> key of encryption
This function encrypt data 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; char *buffer;
int i; int i;
int error = countCharact; int error = countCharact;
char c = 0;
if(error == -1) return error; if(error == -1) return error;
/* Allocation dynamique */ /* Allocation dynamique */
buffer = malloc(countCharact + 1); /* + 1 for '\0' */ 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); error = copyFile(s, buffer);
if(error != 0) return error; 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; int val = 0;
/* For characters of A to Z */ /* For characters of A to Z */
if(buffer[i] >= 'A' && buffer[i] <= 'Z') { if(buffer[i] >= 'A' && buffer[i] <= 'Z') {
if(buffer[i] + key > 'Z') c = buffer[i] - 'A';
val = ((buffer[i] - 'A')-26) + key + 'A'; val = ((c + key) % 26) + 'A';
else
val = (buffer[i] - 'A') + key + 'A';
} }
/* For characters of 'a' to 'z' */ /* For characters of 'a' to 'z' */
else if(buffer[i] >= 'a' && buffer[i] <= 'z') { else if(buffer[i] >= 'a' && buffer[i] <= 'z') {
if(buffer[i] + key > 'z') c = buffer[i] - 'a';
val = ((buffer[i] - 'a')- 26) + key + 'A'; val = ((c + key) % 26) + 'A';
else
val = (buffer[i] - 'a') + key + 'A';
} }
/* For others characters */ /* For others characters */
else val = buffer[i]; else val = buffer[i];
bufferDst[i] = val; bufferDst[i] = val;
} }
bufferDst[i] = '\0'; bufferDst[i] = '\0';
/* Freedom the memory */ /* Freedom the memory */
free(buffer); free(buffer);
return error; return error;
} }
/* /*
@ -56,22 +53,22 @@ int cryptCesar(const char *s, const int key, const int countCharact, char *buffe
key -> key of decryption key -> key of decryption
This function decryption by Cesar 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; int error = countCharact;
char *buffer; char *buffer;
int i; int i;
if(error == -1) if(error == -1)
return error; return error;
/* Allocation dynamique */ /* Allocation dynamique */
buffer = malloc(countCharact + 1); /* +1 for '\0' */ 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); error = copyFile(s, buffer);
if(error != 0) return error; if(error != 0) return error;
/********* Decryption **********/ /********* Decryption **********/
for(i = 0; i < countCharact; i++) { for(i = 0; i < countCharact; i++) {
int val = 0; int val = 0;
/* buffer[i] >= 'A' AND buffer[i] <= 'Z' */ /* 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] = val;
} }
bufferDst[i] = '\0'; bufferDst[i] = '\0';
/* Freedom the memory */ /* Freedom the memory */
free(buffer); free(buffer);

12
caesar.h Executable file

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

13
cesar.h

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

@ -1,3 +1,3 @@
#!/bin/sh #!/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

BIN
main

Binary file not shown.

17
main.c

@ -1,5 +1,5 @@
#include "functions.h" #include "functions.h"
#include "cesar.h" #include "caesar.h"
#include "vigenere.h" #include "vigenere.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -18,7 +18,7 @@ int main(int argc, char *argv[]) {
if(strcmp(argv[1], options[i]) == 0){ if(strcmp(argv[1], options[i]) == 0){
error++; error++;
} }
} }
if(error == 0) { if(error == 0) {
usage(); usage();
@ -43,7 +43,7 @@ int main(int argc, char *argv[]) {
fileSrc = argv[3]; 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; else sFile = sizeof(char) + strlen(fileSrc) + 7;
fileDst = malloc(sFile); fileDst = malloc(sFile);
@ -55,7 +55,7 @@ int main(int argc, char *argv[]) {
fileDst[i++] = '.'; fileDst[i++] = '.';
if(strcmp(hook, "decrypt") == 0){ if(strcmp(hook, "decrypt") == 0){
fileDst[i++] = 'd'; fileDst[i++] = 'd';
fileDst[i++] = 'e'; fileDst[i++] = 'e';
} }
fileDst[i++] = 'c'; fileDst[i++] = 'c';
fileDst[i++] = 'r'; fileDst[i++] = 'r';
@ -71,6 +71,7 @@ int main(int argc, char *argv[]) {
buffer = malloc(sizeof(int) * numberCharacters); buffer = malloc(sizeof(int) * numberCharacters);
memset(buffer, 0, sizeof(int) * numberCharacters); memset(buffer, 0, sizeof(int) * numberCharacters);
// Caesar cipher
if(strcmp(argv[1], "-c") == 0){ if(strcmp(argv[1], "-c") == 0){
// Get key // Get key
int key; int key;
@ -81,17 +82,17 @@ int main(int argc, char *argv[]) {
}while(key < 1 || key > 26); }while(key < 1 || key > 26);
if(strcmp(hook, "crypt") == 0) if(strcmp(hook, "crypt") == 0)
error = cryptCesar(fileSrc, key, numberCharacters, buffer); error = cryptCaesar(fileSrc, key, numberCharacters, buffer);
if(strcmp(hook, "decrypt") == 0) if(strcmp(hook, "decrypt") == 0)
error = decryptCesar(fileSrc, key, numberCharacters, buffer); error = decryptCaesar(fileSrc, key, numberCharacters, buffer);
} }
else if(strcmp(argv[1], "-v") == 0){ else if(strcmp(argv[1], "-v") == 0){
// Get key // Get key
char *key = malloc(8); char *key = malloc(8);
printf("Your key: "); printf("Your key: ");
scanf("%s", key); scanf("%s", key);
if(strcmp(hook, "crypt") == 0) if(strcmp(hook, "crypt") == 0)
error = cryptVigenere(key, fileSrc, numberCharacters, buffer); error = cryptVigenere(key, fileSrc, numberCharacters, buffer);

@ -0,0 +1,2 @@
JGNNQ YQTNF!
LG UWKU WP OGUUCIG GP ENCKT !!

@ -1,2 +0,0 @@
HELLO WORLD!
JE SUIS UN MESSAGE EN CLAIR !!