392 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
Founding Fathers gave the free press the protection it must have to bare the
secrets of government and inform the people." -Hugo Black'''
- pubKeyFilename = 'al_sweigart_pubkey.txt'
- print('Encrypting and writing to %s...' % (filename))
- encryptedText = encryptAndWriteToFile(filename, pubKeyFilename,
message)
- print('Encrypted text:')
- print(encryptedText)
- elif mode == 'decrypt':
- privKeyFilename = 'al_sweigart_privkey.txt'
- print('Reading from %s and decrypting...' % (filename))
- decryptedText = readFromFileAndDecrypt(filename, privKeyFilename)
- print('Decrypted text:')
- print(decryptedText)
- def getBlocksFromText(message, blockSize=DEFAULT_BLOCK_SIZE):
Converts a string message to a list of block integers. Each integer
represents 128 (or whatever blockSize is set to) string characters.
- messageBytes = message.encode('ascii') # convert the string to bytes
- blockInts = []
- for blockStart in range(0, len(messageBytes), blockSize):
Calculate the block integer for this block of text
- blockInt = 0
- for i in range(blockStart, min(blockStart + blockSize,
len(messageBytes))): - blockInt += messageBytes[i] * (BYTE_SIZE ** (i % blockSize))
- blockInts.append(blockInt)
- return blockInts
- def getTextFromBlocks(blockInts, messageLength,
blockSize=DEFAULT_BLOCK_SIZE):
Converts a list of block integers to the original message string.
The original message length is needed to properly convert the last
block integer.
- message = []
- for blockInt in blockInts:
- blockMessage = []
- for i in range(blockSize - 1, -1, -1):
- if len(message) + i < messageLength:
Decode the message string for the 128 (or whatever