woody-woodpacker/srcs/encrypt.c

39 lines
1.6 KiB
C
Raw Normal View History

2024-02-21 12:54:33 +00:00
#include "../includes/woody.h"
2024-06-15 10:43:59 +00:00
#include "../includes/rsa.h"
2024-02-21 12:54:33 +00:00
2024-06-15 10:43:59 +00:00
unsigned long encrypt(char *file, unsigned long int offset, unsigned long int size, rsa_t rsa)
2024-02-21 12:54:33 +00:00
{
2024-06-15 10:43:59 +00:00
size_t padded_len = size * sizeof(char) * 33 / sizeof(uint32_t) / 32 + 1; // every 32 octet one padding octet, plus one for the remainder (uses too much memory for size % 128 == 0 but fuck you)
uint32_t *padded = (uint32_t *)malloc(sizeof(uint32_t) * padded_len);
for (size_t i = 0; i < padded_len; i++) {
padded[padded_len] = 0;
}
(void)rsa;
2024-02-21 12:54:33 +00:00
size_t i = 0;
2024-06-15 10:43:59 +00:00
while (i < size) {
size_t j = 0;
while (j < 8) {
size_t bit_index = i * 8 * sizeof(char) + j;
//printf("bit_index : %ld\n", bit_index);
padded[bit_index / 31] += (1 & (file[bit_index / 8] >> j)) << (bit_index % 31);
j++;
}
//file[offset + i] = file[offset + i] - 1;
2024-02-21 12:54:33 +00:00
++i;
}
2024-06-15 10:43:59 +00:00
for (size_t i = 0; i < padded_len; i++) {
printf("block : %x\n", padded[i]);//, padded[i]);
padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n);
printf("encrypted block : %x\n\n", padded[i]);//, padded[i]);
//printf("decipher block : %lu (%lx)\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i, pow_mod(padded[i], rsa.d, rsa.n) - 42 - i);
}
memcpy(&file[offset], padded, padded_len * sizeof(uint32_t));
printf("\nENCRYPTION : \n");
printf(" File encrypted from %ld (%lx) to %ld (%lx)\n", offset, offset, offset + size, offset + size);
printf(" Size of encryption = %ld (%lx)\n", size, size);
2024-06-15 10:43:59 +00:00
printf(" Size of padded encryption = %ld (%lx)\n", padded_len * sizeof(uint32_t), padded_len * sizeof(uint32_t));
printf("\n");
2024-06-15 10:43:59 +00:00
return offset + padded_len * sizeof(uint32_t);
}