#include "../includes/woody.h" #include "../includes/rsa.h" unsigned long encrypt(char *file, unsigned long int offset, unsigned long int size, rsa_t rsa) { 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; size_t i = 0; 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; ++i; } 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); printf(" Size of padded encryption = %ld (%lx)\n", padded_len * sizeof(uint32_t), padded_len * sizeof(uint32_t)); printf("\n"); return offset + padded_len * sizeof(uint32_t); }