Cryptography Reference
In-Depth Information
( ( tolower( input[ i + 1 ] ) ) - 'a' + 10 ) );
}
}
return len;
}
Whether the input starts with “0x” or not, the decoded pointer is initialized
and fi lled with either the unchanged input or the byte value of the hex-decoded
input — minus, of course, the “0x” leader. While you're at it, go ahead and move
the simple hex_display routine that was at the end of des.c's main routine into
a reusable utility function as shown in Listing 2-24.
Listing 2-24: “hex.c” show_hex
void show_hex( const unsigned char *array, int length )
{
while ( length-- )
{
printf( “%.02x”, *array++ );
}
printf( “\n” );
}
Now, des.c's main function becomes what's shown in Listing 2-25.
Listing 2-25: “des.c” main routine with decryption support
int main( int argc, char *argv[ ] )
{
unsigned char *key;
unsigned char *iv;
unsigned char *input;
int key_len;
int input_len;
int out_len;
int iv_len;
unsigned char *output;
if ( argc < 4 )
{
fprintf( stderr, “Usage: %s [-e|-d] <key> <iv> <input>\n”, argv[ 0 ] );
exit( 0 );
}
key_len = hex_decode( argv[ 2 ], &key );
iv_len = hex_decode( argv[ 3 ], &iv );
input_len = hex_decode( argv[ 4 ], &input );
out_len = input_len;
Search WWH ::




Custom Search