标签: fernet

类型错误:参数应该是类似字节的对象或 ASCII 字符串,而不是“dict”

当我解密加密消息时,我收到此错误。我从 django 视图中的数据库获取了加密消息,pw = donator.objects.filter(emai=email).values('passw')并在解密消息()函数中传递了 pw 对象。crypto_messag() 函数是:

def decrypt_message(encrypted_message,key):
    """
    Decrypts an encrypted message
    """
    f = Fernet(key)
    decrypted_message = f.decrypt(encrypted_message)
    return decrypted_message.decode()
Run Code Online (Sandbox Code Playgroud)

错误信息是:

File "C:\Users\Neelesh Singh\workspace\BookHouse\donatorapp\views.py", line 129, in decrypt_message
    f = Fernet(key)
  File "C:\Users\Neelesh Singh\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\fernet.py", line 37, in __init__
    key = base64.urlsafe_b64decode(key)
  File "C:\Users\Neelesh Singh\AppData\Local\Programs\Python\Python39\lib\base64.py", line 131, in urlsafe_b64decode
    s = _bytes_from_decode_data(s)
  File "C:\Users\Neelesh Singh\AppData\Local\Programs\Python\Python39\lib\base64.py", line 45, in _bytes_from_decode_data
    raise TypeError("argument should be a bytes-like object or ASCII "
TypeError: argument should be a bytes-like …
Run Code Online (Sandbox Code Playgroud)

python django cryptography fernet

8
推荐指数
1
解决办法
1万
查看次数

如何解密最初使用 Fernet 加密的不同服务上的值?

我正在研究一个基于 python 后端的项目。我将使用 Django 来处理“核心”内容,使用 FastAPI 来处理一些爬虫。Fernet我正在使用模块和自定义Django 将一些数据加密到数据库中Field

class EncryptedField(models.CharField):
    description = "Save encrypted data to DB an read as string on application level."

    def __init__(self, *args, **kwargs):
        kwargs["max_length"] = 1000
        super().__init__(*args, **kwargs)

    @cached_property
    def fernet(self) -> Fernet:
        return Fernet(key=settings.FERNET_KEY)

    def get_internal_type(self) -> str:
        return "BinaryField"

    def get_db_prep_save(
        self, value: Any, connection: BaseDatabaseWrapper
    ) -> Union[memoryview, None]:
        value = super().get_db_prep_save(value, connection)
        if value is not None:
            encrypted_value = self.fernet.encrypt(data=force_bytes(s=value))
            return connection.Database.Binary(encrypted_value)

    def from_db_value(self, value: bytes, *args) …
Run Code Online (Sandbox Code Playgroud)

python encryption django fastapi fernet

5
推荐指数
1
解决办法
1376
查看次数

标签 统计

django ×2

fernet ×2

python ×2

cryptography ×1

encryption ×1

fastapi ×1