【发布时间】:2018-10-15 06:02:21
【问题描述】:
我正在尝试执行以下代码:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
print plain_text
获取:
C:\Python27\python.exe E:/PycharmProjects/my_project/utils/encryption.py
Traceback (most recent call last):
File "E:/PycharmProjects/my_project/utils/encryption.py", line 3, in <module>
cipher_suite = Fernet(key)
File "C:\Python27\lib\site-packages\cryptography\fernet.py", line 32, in __init__
backend = default_backend()
File "C:\Python27\lib\site-packages\cryptography\hazmat\backends\__init__.py", line 15, in default_backend
from cryptography.hazmat.backends.openssl.backend import backend
File "C:\Python27\lib\site-packages\cryptography\hazmat\backends\openssl\__init__.py", line 7, in <module>
from cryptography.hazmat.backends.openssl.backend import backend
File "C:\Python27\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 16, in <module>
from cryptography import utils, x509
File "C:\Python27\lib\site-packages\cryptography\x509\__init__.py", line 8, in <module>
from cryptography.x509.base import (
File "C:\Python27\lib\site-packages\cryptography\x509\base.py", line 16, in <module>
from cryptography.x509.extensions import Extension, ExtensionType
File "C:\Python27\lib\site-packages\cryptography\x509\extensions.py", line 24, in <module>
from cryptography.x509.general_name import GeneralName, IPAddress, OtherName
File "C:\Python27\lib\site-packages\cryptography\x509\general_name.py", line 18, in <module>
from cryptography.x509.name import Name
File "C:\Python27\lib\site-packages\cryptography\x509\name.py", line 28, in <module>
_ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
File "C:\Python27\lib\site-packages\cryptography\x509\name.py", line 28, in <genexpr>
_ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
AttributeError: 'int' object has no attribute 'value'
Process finished with exit code 1
文件路径为:my_project/utils/encryption.py
在“utils”包之外或在 python 控制台中运行相同的代码可以正常工作。 可能是什么问题?
【问题讨论】:
-
当它工作正常时,你检查过python版本吗?
-
我使用的是 python 2.7.13。为了使其正常工作,我只需将文件从 root/utils/file.py 移动到 root/file.py 就可以了!
标签: python python-3.x python-2.7 cryptography