Hacking Secret Ciphers with Python

(Ann) #1
Chapter 24 – Public Key Cryptography and the RSA Cipher 403

Table 24-2. Encoding a string into a block.
Index Character ASCII
Number

Multiplied
By

Number

0 H 72 × 256 ^ 0 = 72
1 e 101 × 256 ^ 1 = 25,856
2 l 108 × 256 ^ 2 = 7,077,888
3 l 108 × 256 ^ 3 = 1,811,939,328
4 o 111 × 256 ^ 4 = 476,741,369,856
5 (space) 32 × 256 ^ 5 = 35,184,372,088,832
6 w 119 × 256 ^ 6 = 33,495,522,228,568,064
7 o 111 × 256 ^ 7 = 7,998,392,938,210,000,896
8 r 114 × 256 ^ 8 = 2,102,928,824,402,888,884,224
9 l 108 × 256 ^ 9 = 510,015,580,149,921,683,079,168
10 d 100 × 256 ^ 10 = 120,892,581,961,462,917,470,617,600
11! 33 × 256 ^ 11 = 10,213,005,324,104,387,267,917,774,848
SUM: 10,334,410,032,606,748,633,331,426,632

(You might have noticed that the RSA cipher does a lot of math with big numbers.)


So the string 'Hello world!' when put into a single large integer “block” becomes the
integer 10,334,410,032,606,748,633,331,426,632. This integer uniquely refers to the string
'Hello world!'. By continuing to use larger and larger powers of 256, any string possible
has exactly one large integer. For example, 2 , 175 , 540 is the integer for '42!' and
17,802,628,493,700,941 is the integer for 'Moose??' and
23,071,981,395,336,227,453,293,155,570,939,985,398,502,658,016,284,755,880,397,214,576,11
0,064,091,578,359,739,349,325 is the integer for 'My cat's breath smells like cat
food.'.


Because our block size is 128 bytes, we can only encrypt up to 128 characters in a single block.
But we can just use more blocks if the message is longer than 128 characters. The RSA cipher
program will separate the blocks it outputs with commas so we can tell when one block ends and
the next one begins.


As an example, here’s a message that is split into blocks, and the integer that represents each
block (calculated using the same method in Table 24-2.). Each block has at most 128 characters
of the message.

Free download pdf