ajout crypto

This commit is contained in:
2025-03-31 10:06:09 +02:00
parent 731020a934
commit bff1a74ae7
680 changed files with 14849 additions and 0 deletions

BIN
crypto/TP1/Ex1/file.crypt Normal file

Binary file not shown.

16
crypto/TP1/Ex1/file.txt Normal file
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
crypto/TP1/Ex1/lfsr Executable file

Binary file not shown.

43
crypto/TP1/Ex1/lfsr.c Normal file
View File

@@ -0,0 +1,43 @@
#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)
{
unsigned char new_bit = __builtin_parity(lfsr & (MASK_BIT_7 | MASK_BIT_6 | MASK_BIT_5 | MASK_BIT_4 | MASK_BIT_3 | MASK_BIT_1));
return (lfsr << 1) | new_bit;
}
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;
}

Binary file not shown.

BIN
crypto/TP1/Ex2/fichier.txt Normal file

Binary file not shown.

1
crypto/TP1/Ex2/key1.k Normal file
View File

@@ -0,0 +1 @@
÷Nà {q2*:¬¨œÇmfU#© `í±Å?&Q3

BIN
crypto/TP1/Ex2/xtea Executable file

Binary file not shown.

66
crypto/TP1/Ex2/xtea.c Normal file
View File

@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define DELTA 0x9E3779B9
#define ROUNDS 32
void decrypt(uint32_t v[2], uint32_t const key[4]) {
uint32_t v0 = v[0], v1 = v[1], sum = DELTA * ROUNDS;
for (int i = 0; i < ROUNDS; 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 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 < ROUNDS; 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 process_file(const char *mode, const char *keyfile, const char *inputfile, const char *outputfile) {
uint32_t key[4];
uint32_t block[2];
int fd_key = open(keyfile, O_RDONLY);
int fd_in = open(inputfile, O_RDONLY);
int fd_out = open(outputfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd_key < 0 || fd_in < 0 || fd_out < 0) {
perror("Error opening file");
exit(1);
}
read(fd_key, key, sizeof(key));
close(fd_key);
while (read(fd_in, block, sizeof(block)) == sizeof(block)) {
if (strcmp(mode, "-d") == 0) {
decrypt(block, key);
} else {
encrypt(block, key);
}
write(fd_out, block, sizeof(block));
}
close(fd_in);
close(fd_out);
}
int main(int argc, char *argv[]) {
if (argc != 5) {
fprintf(stderr, "Usage: %s -e|-d filekey file1 file2\n", argv[0]);
return 1;
}
process_file(argv[1], argv[2], argv[3], argv[4]);
return 0;
}

View File

@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define DELTA 0x9E3779B9
#define ROUNDS 32
#define BLOCK_SIZE 24
void 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 < ROUNDS; 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_hash(const char *filename, uint64_t *hash) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
perror("Error opening file");
exit(1);
}
uint64_t current_hash = 0;
uint8_t buffer[BLOCK_SIZE];
ssize_t bytes_read;
while ((bytes_read = read(fd, buffer, BLOCK_SIZE)) > 0) {
if (bytes_read < BLOCK_SIZE) {
memset(buffer + bytes_read, 0, BLOCK_SIZE - bytes_read);
buffer[BLOCK_SIZE - 1] = BLOCK_SIZE - bytes_read;
}
uint32_t x[2];
uint32_t k[4];
memcpy(x, buffer, 8);
memcpy(k, buffer + 8, 16);
encrypt(x, k);
uint64_t hashed_block = ((uint64_t)x[0] << 32) | x[1];
current_hash ^= hashed_block;
}
close(fd);
*hash = current_hash;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
return 1;
}
uint64_t hash;
xtea_hash(argv[1], &hash);
printf("Hash: %016lx\n", hash);
return 0;
}

View File

