ajout crypto
This commit is contained in:
1
crypto/TP2/forcebrute/16bits-encrypted.dat
Normal file
1
crypto/TP2/forcebrute/16bits-encrypted.dat
Normal 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¶¶
|
||||
BIN
crypto/TP2/forcebrute/16bitsef
Normal file
BIN
crypto/TP2/forcebrute/16bitsef
Normal file
Binary file not shown.
19
crypto/TP2/main.c
Normal file
19
crypto/TP2/main.c
Normal 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
BIN
crypto/TP2/spn-decrypt-file
Executable file
Binary file not shown.
BIN
crypto/TP2/spn-encrypt-file
Executable file
BIN
crypto/TP2/spn-encrypt-file
Executable file
Binary file not shown.
112
crypto/TP2/spn.c
Normal file
112
crypto/TP2/spn.c
Normal 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
17
crypto/TP2/spn.h
Normal 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
|
||||
41
crypto/TP2/spn_decrypt_file.c
Normal file
41
crypto/TP2/spn_decrypt_file.c
Normal 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;
|
||||
}
|
||||
41
crypto/TP2/spn_encrypt_file.c
Normal file
41
crypto/TP2/spn_encrypt_file.c
Normal 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
BIN
crypto/TP2/spn_test
Executable file
Binary file not shown.
1
crypto/TP2/tests_ex2/key
Normal file
1
crypto/TP2/tests_ex2/key
Normal file
@@ -0,0 +1 @@
|
||||
<EFBFBD>
|
||||
BIN
crypto/TP2/tests_ex2/perm
Normal file
BIN
crypto/TP2/tests_ex2/perm
Normal file
Binary file not shown.
BIN
crypto/TP2/tests_ex2/spndf
Normal file
BIN
crypto/TP2/tests_ex2/spndf
Normal file
Binary file not shown.
BIN
crypto/TP2/tests_ex2/spndf.txt
Normal file
BIN
crypto/TP2/tests_ex2/spndf.txt
Normal file
Binary file not shown.
0
crypto/TP2/tests_ex2/spndf_dechifre.txt
Normal file
0
crypto/TP2/tests_ex2/spndf_dechifre.txt
Normal file
BIN
crypto/TP2/tests_ex2/spnef
Normal file
BIN
crypto/TP2/tests_ex2/spnef
Normal file
Binary file not shown.
BIN
crypto/TP2/tests_ex2/subst
Normal file
BIN
crypto/TP2/tests_ex2/subst
Normal file
Binary file not shown.
Reference in New Issue
Block a user