410 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
way the ASCII numbers are extracted from blockInt has to work backwards, which is why the
for loop on line 59 starts at blockSize - 1 , and then subtracts 1 on each iteration down to
(but not including) - 1. This means the value of i on the last iteration will be 0.
rsaCipher.py
- if len(message) + i < messageLength:
Decode the message string for the 128 (or whatever
blockSize is set to) characters from this block integer.
- asciiNumber = blockInt // (BYTE_SIZE ** i)
- blockInt = blockInt % (BYTE_SIZE ** i)
The length of the message list will be how many characters have been translated from blocks so
far. The if statement on line 60 makes sure the code does not keep computing text from the
block after i has reached the end of the message.
The ASCII number of the next character from the block is calculated by integer dividing
blockInt by (BYTE_SIZE i). Now that we have calculated this character, we can
“remove” it from the block by setting blockInt to the remainder of blockInt divided by
(BYTE_SIZE i). The % mod operator is used to calculate the remainder.
The insert() List Method
While the append() list method only adds values to the end of a list, the insert() list
method can add a value anywhere in the list. The arguments to insert() are an integer index
of where in the list to insert the value, and the value to be inserted. Try typing the following into
the interactive shell:
spam = [2, 4, 6, 8]
spam.insert(0, 'hello')
spam
['hello', 2, 4, 6, 8]
spam.insert(2, 'world')
spam
['hello', 2, 'world', 4, 6, 8]
rsaCipher.py
- blockMessage.insert(0, chr(asciiNumber))
Using the chr() function, the character that asciiNumber is the ASCII number of is inserted
to the beginning of the list at index 0.