def test_save_load_file(self): """ Test that we can correctly save a PrivateKey to a file and load it back. """ # Use the public key to encrypt the message ciphertext = self.public_key.encrypt_text(self.message) # Get a temporary file object using tempfile (file_object, file_path) = tempfile.mkstemp() # We close the file descriptor since we will not be using it, instead # Ciphertext's methods take the filename and open/close the file as # needed. # Note that using mkstemp() instead tempfile.TemporaryFile means the # file remains in the filesystem even after it is closed. os.close(file_object) # Save the ciphertext using to_file(...) ciphertext.to_file(file_path) # Load it back using Ciphertext.from_file(...) recovered_ciphertext = Ciphertext.from_file(file_path) # Check that the recovered ciphertext is recognized as equal to the # original ciphertext self.assertEqual(recovered_ciphertext, ciphertext) self.assertFalse(recovered_ciphertext != ciphertext) # then use the private key to decrypt the deserialized ciphertext recovered_message = \ self.private_key.decrypt_to_text(recovered_ciphertext) # Check that the message was recovered correctly self.assertEqual(recovered_message, self.message) # Delete the temporary file os.remove(file_path)
def run_tool(key_file, in_file, out_file): """ Runs the plonevote.decrypt tool and decrypts in_file into out_file. """ # Load the private key print "Loading private key..." try: private_key = PrivateKey.from_file(key_file) except InvalidPloneVoteCryptoFileError, e: print "Invalid private key file (%s): %s" % (key_file, e.msg) sys.exit(2) # Open the input encrypted data print "Loading encrypted data..." try: ciphertext = Ciphertext.from_file(in_file) except InvalidPloneVoteCryptoFileError, e: print "Invalid PloneVote encrypted file (%s): %s" % (key_file, e.msg) sys.exit(2) # Define callbacks for the TaskMonitor for monitoring the decryption process if(len(in_file) <= 50): short_in_filename = in_file else: short_in_filename = os.path.split(in_file)[-1] if(len(short_in_filename) > 50): # Do ellipsis shortening short_in_filename = short_in_filename[0,20] + "..." + \ short_in_filename[-20,-1] def cb_task_percent_progress(task):
def run_tool(key_file, in_file, out_file): """ Runs the plonevote.decrypt tool and decrypts in_file into out_file. """ # Load the private key print "Loading private key..." try: private_key = PrivateKey.from_file(key_file) except InvalidPloneVoteCryptoFileError, e: print "Invalid private key file (%s): %s" % (key_file, e.msg) sys.exit(2) # Open the input encrypted data print "Loading encrypted data..." try: ciphertext = Ciphertext.from_file(in_file) except InvalidPloneVoteCryptoFileError, e: print "Invalid PloneVote encrypted file (%s): %s" % (key_file, e.msg) sys.exit(2) # Define callbacks for the TaskMonitor for monitoring the decryption process if (len(in_file) <= 50): short_in_filename = in_file else: short_in_filename = os.path.split(in_file)[-1] if (len(short_in_filename) > 50): # Do ellipsis shortening short_in_filename = short_in_filename[0,20] + "..." + \ short_in_filename[-20,-1] def cb_task_percent_progress(task):