我有以下脚本,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))
当我运行此脚本时,我得到以下输出:
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!
在同一文件夹中我有服务器和客户端脚本,我想在客户端scrypt crypt()
中使用decrypt()
和client.py
,在第6行:
import threading, socket, crypto
当我运行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
版本2.5是一个解决方法:
pip install cryptography==2.5
(见https://github.com/oracle/oci-python-sdk/issues/108)
然后要避免“ImportError:No module named enum”错误,请使用python3。
您可能需要安装enum34,但我不需要。 (见ImportError: No module named enum)