我在 python 密码学 (cryptography.fernet.InvalidToken) 中不断收到无效令牌错误

I keep getting Invalid Token error in python cryptography (cryptography.fernet.InvalidToken)

我编写了这个原型代码来加密一些文本(反之亦然)。当我将命令设置为 self.get()self.write 正常工作时,我不断收到此错误。我不知道是什么原因导致此错误或如何解决它...求助...

from cryptography.fernet import Fernet

class EncodingText:
    def __init__(self):
        self.key = Fernet.generate_key()
        self.f = Fernet(self.key)
        self.get()

    def write(self):
        stuff = "hello there".encode()
        token = self.f.encrypt(stuff)
        open_file_for_edit = open("file.txt", 'wb')
        open_file_for_edit.write(token)
        open_file_for_edit.close()

    def get(self):
        read_file = open("file.txt", 'rb')
        reading = read_file.read()
        print(reading)
        token = self.f.decrypt(reading)
        print(token)
        read_file.close()


if __name__ == "__main__":
    EncodingText()

我得到的错误如下:

Traceback (most recent call last):
  File "C:\Users\xoxo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cryptography\fernet.py", line 113, in _verify_signature
    h.verify(data[-32:])
  File "C:\Users\xoxo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cryptography\hazmat\primitives\hmac.py", line 70, in verify
    ctx.verify(signature)
  File "C:\Users\xoxo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cryptography\hazmat\backends\openssl\hmac.py", line 78, in verify
    raise InvalidSignature("Signature did not match digest.")
cryptography.exceptions.InvalidSignature: Signature did not match digest.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:/Users/xoxo/Desktop/Python Programs/xoxo/xoxo.py", line 26, in <module>
    EncodingText()
  File "c:/Users/xoxo/Desktop/Python Programs/xoxo/xoxo.py", line 7, in __init__
    self.get()
  File "c:/Users/xoxo/Desktop/Python Programs/xoxo/xoxo.py", line 20, in get
    tokenf = self.f.decrypt(reading)
  File "C:\Users\xoxo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cryptography\fernet.py", line 76, in 
decrypt
    return self._decrypt_data(data, timestamp, ttl, int(time.time()))
  File "C:\Users\xoxo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cryptography\fernet.py", line 125, in _decrypt_data
    self._verify_signature(data)
  File "C:\Users\xoxo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cryptography\fernet.py", line 115, in _verify_signature
    raise InvalidToken
cryptography.fernet.InvalidToken

让我们逐行查看代码:

  1. 方法中__init__:

    1. 第 1 行:密钥生成。

      self.key = Fernet.generate_key()  # this is called
      

      每次调用该方法时,我们都会生成一个随机密钥

    2. 第 2 行:密码生成

      self.f = Fernet(self.key)
      

      我们正在使用完全随机的密钥创建密码。

    3. 第 3 行:解密

      self.get()
      

      我们正在调用一个新方法。

  2. 在方法中get:

    1. 第 1、2 和 3 行:读取文件

      read_file = open("file.txt", 'rb')
      reading = read_file.read()
      print(reading)
      

      在这里,有两种可能。

      1. 路径中缺少文件,引发 FileNotFoundError 并且程序停止。

      2. 文件存在。

      假设文件存在(#2)。将读取文件内容并打印内容。

    2. 第 4 行:解密

      token = self.f.decrypt(reading)
      

      到这里,我们的文件内容将被解密。请记住,从 1.1.1 点开始, 1.1.2、每次调用我们的程序时,都会生成一个随机的key和cipher 使用随机密钥生成。

      由于 Fernet,通过实现,使用 AES,这是一个对称密码,我们需要相同的密钥 用于加密和解密。

      但是,到 1.1.1 和 1.1.2,我们在每次程序生成一个随机密钥 运行。

      这解释了错误消息。密码正在尝试解密来自 使用完全随机密钥和另一个随机密钥加密的文件 密钥,导致解密不正确。


如果在 self.get() 之前插入 self.write(),程序将运行。这是因为 相同的密钥用于解密数据。