21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The “official” RC4 key setup function isn’t generally secure without additional work,<br />

but we need to have it around anyway:<br />

#include <br />

void RC4_set_key(RC4_CTX *c, size_t keybytes, unsigned char *key) {<br />

int i, j;<br />

unsigned char keyarr[256], swap;<br />

c->i = c->j = 0;<br />

for (i = j = 0; i < 256; i++, j = (j + 1) % keybytes) {<br />

c->sbox[i] = i;<br />

keyarr[i] = key[j];<br />

}<br />

for (i = j = 0; i < 256; i++) {<br />

j += c->sbox[i] + keyarr[i];<br />

j %= 256;<br />

swap = c->sbox[i];<br />

c->sbox[i] = c->sbox[j];<br />

c->sbox[j] = swap;<br />

}<br />

}<br />

The RC4 function has the following arguments:<br />

c<br />

Pointer to an RC4_CTX object.<br />

n<br />

Number of bytes to encrypt.<br />

in<br />

Buffer to encrypt.<br />

out<br />

Output buffer.<br />

void RC4(RC4_CTX *c, size_t n, unsigned char *in, unsigned char *out) {<br />

unsigned char swap;<br />

while (n--) {<br />

c->j += c->sbox[++c->i];<br />

swap = c->sbox[c->i];<br />

c->sbox[c->i] = c->sbox[c->j];<br />

c->sbox[c->j] = swap;<br />

swap = c->sbox[c->i] + c->sbox[c->j];<br />

*out++ = *in++ ^ c->sbox[swap];<br />

}<br />

}<br />

That’s it for an RC4 implementation. This function can be used incrementally or as<br />

an “all-in-one” solution.<br />

Now let’s look at how to key RC4 properly.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Setting Up and Using RC4 | 235

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!