def test_save_load_file(self): """ Test that we can encrypt a message, save it to file, load it again and decrypt it. """ # Get a new private key object key = self.cryptosystem.new_key_pair().private_key # 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 # PrivateKey'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 PrivateKey.from_file(...) recovered_key = PrivateKey.from_file(file_path) # Check that the recovered key is the same as the original key self.assertEqual(recovered_key, key) self.assertFalse(recovered_key != key) # 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)
def __init__(self, cryptosystem): """ Generates a new key pair for the given EGCryptoSystem """ p = cryptosystem.get_prime() random = StrongRandom() inner_private_key = random.randint(1, p - 2) self.private_key = PrivateKey(cryptosystem, inner_private_key) self.public_key = self.private_key.public_key