@@ -0,0 +1 @@
&&&ÅS£PšØ¦<C398>š&쓦Qc&RëéR&Rë£&<26>Ú¡£&éP£&î¦é<C2A6>£<EFBFBD>¶&&&ÅS£PšØ¦<C398>š&P¦îîc&QÚRë&Rë£ÚP&Ú“Ù£Pc&¡P¦cc£<63>¶&&&ÅS£PšØ¦<C398>š&쓦Qc&RëéR&Rë£&QéP&Úc&¦S£P¶&&&ÅS£PšØ¦<C398>š&쓦Qc&Rë£&Ù¦¦<C2A6>&Ù)šc&î¦cR¶&&&ÅS£PšØ¦<C398>š&쓦Qc&Rë£&ÚÙëR&Qéc&Ú«£<C2AB>¶&&&¼ë£&f¦¦P&cRéš&f¦¦Pl&Rë£&PÚ¡ë&Ù£R&PÚ¡ë¶&&¼ëéR[c&ë¦Q&ÚR&Ù¦£c¶&&ÅS£PšØ¦<C398>š&쓦Qc¶¶

Binary file not shown.

19
crypto/TP2/main.c Normal file
View File

@@ -0,0 +1,19 @@
#include <stdio.h>
#include "spn.h"
int main() {
unsigned char s[16] = {14, 3, 2, 10, 12, 11, 15, 9, 0, 4, 7, 13, 1, 8, 6, 5};
unsigned char perm[8] = {5, 2, 0, 4, 6, 1, 7, 3};
unsigned short key = 0xABCD; // Exemple de clé
unsigned char plaintext = 0x3C; // Exemple de bloc clair (0011 1100)
printf("Texte clair : 0x%02X\n", plaintext);
unsigned char ciphertext = encrypt(plaintext, key, perm, s);
printf("Texte chiffré : 0x%02X\n", ciphertext);
unsigned char decrypted = decrypt(ciphertext, key, perm, s);
printf("Texte déchiffré : 0x%02X\n", decrypted);
return 0;
}

BIN
crypto/TP2/spn-decrypt-file Executable file

Binary file not shown.

BIN
crypto/TP2/spn-encrypt-file Executable file

Binary file not shown.

112
crypto/TP2/spn.c Normal file
View File

@@ -0,0 +1,112 @@
#include "spn.h"
#include <stdio.h>
#include <stdlib.h>
// Applique la permutation définie par perm à l'octet w
unsigned char do_perm(unsigned char w, unsigned char perm[8]) {
unsigned char res = 0;
for (int i = 0; i < 8; i++) {
if (w & (1 << i)) {
res |= (1 << perm[i]);
}
}
return res;
}
// Applique la substitution définie par subst sur chaque moitié de w
unsigned char do_subst(unsigned char w, unsigned char subst[16]) {
unsigned char left = (w >> 4) & 0x0F; // 4 bits de poids fort
unsigned char right = w & 0x0F; // 4 bits de poids faible
return (subst[left] << 4) | subst[right];
}
// Chiffre un bloc de 8 bits avec une clé de 16 bits
unsigned char encrypt(
unsigned char w,
unsigned short key,
unsigned char perm[8],
unsigned char subst[16]
) {
unsigned char subkey1 = (key >> 8) & 0xFF;
unsigned char subkey2 = key & 0xFF;
// Tour 1 : XOR avec la clé, substitution, permutation
w ^= subkey1;
w = do_subst(w, subst);
w = do_perm(w, perm);
// Tour 2 : XOR avec la clé, substitution, XOR avec la sous-clé finale
w ^= subkey2;
w = do_subst(w, subst);
w ^= subkey1;
return w;
}
// Déchiffre un bloc de 8 bits avec une clé de 16 bits
unsigned char decrypt(
unsigned char w,
unsigned short key,
unsigned char perm[8],
unsigned char subst[16]
) {
unsigned char subkey1 = (key >> 8) & 0xFF;
unsigned char subkey2 = key & 0xFF;
// Table de substitution inverse
unsigned char inv_subst[16];
for (int i = 0; i < 16; i++) {
inv_subst[subst[i]] = i;
}
// Tour inverse 2 : XOR avec la clé, substitution inverse, XOR avec la sous-clé
w ^= subkey1;
w = do_subst(w, inv_subst);
w ^= subkey2;
// Tour inverse 1 : permutation inverse, substitution inverse, XOR avec la clé
unsigned char inv_perm[8];
for (int i = 0; i < 8; i++) {
inv_perm[perm[i]] = i;
}
w = do_perm(w, inv_perm);
w = do_subst(w, inv_subst);
w ^= subkey1;
return w;
}
void read_key(const char *filename, unsigned short *key) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Erreur ouverture fichier clé");
exit(EXIT_FAILURE);
}
fscanf(file, "%hx", key); // Lire un entier hexadécimal (16 bits)
fclose(file);
}
void read_subst(const char *filename, unsigned char subst[16]) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Erreur ouverture fichier substitution");
exit(EXIT_FAILURE);
}
for (int i = 0; i < 16; i++) {
fscanf(file, "%hhu", &subst[i]); // Lire un entier 8 bits
}
fclose(file);
}
void read_perm(const char *filename, unsigned char perm[8]) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Erreur ouverture fichier permutation");
exit(EXIT_FAILURE);
}
for (int i = 0; i < 8; i++) {
fscanf(file, "%hhu", &perm[i]); // Lire un entier 8 bits
}
fclose(file);
}

