This commit is contained in:
2025-05-13 12:29:59 +02:00
commit 1bb273e8ac
12 changed files with 304 additions and 0 deletions

6
tp1/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"files.associations": {
"mutex": "c",
"fcntl.h": "c"
}
}

BIN
tp1/ex1/decrypt Executable file

Binary file not shown.

39
tp1/ex1/decrypt.c Normal file
View File

@@ -0,0 +1,39 @@
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#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;
}

View File

@@ -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

BIN
tp1/ex1/file.crypt Normal file

Binary file not shown.

BIN
tp1/ex2/fichier.crypt Normal file

Binary file not shown.

1
tp1/ex2/key1.k Normal file
View File

@@ -0,0 +1 @@
<1E>N<EFBFBD> {q2<71>*:<3A><><EFBFBD><EFBFBD>mfU#<23> `<60><><1B>?&Q3

83
tp1/ex2/xtea.c Normal file
View File

@@ -0,0 +1,83 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

96
tp1/ex2/xtea_cbc.c Normal file
View File

@@ -0,0 +1,96 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

1
tp1/ex3/fichier1.txt Normal file
View File

@@ -0,0 +1 @@
je ne sais pa

BIN
tp1/ex3/hachage Executable file

Binary file not shown.

62
tp1/ex3/hachage.c Normal file
View File

@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#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 <fichier>\n", argv[0]);
return 1;
}
hash_file(argv[1]);
return 0;
}