Cryptography Reference
In-Depth Information
contents of the last byte of the decrypted output, which contains the number of
padding bytes that were appended. Then replace the byte at that position, from
the end with a null terminator, effectively discarding the padding. You don't
want this in a general-purpose decryption routine, though, because it doesn't
deal properly with binary input and because, in SSL, the caller is responsible
for ensuring that the input is block-aligned.
To see this in action, you can add a main routine to your des.c fi le so that you
can do DES encryption and decryption operations from the command line. To
enable compilation of this as a test app as well as an included object in another
app — which you do when you add this to your SSL library — wrap up the
main routine in an #ifdef as shown in Listing 2-22.
Listing 2-22: “des.c” command-line test routine
#ifdef TEST_DES
int main( int argc, char *argv[ ] )
{
unsigned char *key;
unsigned char *iv;
unsigned char *input;
unsigned char *output;
int out_len, input_len;
if ( argc < 4 )
{
fprintf( stderr, “Usage: %s <key> <iv> <input>\n”, argv[ 0 ] );
exit( 0 );
}
key = argv[ 1 ];
iv = argv[ 2 ];
input = argv[ 3 ];
out_len = input_len = strlen( input );
output = ( unsigned char * ) malloc( out_len + 1 );
des_encrypt( input, input_len, output, iv, key );
while ( out_len-- )
{
printf( “%.02x”, *output++ );
}
printf( “\n” );
return 0;
}
#endif
Notice that the input must be an even multiple of eight. If you give it bad data,
the program just crashes unpredictably. The output is displayed in hexadecimal
 
Search WWH ::




Custom Search