tentative 2eme exo
This commit is contained in:
44
tp2/spn-encrypt-file.c
Normal file
44
tp2/spn-encrypt-file.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#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 char b1, b2;
|
||||
while (fread(&b1, 1, 1, fin)) {
|
||||
if (fread(&b2, 1, 1, fin) != 1) {
|
||||
b2 = 0;
|
||||
}
|
||||
unsigned short block = (b1 << 8) | b2;
|
||||
unsigned short enc = encrypt(block, key, perm, subst);
|
||||
fwrite(&enc, 2, 1, fout);
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user