Пример #1
0
 def test_load_threshold(self):
     """
     Test that we can load a threshold public as a PublicKey object
     """
     file_path = os.path.join(os.path.dirname(__file__), 
                              "TestBasicEncryption.resources",
                              "test_threshold_pk.pvpubkey")
     
     recovered_key = PublicKey.from_file(file_path)
Пример #2
0
def run_tool(key_file, in_file, out_file):
    """
	Runs the plonevote.encrypt tool and encrypts in_file into out_file.
	"""
    # Load the public key
    print "Loading public key..."
    try:
        public_key = PublicKey.from_file(key_file)
    except InvalidPloneVoteCryptoFileError, e:
        print "Invalid public key file (%s): %s" % (key_file, e.msg)
        sys.exit(2)
Пример #3
0
def run_tool(key_file, in_file, out_file):
	"""
	Runs the plonevote.encrypt tool and encrypts in_file into out_file.
	"""
	# Load the public key
	print "Loading public key..."
	try:
		public_key = PublicKey.from_file(key_file)
	except InvalidPloneVoteCryptoFileError, e:
		print "Invalid public key file (%s): %s" % (key_file, e.msg)
		sys.exit(2)
Пример #4
0
 def test_save_load_file(self):
     """
     Test that we can correctly save a PublicKey to a file and load it 
     back. 
     """
     # Get a new public key object
     key = self.cryptosystem.new_key_pair().public_key
     
     # Get its fingerprint for comparison after deserialization
     original_fingerprint = key.get_fingerprint()
     
     # 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 
     # PublicKey'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 key using to_file(...)
     key.to_file(file_path)
     
     # Load it back using PublicKey.from_file(...)
     recovered_key = PublicKey.from_file(file_path)
                                      
     # Get the fingerprint of the recovered key
     recovered_fingerprint = recovered_key.get_fingerprint()
     
     # Check that the fingerprints match (and thus they're the same key)
     self.assertEqual(recovered_fingerprint, original_fingerprint)
     
     # Also check that the == operator recognizes them as equal
     self.assertEqual(recovered_key, key)
     self.assertFalse(recovered_key != key)
     
     # Delete the temporary file
     os.remove(file_path)
Пример #5
0
def run_tool(key_file, in_file, out_file):
    """
	Runs the plonevote.encrypt tool and encrypts in_file into out_file.
	"""
    # Load the public key
    print("Loading public key...")
    try:
        public_key = PublicKey.from_file(key_file)
    except InvalidPloneVoteCryptoFileError as e:
        print("Invalid public key file (%s): %s" % (key_file, e.msg))
        sys.exit(2)

    # Open the input file
    print("Reading input file...")
    try:
        in_f = open(in_file, 'rb')
    except Exception as e:
        print("Problem while opening input file %s: %s" % (in_file, e))

    # Read the whole file into a bitstream
    bitstream = BitStream()
    try:
        read_quantum = 1024  # KB at a time
        bytes = in_f.read(read_quantum)
        while (bytes):
            for byte in bytes:
                bitstream.put_byte(ord(byte))
            bytes = in_f.read(read_quantum)
    except Exception as e:
        print("Problem while reading from input file %s: %s" % (in_file, e))

    in_f.close()

    # Define callbacks for the TaskMonitor for monitoring the encryption 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):
        print("  %.2f%% of %s encrypted..." % \
          (task.get_percent_completed(), short_in_filename))

    # Create new TaskMonitor and register the callbacks
    taskmon = TaskMonitor()
    taskmon.add_on_progress_percent_callback(cb_task_percent_progress, \
               percent_span = 5)

    # Encrypt bitstream
    print("Encrypting...")
    ciphertext = public_key.encrypt_bitstream(bitstream, task_monitor=taskmon)

    # Save the ciphertext to the output file
    try:
        ciphertext.to_file(out_file)
    except Exception as e:
        print("Problem while saving the output file %s: %s" % (in_file, e.msg))