Chapter 24 – Public Key Cryptography and the RSA Cipher 413
rsaCipher.py
- return getTextFromBlocks(decryptedBlocks, messageLength, blockSize)
The decrypted blocks along with the messageLength and blockSize parameters are passed
getTextFromBlocks() so that the decrypted plaintext as a string is returned from
decryptMessage().
Reading in the Public & Private Keys from their Key Files
rsaCipher.py
- def readKeyFile(keyFilename):
Given the filename of a file that contains a public or private key,
return the key as a (n,e) or (n,d) tuple value.
- fo = open(keyFilename)
- content = fo.read()
- fo.close()
The key files that makeRsakeys.py creates look like this:
The readKeyFile() function is called to read the key size, n, and e (for the public key) or d
(for the private key) values from the key file. Lines 97 to 99 open this file and read in the contents
as a string into the content variable.
rsaCipher.py
- keySize, n, EorD = content.split(',')
- return (int(keySize), int(n), int(EorD))
The split() string method splits up the string in content along the commas. The list that
split() returns will have three items in it, and the multiple assignment trick will place each of
these items into the keySize, n, and EorD variables respectively on line 100.
Remember that content was a string when it was read from the file, and the items in the list
that split() returns will also be string values. So before returning the keySize, n, and EorD
values, they are each passed to int() to return an integer form of the value. This is how
readKeyFile() returns three integers that were read from the key file.
The Full RSA Encryption Process
rsaCipher.py
- def encryptAndWriteToFile(messageFilename, keyFilename, message,
blockSize=DEFAULT_BLOCK_SIZE):