ajout crypto
This commit is contained in:
BIN
crypto/TP1/Ex1/file.crypt
Normal file
BIN
crypto/TP1/Ex1/file.crypt
Normal file
Binary file not shown.
16
crypto/TP1/Ex1/file.txt
Normal file
16
crypto/TP1/Ex1/file.txt
Normal 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
BIN
crypto/TP1/Ex1/lfsr
Executable file
Binary file not shown.
43
crypto/TP1/Ex1/lfsr.c
Normal file
43
crypto/TP1/Ex1/lfsr.c
Normal 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;
|
||||
}
|
||||
BIN
crypto/TP1/Ex2/fichier.crypt
Normal file
BIN
crypto/TP1/Ex2/fichier.crypt
Normal file
Binary file not shown.
BIN
crypto/TP1/Ex2/fichier.txt
Normal file
BIN
crypto/TP1/Ex2/fichier.txt
Normal file
Binary file not shown.
1
crypto/TP1/Ex2/key1.k
Normal file
1
crypto/TP1/Ex2/key1.k
Normal file
@@ -0,0 +1 @@
|
||||
÷Nà{q2‘*:¬¨œÇmfU#©`í±Å?&Q3
|
||||
BIN
crypto/TP1/Ex2/xtea
Executable file
BIN
crypto/TP1/Ex2/xtea
Executable file
Binary file not shown.
66
crypto/TP1/Ex2/xtea.c
Normal file
66
crypto/TP1/Ex2/xtea.c
Normal 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;
|
||||
}
|
||||
64
crypto/TP1/Ex3/Xtea_hash.c
Normal file
64
crypto/TP1/Ex3/Xtea_hash.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user