Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This Python Code keeps coming back with an error of need more than one value to unpack. I need a detailed explanation if my mistake

This Python Code keeps coming back with an error of need more than one value to unpack. I need a detailed explanation if my mistake and what I have to do to correct it.

Thanks

import sys DEFAULT_BLOCK_SIZE = 128 # 128 bytes BYTE_SIZE = 256 # One byte has 256 different values. def main(): filename = 'encrypted_file.txt' # the file to write to/read from mode = 'encrypt' # set to 'encrypt' or 'decrypt' if mode == 'encrypt': message = '''"Journalists belong in the gutter because that is where the ruling classes throw their guilty secrets." -Gerald Priestland "The Founding Fathers gave the free press the protection it must have to bare the secrets of government and inform the people." -Hugo Black''' pubKeyFilename = 'ghost_pubkey.txt' print('Encrypting and writing to %s...' % (filename)) encryptedText = encryptAndWriteToFile(filename, pubKeyFilename, message) print('Encrypted text:') print(encryptedText) elif mode == 'decrypt': privKeyFilename = 'ghost_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): 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): message = [] for blockInt in blockInts: blockMessage = [] for i in range(blockSize - 1, -1, -1): if len(message) + i < messageLength: asciiNumber = blockInt // (BYTE_SIZE ** i) blockInt = blockInt % (BYTE_SIZE ** i) blockMessage.insert(0, chr(asciiNumber)) message.extend(blockMessage) return ''.join(message) def encryptMessage(message, key, blockSize=DEFAULT_BLOCK_SIZE): encryptedBlocks = [] n, e = key for block in getBlocksFromText(message, blockSize): # ciphertext = plaintext ^ e mod n encryptedBlocks.append(pow(block, e, n)) return encryptedBlocks 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 for block in encryptedBlocks: # plaintext = ciphertext ^ d mod n decryptedBlocks.append(pow(block, d, n)) return getTextFromBlocks(decryptedBlocks, messageLength, blockSize) 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() keySize, n, EorD = content.split(',') return (int(keySize), int(n), int(EorD)) def encryptAndWriteToFile(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE): # Using a key from a key file, encrypt the message and save it to a # file. Returns the encrypted message string. keySize, n, e = readKeyFile(keyFilename) # Check that key size is greater than block size. if keySize < blockSize * 8: # * 8 to convert bytes to bits sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or less than the key size. Either increase the block size or use different keys.' % (blockSize * 8, keySize)) # Encrypt the message encryptedBlocks = encryptMessage(message, (n, e), blockSize) # Convert the large int values to one string value. for i in range(len(encryptedBlocks)): encryptedBlocks[i] = str(encryptedBlocks[i]) encryptedContent = ','.join(encryptedBlocks) # Write out the encrypted string to the output file. encryptedContent = '%s_%s_%s' % (len(message), blockSize, encryptedContent) fo = open(messageFilename, 'w') fo.write(encryptedContent) fo.close() # Also return the encrypted string. return encryptedContent def readFromFileAndDecrypt(messageFilename, keyFilename): # Using a key from a key file, read an encrypted message from a file # and then decrypt it. Returns the decrypted message string. keySize, n, d = readKeyFile(keyFilename) # Read in the message length and the encrypted message from the file. fo = open(messageFilename) content = fo.read() messageLength, blockSize, encryptedMessage = content.split('_') messageLength = int(messageLength) blockSize = int(blockSize) # Check that key size is greater than block size. if keySize < blockSize * 8: # * 8 to convert bytes to bits sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or less than the key size. Did you specify the correct key file and encrypted file?' % (blockSize * 8, keySize)) # Convert the encrypted message into large int values. encryptedBlocks = [] for block in encryptedMessage.split(','): encryptedBlocks.append(int(block)) return decryptMessage(encryptedBlocks, messageLength, (n, d), blockSize) if __name__ == '__main__': main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions