Hacking Secret Ciphers with Python

(Ann) #1

406 http://inventwithpython.com/hacking


Email questions to the author: [email protected]








You can also directly type a bytes object into your source code just like you type a string or list.
A bytes object has the letter b right before what looks like an ordinary string value. But
remember, the letter b right before the quotes means that this is a bytes value, not a string value.
Try typing the following into the interactive shell, making sure that there is no space between the
b and the quote characters:





spam = b'hello'
list(spam)
[104, 101, 108, 108, 111]





We don’t use the decode() bytes method in this program, but you should know about it. It does
the opposite of the encode() string method. When called on a bytes object, the decode()
method returns a string made from the values stored in the bytes object. Try typing the following
into the interactive shell:





spam = bytes([104, 101, 108, 108, 111])
spam.decode('ascii')
'hello'





Practice Exercises, Chapter 24, Set C


Practice exercises can be found at http://invpy.com/hackingpractice 24 C.


Back to the Code


rsaCipher.py


  1. blockInts = []

  2. for blockStart in range(0, len(messageBytes), blockSize):


The blockInts list will contain the large integer “blocks” form of the characters in message.
The blockSize parameter is set to DEFAULT_BLOCK_SIZE by default, and the
DEFAULT_BLOCK_SIZE constant was set to 128 (meaning, 128 bytes) on line 9. This means
that each large integer block can only store 128 string characters at most (since 1 ASCII character
takes up 1 byte). See Table 24-3 for an example of a message split into 128-character blocks.


Line 43’s for loop will set the value in blockStart so that on each iteration it will be set to
the index of the block being created. For example, if blockSize is set to 128 , then the index of

Free download pdf