python的密码学抛出AttributeError:'int'对象没有属性'value'

问题描述 投票:0回答:2

我正在尝试执行以下代码:

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-3.x python-2.7 cryptography
2个回答
2
投票

密码学代码需要Python 3枚举:https://pypi.python.org/pypi/enum34。这就是你遇到这个问题的原因。

我建议验证您的版本是否正确,并且不会被其他模块替换。


0
投票

问题是我有一个名为enum.py的模块以及加密代码(相同的包)。密码学尝试使用它而不是enum34。将'enum.py'重命名为其他东西解决了它。

© www.soinside.com 2019 - 2024. All rights reserved.