Compare commits

...

19 Commits

Author SHA1 Message Date
gbucchino e3cde4be54 Update polybius 2024-10-01 20:01:09 +02:00
gbucchino 3a2e110ad2 Upgrade polybius 2024-09-29 18:30:53 +02:00
gbucchino c68c270dbf Update code 2024-09-26 11:34:17 +02:00
gbucchino 957d28ff06 Update code 2024-09-25 19:44:08 +02:00
gbucchino 52769a7985 Fix fill polybius 2024-09-25 12:03:08 +02:00
gbucchino 0242ed0256 Add polybius square 2024-09-24 19:56:20 +02:00
gbucchino 098a49429c Update gitignore 2024-09-24 14:28:10 +02:00
gbucchino 3131f79f73 Read file block by block 2024-09-24 14:25:00 +02:00
gbucchino 2490ef0087 Update code 2024-09-24 12:34:25 +02:00
gbucchino d715a0ad04 Update project 2023-10-02 18:35:36 +02:00
gbucchino a7ec6b3978 Move sources files 2023-09-25 19:39:35 +02:00
gbucchino 7cce139996 Replace by english message 2023-09-25 19:28:36 +02:00
gbucchino e0c4b67364 Update gitignore 2023-09-21 15:46:47 +02:00
gbucchino 5b6b663831 Add gitignore 2023-09-20 20:18:04 +02:00
gbucchino 50203b6203 Update text file 2022-11-16 20:13:21 +01:00
gbucchino ca47a6a637 Update caesar chipher 2022-06-20 21:41:33 +02:00
gbucchino e58fb770ad upd project 2022-03-11 11:17:55 +01:00
Geoffrey 478f35fe73 upd 2021-10-22 18:14:02 +02:00
gbucchino 80fc8763b0 upd vigenere 2021-10-21 20:08:14 +02:00
24 changed files with 865 additions and 252 deletions
+4
View File
@@ -0,0 +1,4 @@
main
examples/**crypt
examples/**decrypt
**swp
-95
View File
@@ -1,95 +0,0 @@
#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
View File
@@ -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
+139
View File
@@ -0,0 +1,139 @@
Ô saint prêtre ! grande âme ! oh ! je tombe à genoux !
Jeune, il avait encor de longs jours parmi nous,
Il n'en a pas compté le nombre ;
Il était à cet âge où le bonheur fleurit ;
Il a considéré la croix de Jésus-Christ
Toute rayonnante dans l'ombre.
Il a dit : — « C'est le Dieu de progrès et d'amour.
Jésus, qui voit ton front croit voir le front du jour.
Christ sourit à qui le repousse.
Puisqu'il est mort pour nous, je veux mourir pour lui ;
Dans son tombeau, dont j'ai la pierre pour appui,
Il m'appelle d'une voix douce.
« Sa doctrine est le ciel entr'ouvert ; par la main,
Comme un père l'enfant, il tient le genre humain ;
Par lui nous vivons et nous sommes ;
Au chevet des geôliers dormant dans leurs maisons,
Il dérobe les clefs de toutes les prisons
Et met en liberté les hommes.
« Or il est, loin de nous, une autre humanité
Qui ne le connaît point, et dans l'iniquité
Rampe enchaînée, et souffre et tombe ;
Ils font pour trouver Dieu de ténébreux efforts ;
Ils s'agitent en vain ; ils sont comme des morts
Qui tâtent le mur de leur tombe.
« Sans loi, sans but, sans guide, ils errent ici-bas.
Ils sont méchants, étant ignorants ; ils n'ont pas
Leur part de la grande conquête.
J'irai. Pour les sauver je quitte le saint lieu.
Ô mes frères, je viens vous apporter mon Dieu,
Je viens vous apporter ma tête ! » —
Prêtre, il s'est souvenu, calme en nos jours troublés,
De la parole dite aux apôtres : — Allez,
Bravez les bûchers et les claies ! —
Et de l'adieu du Christ au suprême moment :
— Ô vivant, aimez-vous ! aimez. En vous aimant,
Frères, vous fermerez mes plaies. —
Il s'est dit qu'il est bon d'éclairer dans leur nuit
Ces peuples égarés loin du progrès qui luit,
Dont l'âme est couverte de voiles ;
Puis il s'en est allé, dans les vents, dans les flots,
Vers les noirs chevalets et les sanglants billots,
Les yeux fixés sur les étoiles.
II.
Ceux vers qui cet apôtre allait, l'ont égorgé.
III.
Oh ! tandis que là-bas, hélas ! chez ces barbares,
S'étale l'échafaud de tes membres chargé,
Que le bourreau, rangeant ses glaives et ses barres,
Frotte au gibet son ongle où ton sang s'est figé ;
Ciel ! tandis que les chiens dans ce sang viennent boire,
Et que la mouche horrible, essaim au vol joyeux,
Comme dans une ruche entre en ta bouche noire
Et bourdonne au soleil dans les trous de tes yeux ;
Tandis qu'échevelée, et sans voix, sans paupières,
Ta tête blême est là sur un infâme pieu,
Livrée aux vils affronts, meurtrie à coups de pierres,
Ici, derrière toi, martyr, on vend ton Dieu !
Ce Dieu qui n'est qu'à toi, martyr, on te le vole !
On le livre à Mandrin, ce Dieu pour qui tu meurs !
Des hommes, comme toi revêtus de l'étole,
Pour être cardinaux, pour être sénateurs,
Des prêtres, pour avoir des palais, des carrosses,
Et des jardins l'été riant sous le ciel bleu,
Pour argenter leur mitre et pour dorer leurs crosses,
Pour boire de bon vin, assis près d'un bon feu,
Au forban dont la main dans le meurtre est trempée,
Au larron chargé d'or qui paye et qui sourit,
Grand Dieu ! retourne-toi vers nous, tête coupée !
Ils vendent Jésus-Christ ! ils vendent Jésus-Christ !
Ils livrent au bandit, pour quelques sacs sordides,
L'évangile, la loi, l'autel épouvanté,
Et la justice aux yeux sévères et candides,
Et l'étoile du coeur humain, la vérité !
Les bons jetés, vivants, au bagne, ou morts, aux fleuves,
L'homme juste proscrit par Cartouche Sylla,
L'innocent égorgé, le deuil sacré des veuves,
Les pleurs de l'orphelin, ils vendent tout cela !
Tout ! la foi, le serment que Dieu tient sous sa garde,
Le saint temple où, mourant, tu dis :Introïbo,
Ils livrent tout ! pudeur, vertu ! — martyr, regarde,
Rouvre tes yeux qu'emplit la lueur du tombeau ; —
Ils vendent l'arche auguste où l'hostie étincelle !
Ils vendent Christ, te dis-je ! et ses membres liés !
Ils vendent la sueur qui sur son front ruisselle,
Et les clous de ses mains, et les clous de ses pieds !
Ils vendent au brigand qui chez lui les attire
Le grand crucifié sur les hommes penché ;
Ils vendent sa parole, ils vendent son martyre,
Et ton martyre à toi par-dessus le marché !
Tant pour les coups de fouet qu'il reçut à la porte !
César ! tant pour l'amen, tant pour l'alléluia !
Tant pour la pierre où vint heurter sa tête morte !
Tant pour le drap rougi que sa barbe essuya !
Ils vendent ses genoux meurtris, sa palme verte,
Sa plaie au flanc, son oeil tout baigné d'infini,
Ses pleurs, son agonie, et sa bouche entrouverte,
Et le cri qu'il poussa : Lamma Sabacthani !
Ils vendent le sépulcre ! ils vendent les ténèbres !
Les séraphins chantant au seuil profond des cieux,
Et la mère debout sous l'arbre aux bras funèbres,
Qui, sentant là son fils, ne levait pas les yeux !
Oui, ces évêques, oui, ces marchands, oui, ces prêtres
A l'histrion du crime, assouvi, couronné,
A ce Néron repu qui rit parmi les traîtres,
Un pied sur Thraséas, un coude sur Phryné,
Au voleur qui tua les lois à coups de crosse,
Au pirate empereur Napoléon dernier,
Ivre deux fois, immonde encor plus que féroce,
Pourceau dans le cloaque et loup dans le charnier,
Ils vendent, ô martyr, le Dieu pensif et pâle
Qui, debout sur la terre et sous le firmament,
Triste et nous souriant dans notre nuit fatale,
Sur le noir Golgotha saigne éternellement !
File diff suppressed because one or more lines are too long
+139
View File
@@ -0,0 +1,139 @@
Ô SAINT PRêTRE ! GRANDE âME ! OH ! JE TOMBE à GENOUX !
JEUNE, IL AVAIT ENCOR DE LONGS JOURS PARMI NOUS,
IL N'EN A PAS COMPTé LE NOMBRE ;
IL éTAIT à CET âGE Où LE BONHEUR FLEURIT ;
IL A CONSIDéRé LA CROIX DE JéSUS-CHRIST
TOUTE RAYONNANTE DANS L'OMBRE.
IL A DIT : — « C'EST LE DIEU DE PROGRèS ET D'AMOUR.
JéSUS, QUI VOIT TON FRONT CROIT VOIR LE FRONT DU JOUR.
CHRIST SOURIT à QUI LE REPOUSSE.
PUISQU'IL EST MORT POUR NOUS, JE VEUX MOURIR POUR LUI ;
DANS SON TOMBEAU, DONT J'AI LA PIERRE POUR APPUI,
IL M'APPELLE D'UNE VOIX DOUCE.
« SA DOCTRINE EST LE CIEL ENTR'OUVERT ; PAR LA MAIN,
COMME UN PèRE L'ENFANT, IL TIENT LE GENRE HUMAIN ;
PAR LUI NOUS VIVONS ET NOUS SOMMES ;
AU CHEVET DES GEôLIERS DORMANT DANS LEURS MAISONS,
IL DéROBE LES CLEFS DE TOUTES LES PRISONS
ET MET EN LIBERTé LES HOMMES.
« OR IL EST, LOIN DE NOUS, UNE AUTRE HUMANITé
QUI NE LE CONNAîT POINT, ET DANS L'INIQUITé
RAMPE ENCHAîNéE, ET SOUFFRE ET TOMBE ;
ILS FONT POUR TROUVER DIEU DE TéNéBREUX EFFORTS ;
ILS S'AGITENT EN VAIN ; ILS SONT COMME DES MORTS
QUI TâTENT LE MUR DE LEUR TOMBE.
« SANS LOI, SANS BUT, SANS GUIDE, ILS ERRENT ICI-BAS.
ILS SONT MéCHANTS, éTANT IGNORANTS ; ILS N'ONT PAS
LEUR PART DE LA GRANDE CONQUêTE.
J'IRAI. POUR LES SAUVER JE QUITTE LE SAINT LIEU.
Ô MES FRèRES, JE VIENS VOUS APPORTER MON DIEU,
JE VIENS VOUS APPORTER MA TêTE ! » —
PRêTRE, IL S'EST SOUVENU, CALME EN NOS JOURS TROUBLéS,
DE LA PAROLE DITE AUX APôTRES : — ALLEZ,
BRAVEZ LES BûCHERS ET LES CLAIES ! —
ET DE L'ADIEU DU CHRIST AU SUPRêME MOMENT :
— Ô VIVANT, AIMEZ-VOUS ! AIMEZ. EN VOUS AIMANT,
FRèRES, VOUS FERMEREZ MES PLAIES. —
IL S'EST DIT QU'IL EST BON D'éCLAIRER DANS LEUR NUIT
CES PEUPLES éGARéS LOIN DU PROGRèS QUI LUIT,
DONT L'âME EST COUVERTE DE VOILES ;
PUIS IL S'EN EST ALLé, DANS LES VENTS, DANS LES FLOTS,
VERS LES NOIRS CHEVALETS ET LES SANGLANTS BILLOTS,
LES YEUX FIXéS SUR LES éTOILES.
II.
CEUX VERS QUI CET APôTRE ALLAIT, L'ONT éGORGé.
III.
OH ! TANDIS QUE Là-BAS, HéLAS ! CHEZ CES BARBARES,
S'éTALE L'éCHAFAUD DE TES MEMBRES CHARGé,
QUE LE BOURREAU, RANGEANT SES GLAIVES ET SES BARRES,
FROTTE AU GIBET SON ONGLE Où TON SANG S'EST FIGé ;
CIEL ! TANDIS QUE LES CHIENS DANS CE SANG VIENNENT BOIRE,
ET QUE LA MOUCHE HORRIBLE, ESSAIM AU VOL JOYEUX,
COMME DANS UNE RUCHE ENTRE EN TA BOUCHE NOIRE
ET BOURDONNE AU SOLEIL DANS LES TROUS DE TES YEUX ;
TANDIS QU'éCHEVELéE, ET SANS VOIX, SANS PAUPIèRES,
TA TêTE BLêME EST Là SUR UN INFâME PIEU,
LIVRéE AUX VILS AFFRONTS, MEURTRIE à COUPS DE PIERRES,
ICI, DERRIèRE TOI, MARTYR, ON VEND TON DIEU !
CE DIEU QUI N'EST QU'à TOI, MARTYR, ON TE LE VOLE !
ON LE LIVRE à MANDRIN, CE DIEU POUR QUI TU MEURS !
DES HOMMES, COMME TOI REVêTUS DE L'éTOLE,
POUR êTRE CARDINAUX, POUR êTRE SéNATEURS,
DES PRêTRES, POUR AVOIR DES PALAIS, DES CARROSSES,
ET DES JARDINS L'éTé RIANT SOUS LE CIEL BLEU,
POUR ARGENTER LEUR MITRE ET POUR DORER LEURS CROSSES,
POUR BOIRE DE BON VIN, ASSIS PRèS D'UN BON FEU,
AU FORBAN DONT LA MAIN DANS LE MEURTRE EST TREMPéE,
AU LARRON CHARGé D'OR QUI PAYE ET QUI SOURIT,
GRAND DIEU ! RETOURNE-TOI VERS NOUS, TêTE COUPéE !
ILS VENDENT JéSUS-CHRIST ! ILS VENDENT JéSUS-CHRIST !
ILS LIVRENT AU BANDIT, POUR QUELQUES SACS SORDIDES,
L'éVANGILE, LA LOI, L'AUTEL éPOUVANTé,
ET LA JUSTICE AUX YEUX SéVèRES ET CANDIDES,
ET L'éTOILE DU COEUR HUMAIN, LA VéRITé !
LES BONS JETéS, VIVANTS, AU BAGNE, OU MORTS, AUX FLEUVES,
L'HOMME JUSTE PROSCRIT PAR CARTOUCHE SYLLA,
L'INNOCENT éGORGé, LE DEUIL SACRé DES VEUVES,
LES PLEURS DE L'ORPHELIN, ILS VENDENT TOUT CELA !
TOUT ! LA FOI, LE SERMENT QUE DIEU TIENT SOUS SA GARDE,
LE SAINT TEMPLE Où, MOURANT, TU DIS :INTROïBO,
ILS LIVRENT TOUT ! PUDEUR, VERTU ! — MARTYR, REGARDE,
ROUVRE TES YEUX QU'EMPLIT LA LUEUR DU TOMBEAU ; —
ILS VENDENT L'ARCHE AUGUSTE Où L'HOSTIE éTINCELLE !
ILS VENDENT CHRIST, TE DIS-JE ! ET SES MEMBRES LIéS !
ILS VENDENT LA SUEUR QUI SUR SON FRONT RUISSELLE,
ET LES CLOUS DE SES MAINS, ET LES CLOUS DE SES PIEDS !
ILS VENDENT AU BRIGAND QUI CHEZ LUI LES ATTIRE
LE GRAND CRUCIFIé SUR LES HOMMES PENCHé ;
ILS VENDENT SA PAROLE, ILS VENDENT SON MARTYRE,
ET TON MARTYRE à TOI PAR-DESSUS LE MARCHé !
TANT POUR LES COUPS DE FOUET QU'IL REçUT à LA PORTE !
CéSAR ! TANT POUR L'AMEN, TANT POUR L'ALLéLUIA !
TANT POUR LA PIERRE Où VINT HEURTER SA TêTE MORTE !
TANT POUR LE DRAP ROUGI QUE SA BARBE ESSUYA !
ILS VENDENT SES GENOUX MEURTRIS, SA PALME VERTE,
SA PLAIE AU FLANC, SON OEIL TOUT BAIGNé D'INFINI,
SES PLEURS, SON AGONIE, ET SA BOUCHE ENTROUVERTE,
ET LE CRI QU'IL POUSSA : LAMMA SABACTHANI !
ILS VENDENT LE SéPULCRE ! ILS VENDENT LES TéNèBRES !
LES SéRAPHINS CHANTANT AU SEUIL PROFOND DES CIEUX,
ET LA MèRE DEBOUT SOUS L'ARBRE AUX BRAS FUNèBRES,
QUI, SENTANT Là SON FILS, NE LEVAIT PAS LES YEUX !
OUI, CES éVêQUES, OUI, CES MARCHANDS, OUI, CES PRêTRES
A L'HISTRION DU CRIME, ASSOUVI, COURONNé,
A CE NéRON REPU QUI RIT PARMI LES TRAîTRES,
UN PIED SUR THRASéAS, UN COUDE SUR PHRYNé,
AU VOLEUR QUI TUA LES LOIS à COUPS DE CROSSE,
AU PIRATE EMPEREUR NAPOLéON DERNIER,
IVRE DEUX FOIS, IMMONDE ENCOR PLUS QUE FéROCE,
POURCEAU DANS LE CLOAQUE ET LOUP DANS LE CHARNIER,
ILS VENDENT, ô MARTYR, LE DIEU PENSIF ET PâLE
QUI, DEBOUT SUR LA TERRE ET SOUS LE FIRMAMENT,
TRISTE ET NOUS SOURIANT DANS NOTRE NUIT FATALE,
SUR LE NOIR GOLGOTHA SAIGNE éTERNELLEMENT !
+16
View File
@@ -0,0 +1,16 @@
Let me not to the marriage of true minds
Admit impediments. Love is not love
Which alters when it alteration finds,
Or bends with the remover to remove:
O no! it is an ever-fixed mark
That looks on tempests and is never shaken;
It is the star to every wandering bark,
Whose worths unknown, although his height be taken.
Loves not Times fool, though rosy lips and cheeks
Within his bending sickles compass come:
Love alters not with his brief hours and weeks,
But bears it out even to the edge of doom.
If this be error and upon me proved,
I never writ, nor no man ever loved.
William Shakespeare
+1 -1
View File
@@ -1,3 +1,3 @@
#!/bin/sh
gcc -W main.c functions.c cesar.c vigenere.c -o main && ./main $1 $2 $3 $4
gcc -Wall src/main.c src/functions.c src/caesar.c src/vigenere.c src/polybius.c -o main && ./main $1 $2 $3 $4
BIN
View File
Binary file not shown.
-116
View File
@@ -1,116 +0,0 @@
#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;
}
Executable
+112
View File
@@ -0,0 +1,112 @@
#include "caesar.h"
/*
s -> path source
d -> path destination
key -> key of encryption
This function encrypt data
*/
int cryptCaesar(const char *s, const char *filenameDst, const int key) {
int i;
char c = 0;
FILE *f = NULL, *fDst = NULL;
char data[BUFFER_SIZE];
char dataEncrypted[BUFFER_SIZE];
int size = 0, end = 0;
// Open the file
f = fopen(s, "r+");
if (f == NULL) return -1;
fDst = fopen(filenameDst, "w");
if (fDst == NULL) {
fclose(f);
return -1;
}
// We are going to read the file block by block
do {
end = readFile(f, &size, data);
/********* Encryption **********/
for (i = 0; i < size; i++){
if (data[i] >= 'A' && data[i] <= 'Z'){
c = data[i] - 'A';
dataEncrypted[i] = ((c + key) % 26) + 'A';
}
else if (data[i] >= 'a' && data[i] <= 'z'){
c = data[i] - 'a';
dataEncrypted[i] = ((c + key) % 26) + 'A';
}
else dataEncrypted[i] = data[i];
// We can write into the dest file
if (dataEncrypted[i] != '\0')
fputc(dataEncrypted[i], fDst);
}
size = 0;
memset(data, 0, BUFFER_SIZE);
memset(dataEncrypted, 0, BUFFER_SIZE);
}while(end != 1);
/* Close files */
fclose(f);
fclose(fDst);
return 0;
}
/*
s -> path source
d -> path destination
key -> key of decryption
This function decryption by Cesar
*/
int decryptCaesar(const char *s, const char *filenameDst, const int key) {
int i;
FILE *f = NULL, *fDst = NULL;
char data[BUFFER_SIZE];
char dataDecrypted[BUFFER_SIZE];
int size = 0, end = 0;
// Open the file
f = fopen(s, "r+");
if (f == NULL) return -1;
fDst = fopen(filenameDst, "w");
if (fDst == NULL) {
fclose(f);
return -1;
}
// We are going to read the file block by block
do {
end = readFile(f, &size, data);
/********* Decryption **********/
for (i = 0; i < size; i++){
int val = 0;
//if (data[i] >= 'A' && data[i] <= 'Z'){
if(data[i] >= 65 && data[i] <= 90) {
val = data[i] - key;
if (val < 'A')
val = val + 26;
}
else val = data[i];
dataDecrypted[i] = val;
// We can write into the dest file
if (dataDecrypted[i] != '\0')
fputc(dataDecrypted[i], fDst);
}
size = 0;
memset(data, 0, BUFFER_SIZE);
memset(dataDecrypted, 0, BUFFER_SIZE);
}while(end != 1);
/* Close files */
fclose(f);
fclose(fDst);
return 0;
}
Executable
+12
View File
@@ -0,0 +1,12 @@
#ifndef H_INFLATE
#define H_INFLATE
#define SIZE_MAX_FILE 8192
#include "functions.h"
/* Functions */
int cryptCaesar(const char *, const char *, const int);
int decryptCaesar(const char *, const char *, const int);
#endif
+30 -6
View File
@@ -3,28 +3,52 @@
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");
printf("\t-c: Caesar cipher\n");
printf("\t-v: Vigenere cipher\n");
printf("\t-t: Transposition cipher\n");
printf("\t-s: Polybius square\n");
}
int fileExist(const char *path) {
FILE *f=NULL;
f=fopen(path,"r");
if(f==NULL){
if(f==NULL)
return -1;
}
fclose(f);
return 0;
}
/*
This function read the file block by block until the end of it
*/
int readFile(FILE *f, int *size, char *buffer){
int index = 0;
char c;
int end = 0;
do{
c = fgetc(f);
buffer[index] = c;
index++;
*size += 1;
// End of the file, we stop it
if (c == EOF){
buffer[index - 1] = '\0';
index = BUFFER_SIZE;
end = 1;
}
}while(index < BUFFER_SIZE);
return end;
}
/*
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;
FILE *f = fopen(path, "r+");
if(f == NULL) return -1;
+3
View File
@@ -5,8 +5,11 @@
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 4096
/* Functions */
void usage();
int readFile(FILE *, int *, char *);
int fileExist(const char *path);
int copyFile(const char *path, char *buffer);
int addDataToFile(const char *data, const char *path);
Executable
+133
View File
@@ -0,0 +1,133 @@
#include "functions.h"
#include "caesar.h"
#include "vigenere.h"
#include "polybius.h"
int main(int argc, char *argv[]) {
int error = 0;
char *options[4] = {"-c","-v","-t","-s"};
char *hook, *fileSrc, *fileDst, *buffer = NULL;
size_t sFile, i;
if(argc != 4) {
usage();
return -1;
}
// Check kind of encryption
for(i = 0; i < 4; 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';
// Caesar cipher
if(strcmp(argv[1], "-c") == 0){
// Get key
int key;
int res;
do{
printf("Your key (between 1 and 26): ");
scanf("%d",&key);
}while(key < 1 || key > 26);
if(strcmp(hook, "crypt") == 0)
res = cryptCaesar(fileSrc, fileDst, key);
if(strcmp(hook, "decrypt") == 0)
res = decryptCaesar(fileSrc, fileDst, key);
if (res != 0)
printf("Failed to encrypt/decrypt the file");
}
// Vigenere
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, 0, buffer);
free(key);
}
else if(strcmp(argv[1], "-t") == 0){
// Get key
}
// Polybius square
else if(strcmp(argv[1], "-s") == 0){
char key[26];
int res;
printf("Your key: ");
scanf("%s", key);
if (strlen(key) > 26){
printf("Your key is too big. Choose a small one\n");
return -1;
}
if(strcmp(hook, "crypt") == 0)
res = cryptPolybius(fileSrc, fileDst, key);
else
res = decryptPolybius(fileSrc, fileDst, key);
if (res != 0)
printf("Failed to crypt/decrypt the file\n");
}
/* Display error */
/*
* if(error == 0) err(error, fileDst);
* else if(error == -1) err(error, fileSrc);
* else if(error == -2) err(error, fileDst);
*/
free(fileDst);
return 0;
}
+174
View File
@@ -0,0 +1,174 @@
#include "polybius.h"
int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *key){
char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE];
FILE *f = NULL, *fDst = NULL;
char data[BUFFER_SIZE];
char dataEncrypted[BUFFER_SIZE];
int size = 0, end = 0, i = 0, j = 0;
char c = 0;
generateSquarePolybius(polybius, key);
for (i = 0; i < POLYBIUS_SIZE; i++){
for (j = 0; j < POLYBIUS_SIZE; j++)
printf("%c ", polybius[i][j]);
printf("\n");
}
// Open the file
f = fopen(filenameSrc, "r+");
if (f == NULL) return -1;
fDst = fopen(filenameDst, "w");
if (fDst == NULL) {
fclose(f);
return -1;
}
do {
end = readFile(f, &size, data);
/********* Encryption **********/
for (i = 0; i < size; i++){
if (data[i] >= 'a' && data[i] <= 'z')
c = data[i] - 32;
else c = data[i];
for (j = 0; j < POLYBIUS_SIZE; j++){
for (int x = 0; x < POLYBIUS_SIZE; x++){
if (c == polybius[j][x]){
fputc(j + '0', fDst);
fputc(x + '0', fDst);
}
}
}
if (c == '\n' || c == ' '){
fputc(c, fDst);
fputc(0, fDst); // For having a couple of number
}
}
size = 0;
memset(data, 0, BUFFER_SIZE);
memset(dataEncrypted, 0, BUFFER_SIZE);
}while(end != 1);
/* Close files */
fclose(f);
fclose(fDst);
return 0;
}
int decryptPolybius(const char *filenameSrc, const char *filenameDst, const char *key){
char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE];
FILE *f = NULL, *fDst = NULL;
char data[BUFFER_SIZE];
char dataEncrypted[BUFFER_SIZE];
int size = 0, end = 0, i = 0, j = 0;
generateSquarePolybius(polybius, key);
for (i = 0; i < POLYBIUS_SIZE; i++){
for (j = 0; j < POLYBIUS_SIZE; j++)
printf("%c ", polybius[i][j]);
printf("\n");
}
// Open the file
f = fopen(filenameSrc, "r+");
if (f == NULL) return -1;
fDst = fopen(filenameDst, "w");
if (fDst == NULL) {
fclose(f);
return -1;
}
do {
end = readFile(f, &size, data);
/********* Decryption **********/
for (i = 0; i < size; i += 2){
if (data[i] >= 48 && data[i] <= 53) {
int c1 = data[i] - '0';
int c2 = data[i+1] - '0';
char c = polybius[c1][c2];
printf("%c", c);
fputc(c, fDst);
}
else{
printf("%c", data[i]);
fputc(data[i], fDst);
}
}
size = 0;
memset(data, 0, BUFFER_SIZE);
memset(dataEncrypted, 0, BUFFER_SIZE);
}while(end != 1);
/* Close files */
fclose(f);
fclose(fDst);
return 0;
}
/*
This function fill the polybius square
*/
static void fillSquare(int pos, int *posPolybius, char *p, char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE]){
for (int i = 0; i < POLYBIUS_SIZE; i++){
polybius[pos][i] = p[*(posPolybius)];
*posPolybius = *posPolybius + 1;
}
}
static int findKeyInSquare(const char *polybius, char c){
// We exclude the 'J' character
for (int i = 0; i < ALPHABET_SIZE - 1; i++)
if (c == polybius[i])
return 1;
return 0;
}
/*
This function generate the polybius square
*/
void generateSquarePolybius(char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE], const char *key){
char a[ALPHABET_SIZE - 1] = {0}; // The size is 25, we exclude the J character
int pos = 0;
for (int i = 0; i < strlen(key); i++){
char k;
if (key[i] >= 'a' && key[i] <= 'z')
k = key[i] - 32; // 32 = 26 + special characters
else k = key[i];
int res = findKeyInSquare(a, k);
if (res == 0)
a[pos++] = k;
}
int n = 0;
int t = pos;
for (int i = pos; i < ALPHABET_SIZE + pos; i++){
char c = 'A' + n;
if (c == 'J'){
// We check if I is in the array, if yes, we do not add it
}
/*if (c == 'I'){
// We check if J is in the array, is yes, we do not add it
int res = findKeyInSquare(a, 'J');
// J not in the array, we add it
if (res == 0){
a[t++] = 'A';
}
}*/
if (c != 'J'){
int res = findKeyInSquare(a, c);
if (res == 0)
a[t++] = c;
}
n++;
}
pos = 0;
fillSquare(0, &pos, a, polybius);
fillSquare(1, &pos, a, polybius);
fillSquare(2, &pos, a, polybius);
fillSquare(3, &pos, a, polybius);
fillSquare(4, &pos, a, polybius);
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef H_POLYBIUS
#define H_POLYBIUS
#include <stdio.h>
#include <string.h>
#include "functions.h"
#define POLYBIUS_SIZE 5
#define ALPHABET_SIZE 26
int cryptPolybius(const char *, const char *, const char *);
int decryptPolybius(const char *, const char *, const char *);
void generateSquarePolybius(char polybius[5][5], const char *);
#endif
+76
View File
@@ -0,0 +1,76 @@
#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;
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");
}
}
+10
View File
@@ -0,0 +1,10 @@
#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
View File
@@ -1,2 +0,0 @@
Hello world!
Je suis un message en clair !!
View File
-2
View File
@@ -1,2 +0,0 @@
HELLO WORLD!
JE SUIS UN MESSAGE EN CLAIR !!
-9
View File
@@ -1,9 +0,0 @@
#include "vigenere.h"
int cryptVigenere(const char *key, const char *data, char *bufferDst) {
int error = 0;
printf("Vigenere\n");
return error;
}
-8
View File
@@ -1,8 +0,0 @@
#ifndef H_VIGENERE
#define H_VIGENERE
#include "functions.h"
int cryptVigenere(const char *key, const char *data, char *bufferDst);
#endif