我的usb加密解密程序中的cryptography.fernet.InvalidToken

2022-09-28 20:47:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我面临解密方面的问题。我的检测usb闪存驱动器和加密工作正常,但当我尝试在第二次插入时解密usb闪存时,显示错误

import os.path
from cryptography.fernet import Fernet

#find difference between list1 and list2...used here for finding newly connected or disconnected drive
def difference(list1, list2):
    list_difference = [item for item in list1 if item not in list2]
    return list_difference

def driveConnected():
    print("New drive introduced")

def driveDisconnected():
    print("Drive disconnected")

#load key from current directory
def load_key():
    return open("key.key", "rb").read()

#encryption
def encryption(filename, key):

    f = Fernet(key)
    with open(filename, "rb") as file:
        # read all file data
        file_data = file.read()

        # encrypt data
        encrypted_data = f.encrypt(file_data)

        # write the encrypted file
        with open(filename, "wb") as file:
            file.write(encrypted_data)

#decryption
def decryption(filename, key):

    f = Fernet(key)
    with open(filename, "rb") as file:
        # read the encrypted data
        encrypted_data = file.read()

        # decrypt data
        decrypted_data = f.decrypt(encrypted_data)

        # write the original file
        with open(filename, "wb") as file:
            file.write(decrypted_data)
    
#list of possible drives
driveList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
drives = ['%s:' % d for d in driveList if os.path.exists('%s:' % d)]    #connected drives
crypt = False
controller = False       #to avoid encrpting an already encrypted drive
print("connected drives:", drives)

while True:

    #finding newly connected or disconnected drives
    uncheckedDrives = ['%s:' % d for d in driveList if os.path.exists('%s:' % d)]   
    x = difference(uncheckedDrives, drives)

    #if drive is connected
    if x:
        print("New drives: " + str(x))
        driveConnected()

        for i in x:
            path = i + "\\"     #path of the drive
            
            #encrypt files
            if crypt == False and controller == False:

                key = load_key()
                for dirpath, dirnames, files in os.walk(path):
                    for file in files:
                        encryption(dirpath + "\\" + file, key)    #encryption process
                print("files encrypted")
                crypt = True

            #decrypt files
            elif crypt == True and controller == True:

                key = load_key()
                for dirpath, dirnames, files in os.walk(path):
                    for file in files:
                        decryption(dirpath + "\\" + file, key)    #decryption process
                print("files decrypted")
                crypt = False

    x = difference(drives, uncheckedDrives)
    #if drive is disconnected
    if x:

        if controller:
            controller = False
        else:
            controller = True

        print("Removed drives: " + str(x))
        driveDisconnected()

    drives = ['%s:' % d for d in driveList if os.path.exists('%s:' % d)]

    time.sleep(1)

connected drives: ['C:', 'D:', 'E:'] New drives: ['F:'] New drive introduced files encrypted Removed drives: ['F:'] Drive disconnected New drives: ['F:'] New drive introduced Traceback (most recent call last): File "C:\Users\user\Downloads\SEM 4\OS\OS Theory\OS Project\Crypt.py", line 87, in decryption(dirpath + "\" + file, key) #decryption process File "C:\Users\user\Downloads\SEM 4\OS\OS Theory\OS Project\Crypt.py", line 44, in decryption decrypted_data = f.decrypt(encrypted_data) File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\cryptography\fernet.py", line 75, in decrypt timestamp, data = Fernet._get_unverified_token_data(token) File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\cryptography\fernet.py", line 107, in _get_unverified_token_data raise InvalidToken cryptography.fernet.InvalidToken


热门问题