Initial commit
This commit is contained in:
commit
f21294b03b
95
cesar.c
Executable file
95
cesar.c
Executable file
@ -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;
|
||||
}
|
13
cesar.h
Executable file
13
cesar.h
Executable file
@ -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
|
3
exec.sh
Executable file
3
exec.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
gcc -W main.c functions.c cesar.c vigenere.c -o main && ./main $1 $2 $3 $4
|
103
functions.c
Executable file
103
functions.c
Executable file
@ -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;
|
||||
}
|
||||
}
|
16
functions.h
Executable file
16
functions.h
Executable file
@ -0,0 +1,16 @@
|
||||
#ifndef H_FUNCTIONS
|
||||
#define H_FUNCTIONS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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
|
BIN
main
Executable file
BIN
main
Executable file
Binary file not shown.
116
main.c
Executable file
116
main.c
Executable file
@ -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;
|
||||
}
|
2
test.txt
Executable file
2
test.txt
Executable file
@ -0,0 +1,2 @@
|
||||
Hello world!
|
||||
Je suis un message en clair !!
|
0
test.txt.crypt
Normal file
0
test.txt.crypt
Normal file
2
test.txt.crypt.decrypt
Normal file
2
test.txt.crypt.decrypt
Normal file
@ -0,0 +1,2 @@
|
||||
HELLO WORLD!
|
||||
JE SUIS UN MESSAGE EN CLAIR !!
|
9
vigenere.c
Normal file
9
vigenere.c
Normal file
@ -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;
|
||||
}
|
8
vigenere.h
Normal file
8
vigenere.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef H_VIGENERE
|
||||
#define H_VIGENERE
|
||||
|
||||
#include "functions.h"
|
||||
|
||||
int cryptVigenere(const char *key, const char *data, char *bufferDst);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user