Compare commits
No commits in common. "d715a0ad04a2bb47b43e8ada3e3e79f4a61c1cd9" and "f21294b03b47e870990c10d76bf6ed999e6fb1f0" have entirely different histories.
d715a0ad04
...
f21294b03b
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,5 +0,0 @@
|
|||||||
**.txt.crypt
|
|
||||||
**.txt.decrypt
|
|
||||||
**.txt.**
|
|
||||||
**.txt
|
|
||||||
main
|
|
@ -1,4 +1,4 @@
|
|||||||
#include "caesar.h"
|
#include "cesar.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
s -> path source
|
s -> path source
|
||||||
@ -6,18 +6,17 @@
|
|||||||
key -> key of encryption
|
key -> key of encryption
|
||||||
This function encrypt data
|
This function encrypt data
|
||||||
*/
|
*/
|
||||||
int cryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst) {
|
int cryptCesar(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 into the buffer[] */
|
/* Copy the data of file in the buffer[] */
|
||||||
error = copyFile(s, buffer);
|
error = copyFile(s, buffer);
|
||||||
if(error != 0) return error;
|
if(error != 0) return error;
|
||||||
|
|
||||||
@ -26,25 +25,29 @@ int cryptCaesar(const char *s, const int key, const int countCharact, char *buff
|
|||||||
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') {
|
||||||
c = buffer[i] - 'A';
|
if(buffer[i] + key > 'Z')
|
||||||
val = ((c + key) % 26) + 'A';
|
val = ((buffer[i] - 'A')-26) + key + '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') {
|
||||||
c = buffer[i] - 'a';
|
if(buffer[i] + key > 'z')
|
||||||
val = ((c + key) % 26) + 'A';
|
val = ((buffer[i] - 'a')- 26) + key + '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;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
@ -53,22 +56,22 @@ int cryptCaesar(const char *s, const int key, const int countCharact, char *buff
|
|||||||
key -> key of decryption
|
key -> key of decryption
|
||||||
This function decryption by Cesar
|
This function decryption by Cesar
|
||||||
*/
|
*/
|
||||||
int decryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst) {
|
int decryptCesar(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 into buffer */
|
/* Copy the data from file to 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' */
|
||||||
@ -84,7 +87,7 @@ int decryptCaesar(const char *s, const int key, const int countCharact, char *bu
|
|||||||
bufferDst[i] = val;
|
bufferDst[i] = val;
|
||||||
}
|
}
|
||||||
bufferDst[i] = '\0';
|
bufferDst[i] = '\0';
|
||||||
|
|
||||||
/* Freedom the memory */
|
/* Freedom the memory */
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
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
|
2
exec.sh
2
exec.sh
@ -1,3 +1,3 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
gcc -W src/main.c src/functions.c src/caesar.c src/vigenere.c -o output/main && ./output/main $1 $2 $3 $4
|
gcc -W main.c functions.c cesar.c vigenere.c -o main && ./main $1 $2 $3 $4
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
void usage() {
|
void usage() {
|
||||||
printf("Usage: [options] [crypt|decrypt] [file]\n");
|
printf("Usage: [options] [crypt|decrypt] [file]\n");
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf("\t-c: Caesar cipher\n");
|
printf("\t-c: chiffrement de Cesar\n");
|
||||||
printf("\t-v: Vigenere cipher\n");
|
printf("\t-v: chiffrement de Vigenere\n");
|
||||||
printf("\t-t: Transposition cipher\n");
|
printf("\t-t: chiffrement par transposition\n");
|
||||||
}
|
}
|
||||||
int fileExist(const char *path) {
|
int fileExist(const char *path) {
|
||||||
FILE *f=NULL;
|
FILE *f=NULL;
|
BIN
main
Executable file
BIN
main
Executable file
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
#include "functions.h"
|
#include "functions.h"
|
||||||
#include "caesar.h"
|
#include "cesar.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,30 +71,28 @@ 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;
|
||||||
|
|
||||||
do{
|
do{
|
||||||
printf("Your key (between 1 and 26): ");
|
printf("Votre cle de chiffrement (entre 1 et 26): ");
|
||||||
scanf("%d",&key);
|
scanf("%d",&key);
|
||||||
}while(key < 1 || key > 26);
|
}while(key < 1 || key > 26);
|
||||||
|
|
||||||
if(strcmp(hook, "crypt") == 0)
|
if(strcmp(hook, "crypt") == 0)
|
||||||
error = cryptCaesar(fileSrc, key, numberCharacters, buffer);
|
error = cryptCesar(argv[3], key, numberCharacters, buffer);
|
||||||
if(strcmp(hook, "decrypt") == 0)
|
if(strcmp(hook, "decrypt") == 0)
|
||||||
error = decryptCaesar(fileSrc, key, numberCharacters, buffer);
|
error = decryptCesar(argv[3], 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: ");
|
|
||||||
scanf("%s", key);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(strcmp(hook, "crypt") == 0)
|
if(strcmp(hook, "crypt") == 0)
|
||||||
error = cryptVigenere(key, fileSrc, numberCharacters, buffer);
|
error = cryptVigenere(key, argv[3], buffer);
|
||||||
|
|
||||||
free(key);
|
free(key);
|
||||||
}
|
}
|
12
src/caesar.h
12
src/caesar.h
@ -1,12 +0,0 @@
|
|||||||
#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
|
|
@ -1,77 +0,0 @@
|
|||||||
#include "vigenere.h"
|
|
||||||
|
|
||||||
int cryptVigenere(const char *key, const char *fileSrc, const int numberCharacters, char *bufferDst) {
|
|
||||||
int error = 0;
|
|
||||||
char matrice[SIZE_MATRICE_VIGENERE][SIZE_MATRICE_VIGENERE];
|
|
||||||
char *data = malloc(sizeof(char) * numberCharacters);
|
|
||||||
char *dataVigenere = NULL;
|
|
||||||
char c = 0;
|
|
||||||
int pos = 0;
|
|
||||||
size_t i, j = 0;
|
|
||||||
size_t keyLength = strlen(key);
|
|
||||||
|
|
||||||
// Init the Vigenere matrice
|
|
||||||
matriceVigenere(matrice);
|
|
||||||
|
|
||||||
if (data == NULL) return -1;
|
|
||||||
memset(data, 0, numberCharacters);
|
|
||||||
|
|
||||||
error = copyFile(fileSrc, data);
|
|
||||||
|
|
||||||
dataVigenere = malloc(strlen(data));
|
|
||||||
if (dataVigenere == NULL) return -1;
|
|
||||||
|
|
||||||
memset(dataVigenere, 0, numberCharacters);
|
|
||||||
|
|
||||||
// Fill the dataVigenere with the key
|
|
||||||
for (i = 0; i < strlen(data); i++){
|
|
||||||
if (data[i] == '\n' || data[i] == ' ')
|
|
||||||
dataVigenere[i] = data[i];
|
|
||||||
else
|
|
||||||
dataVigenere[i] = key[j++];
|
|
||||||
|
|
||||||
if (j == keyLength)
|
|
||||||
j = 0;
|
|
||||||
}
|
|
||||||
dataVigenere[pos - 1] = '\0';
|
|
||||||
|
|
||||||
printf("%s\n", data);
|
|
||||||
printf("%s\n", dataVigenere);
|
|
||||||
/*do {
|
|
||||||
c = data[pos++];
|
|
||||||
printf("%c", c);
|
|
||||||
if (c == '\n') {}
|
|
||||||
}while (c != '\0');*/
|
|
||||||
|
|
||||||
/* Encrypt the data */
|
|
||||||
/*for (i = 0; i < strlen(data); i++) {
|
|
||||||
printf("%c", matrice[data[i]][dataVigenere[i]]);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
free(dataVigenere);
|
|
||||||
free(data);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
void matriceVigenere(char matrice[][SIZE_MATRICE_VIGENERE]) {
|
|
||||||
int i, j = 0;
|
|
||||||
char pos, pos2 = 'A';
|
|
||||||
|
|
||||||
//for (i = 0; i < SIZE_MATRICE_VIGENERE; i++)
|
|
||||||
// matrice[0][i] = 'A' + i;
|
|
||||||
|
|
||||||
for (i = 0; i < SIZE_MATRICE_VIGENERE; i++) {
|
|
||||||
for (j = 0; j < SIZE_MATRICE_VIGENERE; j++) {
|
|
||||||
if (pos > 'Z')
|
|
||||||
matrice[i][j] = pos2++;
|
|
||||||
else
|
|
||||||
matrice[i][j] = pos++;
|
|
||||||
}
|
|
||||||
pos = 'B' + i;
|
|
||||||
pos2 = 'A';
|
|
||||||
}
|
|
||||||
for (i = 0; i < SIZE_MATRICE_VIGENERE; i++) {
|
|
||||||
for (j = 0; j < SIZE_MATRICE_VIGENERE; j++)
|
|
||||||
printf("%c ", matrice[i][j]);
|
|
||||||
printf("f\n");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
#ifndef H_VIGENERE
|
|
||||||
#define H_VIGENERE
|
|
||||||
|
|
||||||
#include "functions.h"
|
|
||||||
#define SIZE_MATRICE_VIGENERE 26
|
|
||||||
|
|
||||||
int cryptVigenere(const char *key, const char *fileSrc, const int numberCharacters, char *bufferDst);
|
|
||||||
void matriceVigenere(char matrice[][SIZE_MATRICE_VIGENERE]);
|
|
||||||
|
|
||||||
#endif
|
|
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