Cryptography Reference
In-Depth Information
because it's almost defi nitely not going to be printable ASCII. Alternatively, you
could have Base64-encoded this, but using hex output leaves it looking the same
as the network traces presented later. You have to provide a -DTEST_DES fl ag to
the compiler when building this:
gcc -DTEST_DES -g -o des des.c
After this has been compiled, you can invoke it via
[jdavies@localhost ssl]$ ./des password initialz abcdefgh
71828547387b18e5
Just make sure that the input is block-aligned. The key and the initialization
vector must be eight bytes, and the input must be a multiple of eight bytes.
What about decryption? You likely want to see this decrypted, but the output
isn't in printable-ASCII form and you have no way to pass this in as a command-
line parameter. Expand the input to allow the caller to pass in hex-coded values
instead of just printable-ASCII values. You can implement this just like C does;
if the user starts an argument with “0x,” the remainder is assumed to be a hex-
coded byte array. Because this hex-parsing routine is useful again later, put it
into its own utility fi le, shown in Listing 2-23.
Listing 2-23: “hex.c” hex_decode
/**
* Check to see if the input starts with “0x”; if it does, return the decoded
* bytes of the following data (presumed to be hex coded). If not, just return
* the contents. This routine allocates memory, so has to be free'd.
*/
int hex_decode( const unsigned char *input, unsigned char **decoded )
{
int i;
int len;
if ( strncmp( “0x”, input, 2 ) )
{
len = strlen( input ) + 1;
*decoded = malloc( len );
strcpy( *decoded, input );
len--;
}
else
{
len = ( strlen( input ) >> 1 ) - 1;
*decoded = malloc( len );
for ( i = 2; i < strlen( input ); i += 2 )
{
(*decoded)[ ( ( i / 2 ) - 1 ) ] =
( ( ( input[ i ] <= '9' ) ? input[ i ] - '0' :
( ( tolower( input[ i ] ) ) - 'a' + 10 ) ) << 4 ) |
( ( input[ i + 1 ] <= '9' ) ? input[ i + 1 ] - '0' :
(Continued)
 
Search WWH ::




Custom Search