cryptography.fernet.InvalidToken error when decrypting line from file (using key from file)

I’m kind of a beginner still and I’m making a simple password manager in python using the cryptography module. The user can enter a program name and a password, and it is encrypted and then written to a file. They can also view passwords added to the file. When the user selects this, they enter the name of the program/service they are trying to find, and it returns the matching password by decrypting the file line by line and checking if the program name is in that file. I hope this makes sense.

The issue I’m having is I get raise InvalidToken cryptography.fernet.InvalidToken when the program attempts to decrypt lines from the file after selecting “view” and entering a name, even when the file is not empty. I’m really unsure why, as the key is not generated in the program, it is stored in a file so the same key is read each time the program runs. I am already reading the file in bytes, not strings. I also have another simpler program which uses the same file and can successfully encrypt and decrypt from a file so I have a feeling I’ve made a mistake somewhere else.

Here is my program (error occurs at line 58):

from cryptography.fernet import Fernet

#Get key from file
filekey = open("filekey.key", "rb")
key = filekey.read()
fernet = Fernet(key)
filekey.close()

cmd = "" #Force entry to loop

#Run until user quits
while cmd != "quit":
    cmd = input("[add | view | quit] ")

    #Input validation loop
    while cmd not in ["add", "view", "quit"]:
        print("Not a valid command. Try again.")
        cmd = input("[add | view | quit]")

    #Run if user enters "add"
    if cmd == "add":

        #Get user input for name and password
        name = input("Enter name: ")
        pwd = input("Enter password: ")

        #Concatenate and encode input
        new_line = name + " " + pwd
        byte_new_line = new_line.encode()

        #Encrypt and convert to string
        enc_line = fernet.encrypt(byte_new_line)
        enc_line_str = str(enc_line)

        #Write string to file
        outfile = open("passwords.data", "a")
        outfile.write(enc_line_str)
        outfile.close()
    
    #Run if user enters "view"
    if cmd == "view":
        found = False #Keep track of whether name is present in file

        #Get user input for name to find
        search = input("Name to search for: ")

        #Open file to search from
        infile = open("passwords.data", "rb")
        line = infile.readline()
        
        #Read each line of file
        while line != "":
            line = infile.readline()

            #Decrypt line and convert to string
            line_dec = fernet.decrypt(line) #Error here
            line_dec_str = str(line_dec)

            #Get index of name in decoded string
            index = line_dec_str.find(name)

            #Run if name is found in string
            if index != -1:

                #Change found to True and print string
                found = True
                print(line_dec_str)

        #Display error if name not found
        if found == False:
            print("Error: name not found in file. Try again.")

        infile.close()

Any help would be greatly appreciated ☺️

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật