53 lines
1.8 KiB
C
53 lines
1.8 KiB
C
#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 + 4)) {
|
|
/*if (i < 8) {
|
|
printf("%x\n", file[offset+i]);
|
|
}*/
|
|
size_t j = 0;
|
|
size_t tool = i % 4;
|
|
int tool2 = 0;
|
|
if (tool == 0) {
|
|
tool2 = 3;
|
|
} else if (tool == 1) {
|
|
tool2 = 1;
|
|
} else if (tool == 2) {
|
|
tool2 = -1;
|
|
} else {
|
|
tool2 = -3;
|
|
}
|
|
|
|
while (j < 8) {
|
|
size_t bit_index = i * 8 + j;
|
|
//printf("gonna encrypt index %lu\n", offset + bit_index / 8);
|
|
padded[bit_index / 31] += (1 & (file[offset + bit_index / 8 + tool2] >> (7 - j))) << (30 - bit_index % 31);
|
|
j++;
|
|
}
|
|
++i;
|
|
}
|
|
for (size_t i = 0; i < padded_len; i++) {
|
|
printf("block : %x\n", padded[i]);
|
|
padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n);
|
|
printf("encrypted block : %x\n\n", 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);
|
|
}
|
|
|