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.

This would map to the hexadecimal value:<br />

0x54657374<br />

If you simply treat the hexadecimal value as a number, you’ll get the integer representation.<br />

In base 10, the previous number would be 1415934836.<br />

If, for some reason, you need to calculate this value manually given the ASCII values<br />

of the integers, you would compute the following:<br />

84×256 3 + 101×256 2 + 115×256 1 + 116×256 0<br />

In the real world, your arbitrary-precision math library will probably have a way to<br />

turn binary strings into numbers that is compatible with the PKCS algorithm. For<br />

example, OpenSSL provides BN_bin2bn( ), which is discussed in Recipe 7.4.<br />

If you need to perform this conversion yourself, make sure that your numerical representation<br />

uses either an array of char values or an array of unsigned int values. If you<br />

use the former, you can use the binary string directly as a number. If you use the latter,<br />

you will have to byte-swap each word on a little-endian machine before treating<br />

the string as a number. On a big-endian machine, you need not perform any swap.<br />

See Also<br />

• PKCS #1 page: http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/<br />

• Recipe 7.4<br />

7.9 Converting Integers into Binary Strings for<br />

Use with RSA<br />

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

You have a number as a result of an RSA operation that you’d like to turn into a<br />

binary string of a fixed length.<br />

Solution<br />

Use the inverse of the previous recipe, padding the start of the string with zero-bits, if<br />

necessary, to reach the desired output length. If the number is too big, return an<br />

error.<br />

Discussion<br />

In practice, you should be using a binary representation of very large integers that<br />

stores a value as an array of values of type unsigned int or type char. If you’re using a<br />

Converting Integers into Binary Strings for Use with RSA | 331<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!