This commit is contained in:
2026-03-02 18:34:04 +01:00
parent e70a93998c
commit 79c0839aa1
9 changed files with 1381 additions and 1 deletions

41
tp/tp1/README.md Normal file
View File

@@ -0,0 +1,41 @@
# TP1
## Ex1
Lobjectif de cet exercice est de chiffrer une image en ligne de commande à
laide des outils fournis par la librairie openssl.
1. Prenez une image de votre choix et convertissez la dans le format ppm à laide de la commande
`convert`.
2. Avec la commande `head`, mettez les 3 premières lignes de votre fichier ppm dans un fichier
header.txt.
3. Avec la commande tail mettez tout le fichier ppm sauf les 3 premières lignes dans un fichier
body.bin.
4. Chiffrez avec openssl en AES-ECB le fichier body.bin.
5. Remettez len-tête header.txt au début du fichier obtenu à la question précédente pour avoir
une image chiffrée, et observez le résultat.
## EX2
Le but est d'implanter un registre à décalage linéaire, sur un octet.
À chaque étape, le registre (un octet) $(b_7,b_6,b_5,b_4,b_3,b_2,b_1,b_0)$ est décalé à gauche, et
le bit $b_0$ est remplacé par un bit, calculé par une fonction linéaire $f$.
Vous disposez d'un [fichier](src/ex1/file.crypt) crypté avec un lfsr, en faisant
un XOR de chacun des octets avec les valeurs successives du registre. L'état
initial du registre était `0xa7`, et la fonction utilisée
\[
f(b_7,b_6,b_5,b_4,b_3,b_2,b_1,b_0) = b_7\oplus b_6\oplus b_5\oplus b_4\oplus b_3\oplus b_1
\]
Retrouver le fichier initial.
Vous pouvez utiliser la fonction interne `__builtin_parity` de `gcc`.

41
tp/tp1/aide.md Normal file
View File

@@ -0,0 +1,41 @@
### Setting a bit
Use the bitwise OR operator (|) to set a bit.
`number |= 1 << x;`
That will set bit x.
### Clearing a bit
Use the bitwise AND operator (&) to clear a bit.
`number &= ~(1 << x);`
That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.
### Toggling a bit
The XOR operator (^) can be used to toggle a bit.
`number ^= 1 << x;`
That will toggle bit x.
### Checking a bit
You didn't ask for this but I might as well add it.
To check a bit, shift the number x to the right, then bitwise AND it:
`bit = (number >> x) & 1;`
That will put the value of bit x into the variable bit.
### Changing the nth bit to x
Setting the nth bit to either 1 or 0 can be achieved with the following:
`number ^= (-x ^ number) & (1 << n);`
Bit n will be set if x is 1, and cleared if x is 0.

BIN
tp/tp1/img/TEA.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
tp/tp1/img/feistel.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

1263
tp/tp1/img/feistel.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 80 KiB

BIN
tp/tp1/img/xtea.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

BIN
tp/tp1/src/ex2/file.crypt Normal file

Binary file not shown.

36
tp/tp1/src/ex2/lfsr.c Normal file
View File

@@ -0,0 +1,36 @@
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
unsigned char next(
unsigned char lfsr, // le registre
unsigned char retroaction_f // les bits selectionnés par la
) // fonction de retroaction
{
// TODO
}
int main(int argc, char *argv[])
{
int fd_in,fd_out;
unsigned char w,buf,f;
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);
f = ; // TODO
while(1){
ssize_t nb = read(fd_in,&buf,1);
if (nb <=0)
break;
buf ^= w;
write(fd_out,&buf,1);
w=next(w,f);
}
return 0;
}