nucypher / pyUmbral

NuCypher's reference implementation of Umbral (threshold proxy re-encryption) using OpenSSL and Cryptography.io
https://pyumbral.readthedocs.io
GNU General Public License v3.0
285 stars 71 forks source link

Error while using from_bytes and to_bytes #245

Closed devkatcybersecurity closed 5 years ago

devkatcybersecurity commented 5 years ago

from umbral.curve import SECP256K1

from umbral import pre, keys, config
config.set_default_curve(SECP256K1)
# Generate Umbral keys for Alice.
alices_private_key = keys.UmbralPrivateKey.gen_key()
alices_public_key = alices_private_key.get_pubkey()

alices_signing_key = keys.UmbralPrivateKey.gen_key()
alices_verifying_key = alices_signing_key.get_pubkey()

new_public_key = alices_public_key.to_bytes()
decoded_public_key = keys.UmbralPrivateKey.from_bytes(new_public_key)

It is giving me the error as follows :

Traceback (most recent call last):
  File "original_reencrypt.py", line 14, in <module>
    decoded_public_key = keys.UmbralPrivateKey.from_bytes(new_public_key)
  File "/home/devk/.local/lib/python3.6/site-packages/umbral/keys.py", line 195, in from_bytes
    bn_key = CurveBN.from_bytes(key_bytes, params.curve)
  File "/home/devk/.local/lib/python3.6/site-packages/umbral/curvebn.py", line 96, in from_bytes
    raise ValueError("Expected {} B for CurveBNs".format(size))
ValueError: Expected 32 B for CurveBNs

Any quick fix would be really appreciated

cygnusv commented 5 years ago

Hi! The problem is that you're using the wrong class in the last line. It should be UmbralPublicKey. Try the following:

decoded_public_key = keys.UmbralPublicKey.from_bytes(new_public_key)

devkatcybersecurity commented 5 years ago

Also , one more thing.. how do i convert the bytes object to string and get back the bytes object from string again @cygnusv @jMyles @kikofernandez @KPrasch @michwill @dongsam I want to do something like as follows :

decoded_public_key = keys.UmbralPublicKey.from_bytes(new_public_key)
decoded_public_key_string = str(decoded_public_key)

Now i want to get decoded_public_key again using decoded_public_key_string.

michwill commented 5 years ago

Actually, it's very wrong to cast str() on bytestrings. See:

>> str(b'123')
"b'123'"

You probably want some human-readable (hex?) representation? In this case, you could do:

decoded_public_key_string = decoded_public_key.hex()
converted_back = bytes.fromhex(decoded_public_key_string)