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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #define __USE_LARGEFILE64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include <windows.h>
#include "ptrop.h"
#include "file.h"
#include "logger.h"
#include "math.h"
#include "str.h"
#include "dictionnary.h"
#include "tim.h"
#include "crypt.h"
#define MO 1048576
typedef struct
{
double avg;
double transfert_rate;
double file_size;
double file_size_remaining;
double time_init;
double time_current;
}t_stat;
void stat_init(t_stat *s)
{
s->avg=0.;
s->transfert_rate=0.;
s->file_size=0.;
s->file_size_remaining=0.;
s->time_init=0.;
s->time_current=0.;
}
void stat_maj(t_stat *s, double u, double v, double w, double x, double y, double z)
{
s->avg=u;
s->transfert_rate=v;
s->file_size=w;
s->file_size_remaining=x;
s->time_init=y;
s->time_current=z;
}
void stat_printf(t_stat *s)
{
system("cls");
t_time *time = time_create_structure();
time_format(s->time_current, time);
printf("\n %.2f Mo / %.2f Mo @ %.2f Mo/s", s->file_size_remaining, s->file_size, s->transfert_rate);
time_display(time);
time_destroy_structure(time);
}
t_stat g_stat;
void gxor(const char *file_path, unsigned long long int key_length)
{
char buffer[3];
unsigned long long int byte_read=0;
unsigned long long int byte_total=0;
/* Taille du fichier à crypter */
typedef struct __stat64 st;
_stat64(file_path, &st);
unsigned long long int flength = st.st_size;
printf("\n size %I64u", flength);
getchar();
/* Décompose hle chemine du fichier à crypter */
t_2Dc *path_element = path_decompose(file_path);
/* Ajout des extentions .key et .crypt pour désigner le fichier qui contient la clé et le fichier crypté */
char *key_path = str_concat(str_concat_multiple(path_element->da,path_element->x-1),str_concat(path_element->da[path_element->x-1],".key"));
char *xor_path = str_concat(str_concat_multiple(path_element->da,path_element->x-1),str_concat(path_element->da[path_element->x-1],".crypt"));
unsigned long long int *key = allocate_1D_ulli(key_length);
FILE *f_key = file_open(key_path,"wb");
/* Génération de la clé et création du fichier pour la sauvegarder */
unsigned long long int i;
for ( i=0 ; i<key_length ; i++)
{
buffer[1] = rand()%256;
key[i]=buffer[1];
fwrite(&buffer[1], sizeof(char), 1, f_key);
}
stat_init(&g_stat); /* Initialisation des variables pour l'affichage des statistiques */
double time_zero = GetTickCount(); /* Valeur du timer au démarrage du cryptage */
FILE *f = file_open(file_path,"rb");
FILE *f_xor = file_open(xor_path,"wb");
/* Cryptage du fichier original */
while ((byte_read=fread(&buffer[0],sizeof(char), 1,f)) > 0) /* Parcourir l'intégralité du fichier à crypter octet par octet */
{
byte_total+=1;
/* Résultat de l'octet crypté */
buffer[2]=buffer[0]^key[byte_total%key_length];
/* Ecriture de l'octet crypté */
fwrite(&buffer[2], sizeof(char), byte_read, f_xor);
/* Afficher l'avancement du cryptage */
if ( byte_total % MO == 0 )
stat_maj(&g_stat,0,1000.*byte_total/(1024*1024*(GetTickCount()-time_zero)),flength/(1024*1024.),byte_total/(1024*1024.),0,((flength-byte_total)/MO)/(1000.*byte_total/(1024*1024*(GetTickCount()-time_zero)))), stat_printf(&g_stat);
}
stat_maj(&g_stat,0,1000.*byte_total/(1024*1024*(GetTickCount()-time_zero)),flength/(1024*1024.),byte_total/(1024*1024.),0,((flength-byte_total)/MO)/(1000.*byte_total/(1024*1024*(GetTickCount()-time_zero)))), stat_printf(&g_stat);
/* Désallocation des ressources */
destroy_2D_c(path_element);
deallocate_1D_c(key_path);
deallocate_1D_c(xor_path);
deallocate_1D_ulli(key);
/* Fermeture des fichiers ouverts */
file_close(f_key);
file_close(f_xor);
file_close(f);
} |
Partager