【问题标题】:Master Password in Password Manager with Python使用 Python 的密码管理器中的主密码
【发布时间】:2022-10-23 12:15:53
【问题描述】:

我创建了一个基于终端的密码管理器,我们在其中输入主密码,然后我们可以选择必须添加新密码或查看现有密码。我在cryptography.fernet 的帮助下创建了它。

我唯一的问题是,当第一次输入主密码时,我们有 fernet 密钥,但下一次输入错误密码时它实际上可以工作,只有当我们输入正确时它才应该工作我们第一次输入的密码与密钥匹配,但它也适用于错误的密码。我能得到一些帮助吗?

from cryptography.fernet import Fernet
from pickle import TRUE

def load_key():
file = open('key.key', 'rb')
key = file.read()
file.close()
return key

master_pwd = input("Enter your master pass : ")
key = load_key() + master_pwd.encode()
fer = Fernet(key)

#Functions
'''
def write_key():
    key = Fernet.generate_key()
    with open('key.key', 'wb') as key_file:
        key_file.write(key)

write_key()'''

def add():
    name = input("Enter the site name: ")
    url = input("Enter the site URL: ")
    email = input("Enter the email: ")
    pwd = input("Enter the Password: ")

    with open('passwords.txt', 'a') as f:
        f.write("Name: " + name + " | " + "URL: " + url + " | " "Email: " + email + " | " + fer.encrypt(pwd.encode()).decode() + "\n")

def view():
    with open('passwords.txt', 'r') as f:
        for line in f.readlines():
            data = line.rstrip()
            name, url, email, pwd = data.split("|")
            print(name, "|", url, "|", email, "|", "Password:", fer.decrypt(pwd.encode()).decode())
            
while True:
    print("1. Add a new Password: ")
    print("2. View existing Passwords: ")
    print("Enter q to quit: " "\n")
    mode = input()

    if mode == "q":
        print("Come Back Again :)")
        break

    if mode == "1":
        add()
    elif mode == "2":
        view()
    else:
        print("Invalid mode")
        break

【问题讨论】:

    标签: python cryptography fernet password-manager


    【解决方案1】:

    这是因为Fernet(key) 忽略了附加到Fernet.generate_key() 结果的额外字节。如果它执行得很好,它应该会抛出异常。

    请参阅their documentation 末尾的“在 Fernet 中使用密码”部分。

    附带说明一下,我推荐诸如 PyCryptodome 之类的非自以为是的 API,而不是像“密码学”这样的有点自以为是的 API。

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2021-02-01
      • 2020-01-30
      相关资源
      最近更新 更多