pyUmbral/docs/examples/umbral_simple_api.py

77 lines
2.3 KiB
Python
Raw Normal View History

2018-07-25 17:55:12 +00:00
"""
Copyright (C) 2018 NuCypher
This file is part of pyUmbral.
pyUmbral is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pyUmbral is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyUmbral. If not, see <https://www.gnu.org/licenses/>.
"""
2018-02-16 20:36:07 +00:00
#1
# Sets a default curve (secp256k1)
import random
2018-02-22 18:27:36 +00:00
from umbral import pre, keys, config
2018-02-16 20:36:07 +00:00
config.set_default_curve()
#2
# Generate keys for Alice and Bob
alice_priv_key = keys.UmbralPrivateKey.gen_key()
alice_pub_key = alice_priv_key.get_pubkey()
bob_priv_key = keys.UmbralPrivateKey.gen_key()
bob_pub_key = bob_priv_key.get_pubkey()
#3
# Encrypt some data for Alice
plaintext = b'Proxy Re-encryption is cool!!'
2018-02-22 18:27:36 +00:00
alice_ciphertext, umbral_capsule = pre.encrypt(alice_pub_key, plaintext)
2018-02-16 20:36:07 +00:00
print(alice_ciphertext)
#4
# Decrypt data for Alice
2018-04-25 10:02:35 +00:00
alice_decrypted_data = pre.decrypt(alice_ciphertext, umbral_capsule, alice_priv_key, alice_pub_key)
2018-02-16 20:36:07 +00:00
print(alice_decrypted_data)
#5
# Bob receives a capsule through a side channel (s3, ipfs, Google cloud, etc)
bob_capsule = umbral_capsule
#6
# Attempt Bob's decryption (fail)
try:
2018-04-25 10:02:35 +00:00
fail_decrypted_data = pre.decrypt(alice_ciphertext, bob_capsule, bob_priv_key, alice_pubkey)
2018-02-16 20:36:07 +00:00
except:
print("Decryption failed!")
#7
# Generate threshold split re-encryption keys via Shamir's Secret Sharing
# Use Alice's private key, and Bob's public key.
# Use a minimum threshold of 10, and create 20 total shares
2018-02-26 14:44:17 +00:00
kfrags = pre.split_rekey(alice_priv_key, bob_pub_key, 10, 20)
2018-02-16 20:36:07 +00:00
#8
# Have Ursula perform re-encrypton.
# Pick 10 random shares:
rand_min_shares = random.sample(kfrags, 10)
# Have Ursula re-encrypt the shares and attach them to the capsule:
2018-02-24 09:47:18 +00:00
for kfrag in rand_min_shares:
2018-02-22 18:27:36 +00:00
cfrag = pre.reencrypt(kfrag, umbral_capsule)
2018-02-16 20:36:07 +00:00
bob_capsule.attach_cfrag(cfrag)
#9
# Bob reconstructs the capsule and decrypts the ciphertext:
2018-04-25 10:02:35 +00:00
bob_plaintext = pre.decrypt(alice_ciphertext, bob_capsule, bob_priv_key, alice_pub_key)
2018-02-16 20:36:07 +00:00
print(bob_plaintext)