commit 1bb273e8aca52fa0cdb37edc98e69d29e0b2a811 Author: oddos-ma Date: Tue May 13 12:29:59 2025 +0200 tp1 diff --git a/tp1/.vscode/settings.json b/tp1/.vscode/settings.json new file mode 100644 index 0000000..ffc0b53 --- /dev/null +++ b/tp1/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "mutex": "c", + "fcntl.h": "c" + } +} \ No newline at end of file diff --git a/tp1/ex1/decrypt b/tp1/ex1/decrypt new file mode 100755 index 0000000..8f63a42 Binary files /dev/null and b/tp1/ex1/decrypt differ diff --git a/tp1/ex1/decrypt.c b/tp1/ex1/decrypt.c new file mode 100644 index 0000000..daf129b --- /dev/null +++ b/tp1/ex1/decrypt.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +#define MASK_BIT_7 0x80 +#define MASK_BIT_6 0x40 +#define MASK_BIT_5 0x20 +#define MASK_BIT_4 0x10 +#define MASK_BIT_3 0x08 +#define MASK_BIT_2 0x04 +#define MASK_BIT_1 0x02 +#define MASK_BIT_0 0x01 + +unsigned char next(unsigned char lfsr) { + lfsr = lfsr << 1 | __builtin_parity(lfsr & 0xfa); +} + +int main(int argc, char *argv[]) { + int fd_in,fd_out; + unsigned char w,buf;; + + + assert(argc >= 4); + fd_in = open(argv[1],O_RDONLY); + fd_out = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600); + w = (unsigned char)strtol(argv[3],NULL,0); + + while(1){ + ssize_t nb = read(fd_in,&buf,1); + if (nb <=0) + break; + buf ^= w; + write(fd_out,&buf,1); + w=next(w); + } + return 0; +} \ No newline at end of file diff --git a/tp1/ex1/fichierout.decrypt b/tp1/ex1/fichierout.decrypt new file mode 100644 index 0000000..7558a2f --- /dev/null +++ b/tp1/ex1/fichierout.decrypt @@ -0,0 +1,16 @@ +Demain, dès l'aube, à l'heure où blanchit la campagne, +Je partirai. Vois-tu, je sais que tu m'attends. +J'irai par la forêt, j'irai par la montagne. +Je ne puis demeurer loin de toi plus longtemps. + +Je marcherai les yeux fixés sur mes pensées, +Sans rien voir au dehors, sans entendre aucun bruit, +Seul, inconnu, le dos courbé, les mains croisées, +Triste, et le jour pour moi sera comme la nuit. + +Je ne regarderai ni l'or du soir qui tombe, +Ni les voiles au loin descendant vers Harfleur, +Et quand j'arriverai, je mettrai sur ta tombe +Un bouquet de houx vert et de bruyère en fleur. + +— Victor Hugo, Les Contemplations, 3 septembre 1847 diff --git a/tp1/ex1/file.crypt b/tp1/ex1/file.crypt new file mode 100644 index 0000000..1f1e6c2 Binary files /dev/null and b/tp1/ex1/file.crypt differ diff --git a/tp1/ex2/fichier.crypt b/tp1/ex2/fichier.crypt new file mode 100644 index 0000000..37b4742 Binary files /dev/null and b/tp1/ex2/fichier.crypt differ diff --git a/tp1/ex2/key1.k b/tp1/ex2/key1.k new file mode 100644 index 0000000..7fc6ef4 --- /dev/null +++ b/tp1/ex2/key1.k @@ -0,0 +1 @@ +÷Nà {q2‘*:¬¨œÇmfU#© `í±Å?&Q3 \ No newline at end of file diff --git a/tp1/ex2/xtea.c b/tp1/ex2/xtea.c new file mode 100644 index 0000000..ec2d2ad --- /dev/null +++ b/tp1/ex2/xtea.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +#define DELTA 0x9E3779B9 + +void xtea_encrypt_block(uint32_t *v, const uint32_t key[4]) { + uint32_t v0 = v[0], v1 = v[1], sum = 0; + for (unsigned i = 0; i < 32; i++) { + v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); + sum += DELTA; + v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]); + } + v[0] = v0; + v[1] = v1; +} + +void xtea_decrypt_block(uint32_t *v, const uint32_t key[4]) { + uint32_t v0 = v[0], v1 = v[1]; + uint32_t sum = DELTA * 32; + for (unsigned i = 0; i < 32; i++) { + v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]); + sum -= DELTA; + v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); + } + v[0] = v0; + v[1] = v1; +} + +void xtea_process(int encrypt, const char *keyfile, const char *infile, const char *outfile) { + uint32_t key[4]; + + FILE *fk = fopen(keyfile, "rb"); + if (!fk || fread(key, sizeof *key, 4, fk) != 4) { + fprintf(stderr, "Erreur lecture clé\n"); + exit(1); + } + fclose(fk); + + FILE *fin = fopen(infile, "rb"); + FILE *fout = fopen(outfile, "wb"); + if (!fin || !fout) { + fprintf(stderr, "Erreur ouverture fichiers\n"); + exit(1); + } + + uint8_t block[8]; + size_t n; + + while ((n = fread(block, 1, 8, fin)) == 8) { + xtea_encrypt_block((uint32_t*)block, key); + if (!encrypt) + xtea_decrypt_block((uint32_t*)block, key); + fwrite(block, 1, 8, fout); + } + + if (encrypt) { + for (size_t i = n; i < 8 - 1; i++) { + block[i] = 0; + } + block[8 - 1] = 8 - n; + xtea_encrypt_block((uint32_t*)block, key); + fwrite(block, 1, 8, fout); + } else if (n > 0) { + fprintf(stderr, "Erreur : fichier chiffré mal aligné\n"); + exit(1); + } + + fclose(fin); + fclose(fout); +} + +int main(int argc, char *argv[]) +{ + if (argc != 5) { + fprintf(stderr, "Usage : %s -e|-d keyfile infile outfile\n", argv[0]); + return 1; + } + int enc = (strcmp(argv[1], "-e") == 0); + xtea_process(enc, argv[2], argv[3], argv[4]); + return 0; +} \ No newline at end of file diff --git a/tp1/ex2/xtea_cbc.c b/tp1/ex2/xtea_cbc.c new file mode 100644 index 0000000..e65f342 --- /dev/null +++ b/tp1/ex2/xtea_cbc.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +#define DELTA 0x9E3779B9 + +void xtea_encrypt_block(uint32_t v[2], const uint32_t key[4]) { + uint32_t v0 = v[0], v1 = v[1], sum = 0; + for (unsigned i = 0; i < 32; i++) { + v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); + sum += DELTA; + v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]); + } + v[0] = v0; v[1] = v1; +} + +void xtea_decrypt_block(uint32_t v[2], const uint32_t key[4]) { + uint32_t v0 = v[0], v1 = v[1]; + uint32_t sum = DELTA * 32; + for (unsigned i = 0; i < 32; i++) { + v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]); + sum -= DELTA; + v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); + } + v[0] = v0; v[1] = v1; +} + +void xtea_cbc_process(int encrypt, const char *iv_str, const char *keyfile, const char *infile, const char *outfile) { + uint32_t key[4]; + uint8_t iv[8]; + memcpy(iv, iv_str, 8); + + FILE *fk = fopen(keyfile, "rb"); + if (!fk || fread(key, sizeof(uint32_t), 4, fk) != 4) { + fprintf(stderr, "Erreur lecture clé\n"); + exit(1); + } + fclose(fk); + + FILE *fin = fopen(infile, "rb"); + FILE *fout = fopen(outfile, "wb"); + if (!fin || !fout) { + fprintf(stderr, "Erreur ouverture fichiers\n"); + exit(1); + } + + uint8_t block[8], prev[8]; + memcpy(prev, iv, 8); + size_t n; + + while ((n = fread(block, 1, 8, fin)) == 8) { + if (encrypt) { + for (int i = 0; i < 8; i++){ + block[i] ^= prev[i]; + } + xtea_encrypt_block((uint32_t*)block, key); + fwrite(block, 1, 8, fout); + memcpy(prev, block, 8); + } else { + uint8_t saved[8]; + memcpy(saved, block, 8); + xtea_decrypt_block((uint32_t*)block, key); + for (int i = 0; i < 8; i++) { + block[i] ^= prev[i]; + } + fwrite(block, 1, 8, fout); + memcpy(prev, saved, 8); + } + } + + if (encrypt) { + for (size_t i = n; i < 7; i++) block[i] = 0; + block[7] = 8 - n; + for (int i = 0; i < 8; i++) block[i] ^= prev[i]; + xtea_encrypt_block((uint32_t*)block, key); + fwrite(block, 1, 8, fout); + } else if (n > 0) { + fprintf(stderr, "Erreur : fichier chiffré mal aligné\n"); + exit(1); + } + + fclose(fin); + fclose(fout); +} + +int main(int argc, char *argv[]) { + if (argc != 6) { + fprintf(stderr, "Usage : %s -e|-d iv keyfile infile outfile\n", argv[0]); + return 1; + } + int encrypt = (strcmp(argv[1], "-e") == 0); + xtea_cbc_process(encrypt, argv[2], argv[3], argv[4], argv[5]); + return 0; + +} \ No newline at end of file diff --git a/tp1/ex3/fichier1.txt b/tp1/ex3/fichier1.txt new file mode 100644 index 0000000..f6c118c --- /dev/null +++ b/tp1/ex3/fichier1.txt @@ -0,0 +1 @@ +je ne sais pa \ No newline at end of file diff --git a/tp1/ex3/hachage b/tp1/ex3/hachage new file mode 100755 index 0000000..8b70af9 Binary files /dev/null and b/tp1/ex3/hachage differ diff --git a/tp1/ex3/hachage.c b/tp1/ex3/hachage.c new file mode 100644 index 0000000..3d3b3c5 --- /dev/null +++ b/tp1/ex3/hachage.c @@ -0,0 +1,62 @@ +#include +#include +#include + +#define DELTA 0x9e3779b9 + +void xtea_encrypt(uint32_t v[2], uint32_t const key[4]) { + uint32_t v0 = v[0], v1 = v[1], sum = 0; + for (int i = 0; i < 32; i++) { + v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + key[sum & 3]); + sum += DELTA; + v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + key[(sum >> 11) & 3]); + } + v[0] = v0; + v[1] = v1; +} + +void hash_file(const char *filename) { + FILE *f = fopen(filename, "rb"); + if (!f) { + perror("Erreur ouverture fichier"); + return; + } + + uint8_t buffer[24]; + uint64_t final_hash[1] = {0}; + size_t bytes_read; + + while ((bytes_read = fread(buffer, 1, 24, f)) > 0) { + if (bytes_read < 24) { + memset(buffer + bytes_read, 24 - bytes_read, 24 - bytes_read); + } + + uint32_t x[2]; + memcpy(x, buffer, 8); + + uint32_t key[4]; + memcpy(key, buffer + 8, 16); + + uint32_t x_orig[2] = {x[0], x[1]}; + xtea_encrypt(x, key); + + x[0] ^= x_orig[0]; + x[1] ^= x_orig[1]; + + final_hash[0] ^= ((uint64_t)x[0] << 32) | x[1]; + } + + fclose(f); + + printf("Empreinte (hachage) : %016llx\n", (unsigned long long)final_hash[0]); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage : %s \n", argv[0]); + return 1; + } + + hash_file(argv[1]); + return 0; +} \ No newline at end of file