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.

ol = 0;<br />

if (!(ret = (char *)malloc(inl + EVP_CIPHER_CTX_block_size(ctx)))) abort( );<br />

for (i = 0; i < inl / 100; i++) {<br />

if (!EVP_EncryptUpdate(ctx, &ret[ol], &tmp, &data[ol], 100)) abort( );<br />

ol += tmp;<br />

}<br />

if (inl % 100) {<br />

if (!EVP_EncryptUpdate(ctx, &ret[ol], &tmp, &data[ol], inl % 100)) abort( );<br />

ol += tmp;<br />

}<br />

if (!EVP_EncryptFinal_ex(ctx, &ret[ol], &tmp)) abort( );<br />

ol += tmp;<br />

if (rb) *rb = ol;<br />

return ret;<br />

}<br />

Here’s a simple function for decryption that decrypts an entire message at once:<br />

#include <br />

#include <br />

char *decrypt_example(EVP_CIPHER_CTX *ctx, char *ct, int inl) {<br />

/* We're going to null-terminate the plaintext under the assumption that it's<br />

* non-null terminated ASCII text. The null can otherwise be ignored if it<br />

* wasn't necessary, though the length of the result should be passed back in<br />

* such a case.<br />

*/<br />

int ol;<br />

char *pt;<br />

if (!(pt = (char *)malloc(inl + EVP_CIPHER_CTX_block_size(ctx) + 1))) abort( );<br />

EVP_DecryptUpdate(ctx, pt, &ol, ct, inl);<br />

if (!ol) { /* There is no data to decrypt */<br />

free(pt);<br />

return 0;<br />

}<br />

pt[ol] = 0;<br />

return pt;<br />

}<br />

See Also<br />

Recipes 5.16, 5.17<br />

5.23 Setting Up and Using RC4<br />

<strong>Problem</strong><br />

You want to use RC4 securely.<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 | 233

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

Saved successfully!

Ooh no, something went wrong!