You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python密码管理器添加多条密码时存储文件被覆盖问题求助

问题根因
  • 文件打开模式错误:新增密码时使用w+模式打开Database.txt,该模式会先清空文件原有内容再写入,直接导致旧记录被覆盖。
  • 加密存储逻辑缺陷:cryptocode每次加密生成的是独立的解密单元,若直接将多条加密字符串拼接存储,整块读取时无法正确解密所有记录。
  • 文件名大小写不一致:写入时使用文件名Database.txt,读取时使用database.txt,在Linux、Mac等区分文件名大小写的系统中会触发文件不存在报错。
修复方案

1. 写入逻辑调整

将文件打开模式改为追加模式a,每条加密后的密码单独占一行写入,避免覆盖原有内容。

2. 读取逻辑调整

读取文件时逐行读取,每行单独解密后再拼接输出,保证所有记录都能被正确解密。

3. 统一文件名

读写操作使用完全一致的文件名,避免跨平台兼容问题。

4. 补全缺失的退出、删除数据库逻辑

补全原代码中command=3和command=4的空实现,保证功能完整。

完整修复后代码
import time
import hashlib
import os
import cryptocode

master_pass_db = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"
# 统一数据库文件名
DB_FILE = "Database.txt"

def main():
    while True:
        print('''
        -------------------------
        Commands:
        1. add a password
        2. read passwords
        3. quit
        4. delete database
        -------------------------
        ''')
        command = input('Command : ')
        if command == '1':
            platform = input('请输入账号所属平台(如Facebook/Instagram): ')
            username = input('请输入用户名 :')
            password = input('请输入密码 : ')
            # 整理存储内容
            content = f'''
            平台:{platform}
            用户名:{username}
            密码:{password}
            '''
            encoded_password = cryptocode.encrypt(content, master_pass_db)
            # 追加模式写入,每条记录单独占一行
            with open(DB_FILE, 'a', encoding='utf-8') as f:
                f.write(encoded_password + '\n')
            print('密码已保存')
                
        elif command == '2':
            if not os.path.exists(DB_FILE):
                print('暂无存储的密码记录')
                continue
            all_passwords = []
            with open(DB_FILE, 'r', encoding='utf-8') as f:
                # 逐行解密每条记录
                for line in f:
                    line = line.strip()
                    if not line:
                        continue
                    decrypted = cryptocode.decrypt(line, master_pass_db)
                    all_passwords.append(decrypted)
            print('='*30)
            print('已存储的所有密码:')
            print('\n'.join(all_passwords))
            print('='*30)
        elif command == '3':
            print('正在退出系统...')
            time.sleep(1)
            break
        elif command == '4':
            confirm = input('确认删除所有密码记录?输入Y确认:')
            if confirm.upper() == 'Y':
                if os.path.exists(DB_FILE):
                    os.remove(DB_FILE)
                    print('数据库已删除')
                else:
                    print('数据库不存在')
        else:
            print('无效命令,请重新输入')

master_pass_input = input("请输入主密码 : ").encode()
hashing_input = hashlib.sha256(master_pass_input).hexdigest()
if master_pass_db == hashing_input :
    main()
else:
    print('密码错误...')
    time.sleep(1)
    quit()
验证说明

输入主密码1234进入系统后,依次添加Facebook、Instagram的账号信息,再调用读取功能,即可看到所有已存储的记录,新增记录不会覆盖旧内容。

内容的提问来源于stack exchange,提问作者omar shokry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 12:45:03