3

I have the following script, crypto.py in Python 2:

import hashlib
from cryptography.fernet import Fernet

def make_hash(password):
    return hashlib.sha224(password).hexdigest()

def check_hash(password, hash):
    """Check a password against an existing hash."""
    return hashlib.sha224(password).hexdigest() == hash

def start():
    key = Fernet.generate_key()
    print key
    cipher_suite = Fernet(key)
    print cipher_suite
    return cipher_suite

def crypt(message):
    global cipher_suite
    cipher_suite=start()
    cipher_text = cipher_suite.encrypt(message)
    return cipher_text
print "123"

def decrypt(cipher_text):
    plain_text = cipher_suite.decrypt(cipher_text)
    return plain_text
print "456"

message = "Hello!"
print crypt(message)
print decrypt(crypt(message))

When I run this script I get the following output:

123
456
Hgir1BHvlLLMUH-Xi-aDrtNFcT3XU86XQsWtrvn6S2s=
<cryptography.fernet.Fernet object at 0x01661A10>
gAAAAABYRAxpvX9ksY5HVNiVa__S9zfBtV0XvVjUS9RpOOJhLp0fVZPbnk1hNMk9xB9x_s88WDRNF14GhY7DJG7B7g0ngIrENA==
cUKBKF-dsP-sQ5_BN1H6yuq_t1h-kBbBgf6N-LCrynM=
<cryptography.fernet.Fernet object at 0x01BDB5D0>
Hello!

In the same folder I have server and client scripts and I want to use the crypt() and decrypt() in the client scrypt client.py, in line 6:

import threading, socket, crypto

When I run the client.py script I get this ImportError:

Traceback (most recent call last):
  File "K:/A/Project/client.py", line 6, in <module>
    import threading, socket, crypto
  File "K:\A\Project\crypto.py", line 2, in <module>
    from cryptography.fernet import Fernet
ImportError: No module named cryptography.fernet
Adam Modern
  • 95
  • 1
  • 1
  • 9
  • 2
    Insert `import sys; print(sys.path)` right before `from cryptography.fernet import Fernet` in `crypto.py`. Then rerun `crypto.py` and `client.py`. Look for differences in the `sys.path`. Make sure that the directory where the `cryptography` package is installed is listed in `sys.path`. – unutbu Dec 04 '16 at 13:07
  • 1
    @unutbu I get `len(sys.path)` = 8 from `client.py` and from `crypto.py` I get 9. However, the first item is repeated in the second `sys.path`. i.e. there are no differences. – Adam Modern Dec 04 '16 at 13:17
  • 1
    @unutbu I get the same directory in both scripts when I print: `import os` `dir_path = os.path.dirname(os.path.realpath(__file__))` `print dir_path` – Adam Modern Dec 04 '16 at 13:28
  • 1
    What happens if you put `import cryptography; print(cryptography.__file__)` in `crypto.py` and rerun `crypto.py` and `client.py`? Depending on the order of the directories in `sys.path`, Python may be finding the wrong package or module named `cryptography`. Printing `cryptography.__file__` will help you see what module or package (if any) Python is finding. – unutbu Dec 04 '16 at 17:41

1 Answers1

1

Version 2.5 is a work around:

pip install cryptography==2.5

(See https://github.com/oracle/oci-python-sdk/issues/108)

Then to avoid "ImportError: No module named enum" errors, use python3.

You might need to install enum34, but I didn't need to. (See ImportError: No module named enum)