107 lines
3.1 KiB
C
107 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "../lib/big.h"
|
|
#include "../inc/crypt_utils.h"
|
|
#include "../inc/key_utils.h"
|
|
|
|
void encrypt_file(public_key pub_key, const char *input_filepath, const char *output_filepath) {
|
|
FILE *input_file, *output_file;
|
|
size_t total_size, bits, encrypted_size, input_data_size;
|
|
big_n input_data, encrypted_data;
|
|
|
|
input_file = fopen(input_filepath, "r");
|
|
if (!input_file) {
|
|
fprintf(stderr, "Une erreur est survenue lors de la lecture du fichier \"%s\".\n", input_filepath);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
output_file = fopen(output_filepath, "w");
|
|
if (!output_file) {
|
|
fprintf(stderr, "Une erreur est survenue lors de l'ecriture du fichier \"%s\".\n", output_filepath);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
fseek(input_file, 0, SEEK_END);
|
|
total_size = ftell(input_file);
|
|
fseek(input_file, 0, SEEK_SET);
|
|
|
|
fwrite(&total_size, sizeof(total_size), 1, output_file);
|
|
|
|
bits = mssb(pub_key.n);
|
|
encrypted_size = (bits + 7) / 8;
|
|
input_data_size = (bits - 1) / 8;
|
|
|
|
zero_big(input_data);
|
|
while (fread(input_data, 1, input_data_size, input_file) > 0) {
|
|
encrypt(pub_key, input_data, encrypted_data);
|
|
fwrite(encrypted_data, 1, encrypted_size, output_file);
|
|
zero_big(input_data);
|
|
}
|
|
|
|
fclose(input_file);
|
|
fclose(output_file);
|
|
}
|
|
|
|
void decrypt_file(private_key priv_key, const char *input_filepath, const char *output_filepath) {
|
|
FILE *input_file, *output_file;
|
|
size_t total_size, bits, encrypted_size, output_data_size, total_written;
|
|
big_n encrypted_data, output_data;
|
|
|
|
input_file = fopen(input_filepath, "r");
|
|
if (!input_file) {
|
|
fprintf(stderr, "Une erreur est survenue lors de la lecture du fichier \"%s\".\n", input_filepath);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
output_file = fopen(output_filepath, "w");
|
|
if (!output_file) {
|
|
fprintf(stderr, "Une erreur est survenue lors de l'ecriture du fichier \"%s\".\n", output_filepath);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
fread(&total_size, sizeof(total_size), 1, input_file);
|
|
|
|
bits = mssb(priv_key.n);
|
|
encrypted_size = (bits + 7) / 8;
|
|
output_data_size = (bits - 1) / 8;
|
|
|
|
total_written = 0;
|
|
|
|
zero_big(encrypted_data);
|
|
while (fread(encrypted_data, 1, encrypted_size, input_file) > 0) {
|
|
decrypt(priv_key, encrypted_data, output_data);
|
|
fwrite(output_data, 1, (total_written + output_data_size > total_size) ? total_size - total_written : output_data_size, output_file);
|
|
total_written += output_data_size;
|
|
zero_big(encrypted_data);
|
|
}
|
|
|
|
fclose(input_file);
|
|
fclose(output_file);
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
private_key priv_key;
|
|
public_key pub_key;
|
|
|
|
if (argc != 5) {
|
|
fprintf(stderr, "Usage : %s <-e|-d> <clé> <fichier d'entrée> <fichier de sortie>\n", argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (strcmp(argv[1], "-e") == 0) {
|
|
pub_key = public_key_read(argv[2]);
|
|
encrypt_file(pub_key, argv[3], argv[4]);
|
|
} else if (strcmp(argv[1], "-d") == 0) {
|
|
priv_key = private_key_read(argv[2]);
|
|
decrypt_file(priv_key, argv[3], argv[4]);
|
|
} else {
|
|
fprintf(stderr, "Commande \"%s\" invalide. Commandes disponibles :\n\t-e pour crypter\n\t-d pour décrypter\n", argv[2]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|