412 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
256
(2 8)
256
pow(2, 8, 10)
6
(2 8) % 10
6
rsaCipher.py
- for block in getBlocksFromText(message, blockSize):
ciphertext = plaintext ^ e mod n
- encryptedBlocks.append(pow(block, e, n))
- return encryptedBlocks
While creating the public and private keys involved a lot of math, the actual math of the
encryption is simple. The very large integer of the block created from the string in message is
raised to e and then modded by n. This expression evaluates to the encrypted block integer, and
is then appended to encryptedBlocks on line 78.
After all the blocks have been encrypted, the function returns encryptedBlocks on line 79.
rsaCipher.py
- def decryptMessage(encryptedBlocks, messageLength, key,
blockSize=DEFAULT_BLOCK_SIZE):
Decrypts a list of encrypted block ints into the original message
string. The original message length is required to properly decrypt
the last block. Be sure to pass the PRIVATE key to decrypt.
- decryptedBlocks = []
- n, d = key
The math used in the decryptMessage() function is also simple. The decryptedBlocks
variable will store a list of the decrypted integer blocks, and the two integers of the key tuple are
placed in n and d respectively using the multiple assignment trick.
rsaCipher.py
- for block in encryptedBlocks:
plaintext = ciphertext ^ d mod n
- decryptedBlocks.append(pow(block, d, n))
The math of the decryption on line 90 is the same as the encryption’s math, except the integer
block is being raised to d instead of e.