简体   繁体   English

ImportError:没有名为cryptography.fernet的模块

[英]ImportError: No module named cryptography.fernet

I have the following script, crypto.py in Python 2: 我在Python 2中有以下脚本crypto.py

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: 在同一个文件夹中我有服务器和客户端脚本,我想在第6行的客户端scrypt client.py中使用crypt()decrypt()

import threading, socket, crypto

When I run the client.py script I get this ImportError: 当我运行client.py脚本时,我得到这个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

Version 2.5 is a work around: 版本2.5是一个解决方法:

pip install cryptography==2.5

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

Then to avoid "ImportError: No module named enum" errors, use python3. 然后要避免“ImportError:No module named enum”错误,请使用python3。

You might need to install enum34, but I didn't need to. 可能需要安装enum34,但我不需要。 (See ImportError: No module named enum ) (参见ImportError:没有名为enum的模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM