pyUmbral/docs/notebooks/pyUmbral Simple API.ipynb

198 lines
4.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# pyUmbral python API"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting the default curve\n",
"\n",
"The first time you use umbral, you may want to specify an elliptic curve to use. If you do not specify a curve, secp256k1 will be used for all operations, with a slight performace hit for the lookup.\n",
"\n",
"To set the default curve use `umbral.config.set_default_curve()`\n",
"\n",
"Note: you can only set the dafault once, or `UmbralConfigurationError` will be raised."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from umbral.config import set_default_curve\n",
"from cryptography.hazmat.primitives.asymmetric import ec\n",
"\n",
"set_default_curve(ec.SECP256K1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate Umbral Keys"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from umbral import keys\n",
"\n",
"\n",
"# Alice's Keys\n",
"alice_private_key = keys.UmbralPrivateKey.gen_key()\n",
"alice_public_key = alice_private_key.get_pubkey()\n",
"\n",
"# Bob's Keys\n",
"bob_private_key = keys.UmbralPrivateKey.gen_key()\n",
"bob_public_key = bob_private_key.get_pubkey()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Alice encrypts data; Generates a capsule"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from umbral import umbral\n",
"\n",
"\n",
"plaintext = b'attack at dawn'\n",
"ciphertext, capsule = umbral.encrypt(alice_public_key, plaintext)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Alice decrypts data for self"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'attack at dawn'\n"
]
}
],
"source": [
"cleartext = umbral.decrypt(capsule, alice_private_key, ciphertext)\n",
"print(cleartext)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Proxy Re-encryption\n",
"\n",
"![proxy_reencryption](https://cdn-images-1.medium.com/max/1200/0*yTKUeeuKPu-aIZdw.)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Split Rekeys"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"N, M = 1, 1\n",
"kfrags, vkeys = umbral.split_rekey(alice_private_key, bob_public_key, M, N)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Ursula Re-encrypts; Bob attaches fragments to capsule\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"for kfrag in kfrags:\n",
" cfrag = umbral.reencrypt(kfrag, capsule)\n",
" capsule.attach_cfrag(cfrag)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bob activates and opens the capsule; Decrypts data from Alice."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'attack at dawn'\n"
]
}
],
"source": [
"reencrypted_cleartext = umbral.decrypt(capsule, bob_private_key, ciphertext, alice_public_key)\n",
"print(reencrypted_cleartext)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}