Compare commits

...

9 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
14 changed files with 705 additions and 161 deletions
+3 -4
View File
@@ -1,5 +1,4 @@
**.txt.crypt
**.txt.decrypt
**.txt.**
**.txt
main main
examples/**crypt
examples/**decrypt
**swp
+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 #!/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 -Wall src/main.c src/functions.c src/caesar.c src/vigenere.c src/polybius.c -o main && ./main $1 $2 $3 $4
+71 -51
View File
@@ -1,51 +1,60 @@
#include "caesar.h" #include "caesar.h"
/* /*
s -> path source s -> path source
d -> path destination d -> path destination
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 cryptCaesar(const char *s, const char *filenameDst, const int key) {
char *buffer;
int i; int i;
int error = countCharact;
char c = 0; char c = 0;
FILE *f = NULL, *fDst = NULL;
char data[BUFFER_SIZE];
char dataEncrypted[BUFFER_SIZE];
int size = 0, end = 0;
if(error == -1) return error; // Open the file
f = fopen(s, "r+");
if (f == NULL) return -1;
/* Allocation dynamique */ fDst = fopen(filenameDst, "w");
buffer = malloc(countCharact + 1); /* + 1 for '\0' */ if (fDst == NULL) {
fclose(f);
return -1;
}
/* Copy the data of file into the buffer[] */ // We are going to read the file block by block
error = copyFile(s, buffer); do {
if(error != 0) return error; end = readFile(f, &size, data);
/********* Encryption **********/ /********* Encryption **********/
for(i = 0; i < countCharact; i++) { for (i = 0; i < size; i++){
int val = 0; if (data[i] >= 'A' && data[i] <= 'Z'){
/* For characters of A to Z */ c = data[i] - 'A';
if(buffer[i] >= 'A' && buffer[i] <= 'Z') { dataEncrypted[i] = ((c + key) % 26) + 'A';
c = buffer[i] - 'A';
val = ((c + key) % 26) + 'A';
} }
/* For characters of 'a' to 'z' */ else if (data[i] >= 'a' && data[i] <= 'z'){
else if(buffer[i] >= 'a' && buffer[i] <= 'z') { c = data[i] - 'a';
c = buffer[i] - 'a'; dataEncrypted[i] = ((c + key) % 26) + 'A';
val = ((c + key) % 26) + 'A';
} }
/* For others characters */ else dataEncrypted[i] = data[i];
else val = buffer[i];
bufferDst[i] = val; // We can write into the dest file
if (dataEncrypted[i] != '\0')
fputc(dataEncrypted[i], fDst);
} }
size = 0;
bufferDst[i] = '\0'; memset(data, 0, BUFFER_SIZE);
memset(dataEncrypted, 0, BUFFER_SIZE);
}while(end != 1);
/* Freedom the memory */ /* Close files */
free(buffer); fclose(f);
fclose(fDst);
return error; return 0;
} }
/* /*
s -> path source s -> path source
@@ -53,40 +62,51 @@ 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 decryptCaesar(const char *s, const char *filenameDst, const int key) {
int error = countCharact;
char *buffer;
int i; int i;
FILE *f = NULL, *fDst = NULL;
char data[BUFFER_SIZE];
char dataDecrypted[BUFFER_SIZE];
int size = 0, end = 0;
if(error == -1) // Open the file
return error; f = fopen(s, "r+");
if (f == NULL) return -1;
/* Allocation dynamique */ fDst = fopen(filenameDst, "w");
buffer = malloc(countCharact + 1); /* +1 for '\0' */ if (fDst == NULL) {
fclose(f);
return -1;
}
/* Copy the data from file into buffer */ // We are going to read the file block by block
error = copyFile(s, buffer); do {
if(error != 0) return error; end = readFile(f, &size, data);
/********* Decryption **********/ /********* Decryption **********/
for(i = 0; i < countCharact; i++) { for (i = 0; i < size; i++){
int val = 0; int val = 0;
/* buffer[i] >= 'A' AND buffer[i] <= 'Z' */ //if (data[i] >= 'A' && data[i] <= 'Z'){
if(buffer[i] >= 65 && buffer[i] <= 90) { if(data[i] >= 65 && data[i] <= 90) {
val = buffer[i] - key; val = data[i] - key;
/* val < 'A' */ if (val < 'A')
if(val < 65)
val = val + 26; val = val + 26;
} }
else else val = data[i];
val = buffer[i]; dataDecrypted[i] = val;
// We can write into the dest file
bufferDst[i] = val; if (dataDecrypted[i] != '\0')
fputc(dataDecrypted[i], fDst);
} }
bufferDst[i] = '\0'; size = 0;
/* Freedom the memory */ memset(data, 0, BUFFER_SIZE);
free(buffer); memset(dataDecrypted, 0, BUFFER_SIZE);
}while(end != 1);
return error; /* Close files */
fclose(f);
fclose(fDst);
return 0;
} }
+2 -2
View File
@@ -6,7 +6,7 @@
#include "functions.h" #include "functions.h"
/* Functions */ /* Functions */
int cryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst); int cryptCaesar(const char *, const char *, const int);
int decryptCaesar(const char *s, const int key, const int countCharact, char *bufferDst); int decryptCaesar(const char *, const char *, const int);
#endif #endif
+27 -3
View File
@@ -6,25 +6,49 @@ void usage() {
printf("\t-c: Caesar cipher\n"); printf("\t-c: Caesar cipher\n");
printf("\t-v: Vigenere cipher\n"); printf("\t-v: Vigenere cipher\n");
printf("\t-t: Transposition cipher\n"); printf("\t-t: Transposition cipher\n");
printf("\t-s: Polybius square\n");
} }
int fileExist(const char *path) { int fileExist(const char *path) {
FILE *f=NULL; FILE *f=NULL;
f=fopen(path,"r"); f=fopen(path,"r");
if(f==NULL){ if(f==NULL)
return -1; return -1;
}
fclose(f); fclose(f);
return 0; 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 path -> path source
This function copy all characters of the file in the buffer This function copy all characters of the file in the buffer
*/ */
int copyFile(const char *path, char *buffer) { int copyFile(const char *path, char *buffer) {
FILE *f = fopen(path, "r+");
int caract = 0; int caract = 0;
int i = 0; int i = 0;
FILE *f = fopen(path, "r+");
if(f == NULL) return -1; if(f == NULL) return -1;
+3
View File
@@ -5,8 +5,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define BUFFER_SIZE 4096
/* Functions */ /* Functions */
void usage(); void usage();
int readFile(FILE *, int *, char *);
int fileExist(const char *path); int fileExist(const char *path);
int copyFile(const char *path, char *buffer); int copyFile(const char *path, char *buffer);
int addDataToFile(const char *data, const char *path); int addDataToFile(const char *data, const char *path);
+35 -20
View File
@@ -1,24 +1,24 @@
#include "functions.h" #include "functions.h"
#include "caesar.h" #include "caesar.h"
#include "vigenere.h" #include "vigenere.h"
#include "polybius.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int error = 0; int error = 0;
int numberCharacters = 0; char *options[4] = {"-c","-v","-t","-s"};
char *options[3] = {"-c","-v","-t"}; char *hook, *fileSrc, *fileDst, *buffer = NULL;
char *hook, *fileSrc, *fileDst, *buffer;
size_t sFile, i; size_t sFile, i;
if(argc != 4) { if(argc != 4) {
usage(); usage();
return -1;
} }
// Check options // Check kind of encryption
for(i = 0; i < 3; i++){ for(i = 0; i < 4; i++){
if(strcmp(argv[1], options[i]) == 0){ if(strcmp(argv[1], options[i]) == 0)
error++; error++;
} }
}
if(error == 0) { if(error == 0) {
usage(); usage();
@@ -64,17 +64,11 @@ int main(int argc, char *argv[]) {
fileDst[i++] = 't'; fileDst[i++] = 't';
fileDst[i++] = '\0'; 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);
// Caesar cipher // Caesar cipher
if(strcmp(argv[1], "-c") == 0){ if(strcmp(argv[1], "-c") == 0){
// Get key // Get key
int key; int key;
int res;
do{ do{
printf("Your key (between 1 and 26): "); printf("Your key (between 1 and 26): ");
@@ -82,10 +76,14 @@ 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 = cryptCaesar(fileSrc, key, numberCharacters, buffer); res = cryptCaesar(fileSrc, fileDst, key);
if(strcmp(hook, "decrypt") == 0) if(strcmp(hook, "decrypt") == 0)
error = decryptCaesar(fileSrc, key, numberCharacters, buffer); res = decryptCaesar(fileSrc, fileDst, key);
if (res != 0)
printf("Failed to encrypt/decrypt the file");
} }
// Vigenere
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);
@@ -94,16 +92,34 @@ int main(int argc, char *argv[]) {
if(strcmp(hook, "crypt") == 0) if(strcmp(hook, "crypt") == 0)
error = cryptVigenere(key, fileSrc, numberCharacters, buffer); error = cryptVigenere(key, fileSrc, 0, buffer);
free(key); free(key);
} }
else if(strcmp(argv[1], "-t") == 0){ else if(strcmp(argv[1], "-t") == 0){
// Get key // Get key
} }
// Polybius square
else if(strcmp(argv[1], "-s") == 0){
char key[26];
int res;
// Add data to file printf("Your key: ");
addDataToFile(buffer, fileDst); 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 */ /* Display error */
/* /*
@@ -113,6 +129,5 @@ int main(int argc, char *argv[]) {
*/ */
free(fileDst); free(fileDst);
free(buffer);
return 0; 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
-1
View File
@@ -5,7 +5,6 @@ int cryptVigenere(const char *key, const char *fileSrc, const int numberCharacte
char matrice[SIZE_MATRICE_VIGENERE][SIZE_MATRICE_VIGENERE]; char matrice[SIZE_MATRICE_VIGENERE][SIZE_MATRICE_VIGENERE];
char *data = malloc(sizeof(char) * numberCharacters); char *data = malloc(sizeof(char) * numberCharacters);
char *dataVigenere = NULL; char *dataVigenere = NULL;
char c = 0;
int pos = 0; int pos = 0;
size_t i, j = 0; size_t i, j = 0;
size_t keyLength = strlen(key); size_t keyLength = strlen(key);