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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的密码学抛出AttributeError:'int'对象没有属性'value'相关的知识,希望对你有一定的参考价值。

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

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:Python27python.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:Python27libsite-packagescryptographyfernet.py", line 32, in __init__
    backend = default_backend()
  File "C:Python27libsite-packagescryptographyhazmatackends\__init__.py", line 15, in default_backend
    from cryptography.hazmat.backends.openssl.backend import backend
  File "C:Python27libsite-packagescryptographyhazmatackendsopenssl\__init__.py", line 7, in <module>
    from cryptography.hazmat.backends.openssl.backend import backend
  File "C:Python27libsite-packagescryptographyhazmatackendsopensslackend.py", line 16, in <module>
    from cryptography import utils, x509
  File "C:Python27libsite-packagescryptographyx509\__init__.py", line 8, in <module>
    from cryptography.x509.base import (
  File "C:Python27libsite-packagescryptographyx509ase.py", line 16, in <module>
    from cryptography.x509.extensions import Extension, ExtensionType
  File "C:Python27libsite-packagescryptographyx509extensions.py", line 24, in <module>
    from cryptography.x509.general_name import GeneralName, IPAddress, OtherName
  File "C:Python27libsite-packagescryptographyx509general_name.py", line 18, in <module>
    from cryptography.x509.name import Name
  File "C:Python27libsite-packagescryptographyx509
ame.py", line 28, in <module>
    _ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
  File "C:Python27libsite-packagescryptographyx509
ame.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 3枚举:https://pypi.python.org/pypi/enum34。这就是你遇到这个问题的原因。

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

另一答案

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

以上是关于python的密码学抛出AttributeError:'int'对象没有属性'value'的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Dash Web 应用程序中使用“dash.properties.Synced”? | Python

PdfRenderer 在呈现受密码保护的文件后总是抛出“需要密码”SecurityException

PrincipalContext::ValidateCredentials 抛出带有无效密码的 LdapException

StreamWriter 抛出 IOException 登录失败:未知用户名或密码错误

Laravel 5.5 自定义重置密码抛出令牌不匹配

DRF密码休息工作流程抛出django.template.exceptions.TemplateDoesNotExist