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 previous code is subtly incompatible with the S/KEY dictionary<br />

because their dictionary is not in alphabetical order. (S/KEY is an<br />

authentication system using one-time passwords.) Be sure to use the<br />

right dictionary!<br />

The code is written in such a way that you can use dictionaries of different sizes if<br />

you wish to encode a different number of bits per word. Currently, the dictionary<br />

encodes 11 bits of data (by having exactly 211 words), where no word is more than 4<br />

characters long. The web site also provides a dictionary that encodes 13 bits of data,<br />

where no word is more than 6 letters long. The previous code can be modified to use<br />

the larger dictionary simply by changing the two appropriate preprocessor definitions<br />

at the top.<br />

The algorithm takes 11 bits of the binary string, then finds the word that maps to the<br />

unique 11-bit value. Note that it is rare for the number of bits represented by a single<br />

word to align exactly to a byte. For example, if you were to encode a 2-byte<br />

binary string, those 16 bits would be encoded by 2 words, which could represent up<br />

to 22 bits. Therefore, there will usually be leftover bits. In the case of 2 bytes, there<br />

are 6 leftover bits. The algorithm sets all leftover bits to 0.<br />

Because of this padding scheme, the output doesn’t always encode how many bytes<br />

were in the input. For example, if the output is 6 words long, the input could have<br />

been either 7 or 8 bytes long. Therefore, you need to manually truncate the output to<br />

the desired length.<br />

See Also<br />

Recipe 4.8<br />

4.8 Converting Text Keys to Binary Keys<br />

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

A user enters a textual representation of a key or other binary data (see Recipe 4.7).<br />

You need to convert it to binary.<br />

Solution<br />

Parse out the words, then look them up in the dictionary to reconstruct the actual<br />

bits, as shown in the code included in the next section.<br />

130 | Chapter 4: Symmetric Cryptography Fundamentals<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!