Chapter 24 – Public Key Cryptography and the RSA Cipher 415
- encryptedBlocks[i] = str(encryptedBlocks[i])
- encryptedContent = ','.join(encryptedBlocks)
The join() method will return a string of the blocks separated by commas, but join() only
works on lists with string values, and encryptedBlocks is a list of integers. These integers
will have to first be converted into strings.
The for loop on line 118 iterates through each index in encryptedBlocks, replacing the
integer at encryptedBlocks[i] with a string form of the integer. When the loop completes,
encryptedBlocks now contains a list of string values instead of a list of integer values.
The list of string values is passed to the join() method, which returns a single string of the
list’s strings joined together with commas. Line 120 stores this string in a variable named
encryptedContent.
rsaCipher.py
Write out the encrypted string to the output file.
- encryptedContent = '%s%s%s' % (len(message), blockSize,
encryptedContent)
We want to write out more than just the encrypted integer blocks to the file though, so line 123
changes the encryptedContent variable to include the size of the message (as an integer),
followed by an underscore, followed by the blockSize (which is also an integer), followed by
another underscore, and then followed by the encrypted integer blocks.
rsaCipher.py
124. fo = open(messageFilename, 'w')
125. fo.write(encryptedContent)
126. fo.close()
The last step is to write out the contents of the encrypted file. The filename provided by the
messageFilename parameter is created with the call to open() on line 124. (The 'w'
argument tells open() to open the file in “write mode”.) Note that if a file with this name
already exists, then it will be overwritten by the new file.
The string in encryptedContent is written to the file by calling the write() method on
line 125. Now that we are done writing the file’s contents, line 126 closes the file object in fo.
rsaCipher.py
Also return the encrypted string.
- return encryptedContent