Cryptography Reference
In-Depth Information
if ( S[ 0 ] == 0 && S[ 1 ] == 0 )
{
// First invocation; initialize the state array
for ( i = 0; i < 256; i++ )
{
S[ i ] = i;
}
i = 0;
j = 0;
}
*(ciphertext++) = S[ ( S[ i ] + S[ j ] ) % 256 ] ^ *(plaintext++);
}
state->i = i;
state->j = j;
}
Now, it's up to the caller to initialize a new rc4_state structure, fi ll it with
0's (or zero out at least the fi rst two elements), and pass it into each rc4_operate
call. Technically, you probably ought to defi ne an rc4_initialize function that
does this to make it more explicit — while you're at it, you could and should
defi ne similar functions for DES and AES that compute the key schedule and
store it somewhere so it doesn't need to be recomputed on each iteration. I leave
this as an exercise for you.
One last tweak: Because there are “standard” rc4 key sizes, create a couple
of wrapper functions that identify the key lengths explicitly, as shown in
Listing 2-50.
Listing 2-50: “rc4.c” key-length wrapper functions
void rc4_40_encrypt( const unsigned char *plaintext,
const int plaintext_len,
unsigned char ciphertext[],
void *state,
const unsigned char *key )
{
rc4_operate( plaintext, plaintext_len, ciphertext, key, 5,
( rc4_state * ) state );
}
void rc4_40_decrypt( const unsigned char *ciphertext,
const int ciphertext_len,
unsigned char plaintext[],
void *state,
const unsigned char *key )
{
rc4_operate( ciphertext, ciphertext_len, plaintext, key, 5,
( rc4_state * ) state );
(Continued)
 
Search WWH ::




Custom Search