17
crypto/TP2/spn.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef _SPN_H
#define _SPN_H
#include <stdint.h>
#include <stdio.h>
unsigned char do_perm(unsigned char w, unsigned char perm[8]);
unsigned char do_subst(unsigned char w, unsigned char subst[16]);
unsigned char encrypt(unsigned char w, unsigned short key, unsigned char perm[8], unsigned char subst[16]);
unsigned char decrypt(unsigned char w, unsigned short key, unsigned char perm[8], unsigned char subst[16]);
// Ajout des déclarations des fonctions de lecture
void read_key(const char *filename, unsigned short *key);
void read_subst(const char *filename, unsigned char subst[16]);
void read_perm(const char *filename, unsigned char perm[8]);
#endif

View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include "spn.h"
void decrypt_file(const char *input_filename, const char *output_filename, unsigned short key, unsigned char perm[8], unsigned char subst[16]) {
FILE *in = fopen(input_filename, "rb");
FILE *out = fopen(output_filename, "wb");
if (!in || !out) {
perror("Erreur ouverture fichier");
exit(EXIT_FAILURE);
}
unsigned char byte;
while (fread(&byte, 1, 1, in) == 1) {
unsigned char decrypted = decrypt(byte, key, perm, subst);
fwrite(&decrypted, 1, 1, out);
}
fclose(in);
fclose(out);
}
int main(int argc, char *argv[]) {
if (argc != 6) {
printf("Usage: %s clé.txt subst.txt perm.txt fichier_chiffre fichier_déchiffré\n", argv[0]);
return EXIT_FAILURE;
}
unsigned short key;
unsigned char subst[16];
unsigned char perm[8];
read_key(argv[1], &key);
read_subst(argv[2], subst);
read_perm(argv[3], perm);
decrypt_file(argv[4], argv[5], key, perm, subst);
printf("Déchiffrement terminé : %s → %s\n", argv[4], argv[5]);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include "spn.h"
void encrypt_file(const char *input_filename, const char *output_filename, unsigned short key, unsigned char perm[8], unsigned char subst[16]) {
FILE *in = fopen(input_filename, "rb");
FILE *out = fopen(output_filename, "wb");
if (!in || !out) {
perror("Erreur ouverture fichier");
exit(EXIT_FAILURE);
}
unsigned char byte;
while (fread(&byte, 1, 1, in) == 1) {
unsigned char encrypted = encrypt(byte, key, perm, subst);
fwrite(&encrypted, 1, 1, out);
}
fclose(in);
fclose(out);
}
int main(int argc, char *argv[]) {
if (argc != 6) {
printf("Usage: %s clé.txt subst.txt perm.txt fichier_clair fichier_chiffre\n", argv[0]);
return EXIT_FAILURE;
}
unsigned short key;
unsigned char subst[16];
unsigned char perm[8];
read_key(argv[1], &key);
read_subst(argv[2], subst);
read_perm(argv[3], perm);
encrypt_file(argv[4], argv[5], key, perm, subst);
printf("Chiffrement terminé : %s → %s\n", argv[4], argv[5]);
return EXIT_SUCCESS;
}

BIN
crypto/TP2/spn_test Executable file

Binary file not shown.

1
crypto/TP2/tests_ex2/key Normal file
View File

@@ -0,0 +1 @@
<EFBFBD>

BIN
crypto/TP2/tests_ex2/perm Normal file

Binary file not shown.

BIN
crypto/TP2/tests_ex2/spndf Normal file

Binary file not shown.

Binary file not shown.

View File

BIN
crypto/TP2/tests_ex2/spnef Normal file

Binary file not shown.

BIN
crypto/TP2/tests_ex2/subst Normal file

Binary file not shown.