Python 使用相同密钥解密文本时,给出的令牌无效

Python 使用相同密钥解密文本时,给出的令牌无效,python,encryption,cryptography,python-cryptography,Python,Encryption,Cryptography,Python Cryptography,我可以使用来自特定文件的密钥key1进行加密,但我无法使用相同的密钥对文件进行解密,它会为我提供cryptography.fernet.InvalidToken,即使它是来自同一文件的相同密钥Securi1.key,并作为rb读入 # This function calls the pre-existing key to encrypt and decrypt files def securi1_key(): file = open("C:/Users/devff/PycharmProj

我可以使用来自特定文件的密钥
key1
进行加密,但我无法使用相同的密钥对文件进行解密,它会为我提供
cryptography.fernet.InvalidToken
,即使它是来自同一文件的相同密钥
Securi1.key
,并作为
rb
读入

# This function calls the pre-existing key to encrypt and decrypt files
def securi1_key():
    file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi1.key", 'rb')
    global key1
    key1 = file.read()
    file.close()

# this code encrypts the documents text and then replaces the raw text with encrypted string
def rewrite_yellow():
    securi1_key()
    save_yellow_text() #existing text
    #   get key from file
    file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi1.key", 'rb')
    texty = scan4yellow.decode("utf-8")
    encodedy = texty.encode()

    #   encrypt message
    f = Fernet(key1)
    encryptedy = f.encrypt(encodedy)
    # replaces document text with encrypted text
    document = docx.Document(f1)
    for paragraph in document.paragraphs:
        if scan4yellow.decode("utf-8") in paragraph.text:
            inline = paragraph.runs
            # loops for runs
            for i in range(len(inline)):
                if scan4yellow.decode("utf-8") in inline[i].text:
                    text = inline[i].text.replace(scan4yellow.decode("utf-8"), encryptedy.decode("utf-8"))
                    inline[i].text = text
    document.save(f1)

#this function should ideally decrypt
def decrypt_yellow():
    securi1_key()
    inputfile = (f1)
    list_of_inputfiles = os.listdir("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/")

    with open(inputfile, "rb") as f:
        data = f.read()

    file = open("C:/Users/devff/PycharmProjects/SecurityLevels/Stored Keys/Securi1.key", "rb")
    global key1
    key1 = file.read()
    file.close()

    f2 = Fernet(key1) # <-- key1 is from Securi1 so it should decrypt
    decryptedy = decryptedy = f2.decrypt(data) #encrypted contains OG text so it decrypts it with the key
    # replaces the text
    document = docx.Document(f1)
    for paragraph in document.paragraphs:
        if scan4yellow.decode("utf-8") in paragraph.text:
            inline = paragraph.runs
            #loops for the runs
            for i in range(len(inline)):
                if scan4yellow.decode("utf-8") in inline[i].text:
                    text = inline[i].text.replace(scan4yellow.decode("utf-8"), decryptedy.decode("utf-8"))
                    inline[i].text = text
    document.save(f1)
    inputfile.close()
    os.remove(inputfile)
#此函数调用预先存在的密钥来加密和解密文件
def安全1_键():
file=open(“C:/Users/devff/PycharmProjects/SecurityLevels/storagekeys/Securi1.key”,“rb”)
全局键1
key1=file.read()
file.close()文件
#此代码加密文档文本,然后用加密字符串替换原始文本
def rewrite_yellow():
安全1_密钥()
保存_yellow_text()#现有文本
#从文件中获取密钥
file=open(“C:/Users/devff/PycharmProjects/SecurityLevels/storagekeys/Securi1.key”,“rb”)
texty=scan4yellow.decode(“utf-8”)
encodedy=texty.encode()
#加密消息
f=Fernet(键1)
encryptedy=f.encrypt(encodedy)
#用加密文本替换文档文本
文档=docx.document(f1)
对于文件中的段落。段落:
如果在段落.text中扫描4黄色解码(“utf-8”):
内联=段落.runs
#运行循环
对于范围内的i(len(inline)):
如果在内联[i]中扫描4Yellow.decode(“utf-8”)。文本:
text=inline[i].text.replace(scan4yellow.decode(“utf-8”)、encryptedy.decode(“utf-8”))
内联[i]。文本=文本
文档保存(f1)
#理想情况下,此函数应该进行解密
def decrypt_yellow():
安全1_密钥()
inputfile=(f1)
输入文件列表=os.listdir(“C:/Users/devff/PycharmProjects/SecurityLevels/Stored Text/”)
将open(inputfile,“rb”)作为f:
data=f.read()
file=open(“C:/Users/devff/PycharmProjects/SecurityLevels/storagekeys/Securi1.key”,“rb”)
全局键1
key1=file.read()
file.close()文件

f2=Fernet(键1)#您能编辑并包含准确的回溯吗?此外,密钥文件的副本也很有用。我会尽我最大的努力帮助你。另外一件需要补充的事情是,当密文损坏时,
InvalidToken
会被引发。你能编辑并包含准确的回溯吗?此外,密钥文件的副本也很有用。我会尽我最大的努力来帮助你。另外一件需要补充的事情是,当密文损坏时,
InvalidToken
会被触发。