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-16 14:27:37 +00:00
while ( i < ( size + 4 ) ) {
/*if (i < 8) {
printf ( " %x \n " , file [ offset + i ] ) ;
} */
2024-06-15 10:43:59 +00:00
size_t j = 0 ;
2024-06-16 14:27:37 +00:00
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 ;
}
2024-06-15 10:43:59 +00:00
while ( j < 8 ) {
2024-06-16 14:27:37 +00:00
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 ) ;
2024-06-15 10:43:59 +00:00
j + + ;
}
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 + + ) {
2024-06-16 14:27:37 +00:00
printf ( " block : %x \n " , padded [ i ] ) ;
2024-06-15 10:43:59 +00:00
padded [ i ] = pow_mod ( padded [ i ] + 42 + i , 11317 , rsa . n ) ;
2024-06-16 14:27:37 +00:00
printf ( " encrypted block : %x \n \n " , padded [ i ] ) ;
2024-06-15 10:43:59 +00:00
//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 ) ) ;
2024-04-19 09:41:15 +00:00
printf ( " \n ENCRYPTION : \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 ) ) ;
2024-04-19 09:41:15 +00:00
printf ( " \n " ) ;
2024-06-15 10:43:59 +00:00
return offset + padded_len * sizeof ( uint32_t ) ;
}