Chapter 24 – Public Key Cryptography and the RSA Cipher 409
After line 43’s for loop has finished, all of the block integers have been calculated and are
stored in the blockInts list. Line 49 returns blockInts from getBlocksFromText().
rsaCipher.py
52. def getTextFromBlocks(blockInts, messageLength,
blockSize=DEFAULT_BLOCK_SIZE):
53. # Converts a list of block integers to the original message string.
54. # The original message length is needed to properly convert the last
55. # block integer.
56. message = []
- for blockInt in blockInts:
The getTextFromBlocks() function does the opposite of getBlocksFromText(). This
function is passed a list of block integers (as the blockInts parameter) and returns the string
value that these blocks represent. The function needs the length of the message encoded in
messageLength, since this information is needed to get the string from the last block integer if
it is not a full 128 characters in size.
Just as before, blockSize will default to DEFAULT_BLOCK_SIZE if no third argument is
passed to getTextFromBlocks(), and DEFAULT_BLOCK_SIZE was set to 128 on line 9.
The message list (which starts as blank on line 56) will contain a string value for each character
that was computed from each block integer in blockInts. (This list of strings will be joined
together to form the complete string at the end of getTextFromBlocks().) The message
list starts off empty on line 56. The for loop on line 57 iterates over each block integer in the
blockInts list.
rsaCipher.py
- blockMessage = []
- for i in range(blockSize - 1, -1, -1):
Inside the for loop, the code from lines 58 to 65 calculates the letters that are in the current
iteration’s block. Recall from Chapter 15’s affine cipher program how one integer key was split
into two integer keys:
- def getKeyParts(key):
- keyA = key // len(SYMBOLS)
- keyB = key % len(SYMBOLS)
- return (keyA, keyB)
The code in getTextFromBlocks() works in a similar way, except the single integer (i.e. the
block integer) is split into 128 integers (and each is the ASCII value for a single character). The