48 lines
1.3 KiB
C
48 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "../lib/big.h"
|
|
#include "../inc/crypt_utils.h"
|
|
#include "../inc/key_utils.h"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
public_key pub_key;
|
|
private_key priv_key;
|
|
big_n input_big, output_big;
|
|
FILE *input_file, *output_file;
|
|
|
|
if (argc != 5) {
|
|
fprintf(stderr, "Usage : %s <-e|-d> <clé> <fichier d'entrée> <fichier de sortie>\n", argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
input_file = fopen(argv[3], "r");
|
|
if (!input_file) {
|
|
fprintf(stderr, "Une erreur est survenue lors de la lecture du fichier \"%s\".\n", argv[3]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
read_hex(input_file, input_big);
|
|
fclose(input_file);
|
|
|
|
if (strcmp(argv[1], "-e") == 0) {
|
|
pub_key = public_key_read(argv[2]);
|
|
encrypt(pub_key, input_big, output_big);
|
|
} else if (strcmp(argv[1], "-d") == 0) {
|
|
priv_key = private_key_read(argv[2]);
|
|
decrypt(priv_key, input_big, output_big);
|
|
} 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;
|
|
}
|
|
|
|
output_file = fopen(argv[4], "w");
|
|
if (!output_file) {
|
|
fprintf(stderr, "Une erreur est survenue lors de l'écriture du fichier \"%s\".\n", argv[4]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
write_hex(output_file, output_big, '\n');
|
|
fclose(output_file);
|
|
|
|
return EXIT_SUCCESS;
|
|
} |