Files
chiffrement/tp2/spn-decrypt-file.c

41 lines
1018 B
C
Raw Permalink Normal View History

2025-06-03 11:42:20 +02:00
#include <stdio.h>
#include <stdlib.h>
#include "spn.h"
void read_array(const char* filename, unsigned char* array, int size) {
FILE* f = fopen(filename, "r");
for (int i = 0; i < size; i++) {
fscanf(f, "%hhu", &array[i]);
}
fclose(f);
}
int main(int argc, char* argv[]) {
if (argc != 6) {
printf("%s <key_file> <subst_file> <perm_file> <input> <output>\n", argv[0]);
return 1;
}
FILE* fkey = fopen(argv[1], "r");
unsigned int key;
fscanf(fkey, "%u", &key);
fclose(fkey);
unsigned char subst[16], perm[16];
read_array(argv[2], subst, 16);
read_array(argv[3], perm, 16);
FILE* fin = fopen(argv[4], "rb");
FILE* fout = fopen(argv[5], "wb");
unsigned short block;
while (fread(&block, 2, 1, fin)) {
unsigned short dec = decrypt(block, key, perm, subst);
fputc(dec >> 8, fout);
fputc(dec & 0xFF, fout);
}
fclose(fin);
fclose(fout);
return 0;
}