1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAILLE_MAX_PATH 50
#define TAILLE_MAX_CLE 20
int xor(FILE *file1, FILE *file2, char cle[]);
int main(int argc, char *argv[])
{
FILE *file1 = NULL;
FILE *file2 = NULL;
char path1[TAILLE_MAX_PATH];
char path2[TAILLE_MAX_PATH];
char cleDeCryptage[TAILLE_MAX_CLE];
printf("Nom du fichier %c crypter ou %c d%ccrypter : ",
'\x85','\x85','\x82');
scanf("%s", path1);
file1 = fopen(path1, "rb");
if(file1 == NULL)
{
printf("\nErreur : impossible d'ouvrir le fichier %s.\n", path1);
exit(EXIT_FAILURE);
}
printf("\nNom du fichier de destination : ");
scanf("%s", path2);
file2 = fopen(path2, "wb"); // mode binaire
if(file2 == NULL)
{
printf("\nErreur : impossible d'ouvrir ou de cr%cer le fichier %s.\n",
'\x82', path2);
exit(EXIT_FAILURE);
}
printf("\nCl%c de cryptage : ",'\x82');
scanf("%s", cleDeCryptage);
if( xor(file1, file2, cleDeCryptage) )
{
printf("Cryptage ou d%ccryptage r%cussi avec succ%cs",
'\x82', '\x82', '\x8a');
}
else
{
printf("Une erreur est survenue lors du cryptage ou d%ccryptage.",
'\x82');
}
if ( file1 != NULL )
fclose(file1);
if ( file2 != NULL )
fclose(file2);
return EXIT_SUCCESS;
}
int xor(FILE *file1, FILE *file2, char cle[])
{
int i=0;
char caract;
char newCaract;
fseek(file1, 0, SEEK_SET);
do{
caract = fgetc(file1);
newCaract = cle[i] ^ caract;
fputc(newCaract, file2);
i++;
if ( i == strlen(cle) )
i=0;
} while (caract != EOF);
return 1;
} |
Partager