【发布时间】:2020-12-21 03:53:41
【问题描述】:
所以,我正在制作一个密码管理器,并且我正在使用密码学模块。该程序的问题在于解密时。当我在同一个会话中加密和解密时它会正常工作,但是当我在不同的会话中加密、关闭然后解密时,会引发错误。这个错误没有发生,因为我每次都生成一个随机密钥,但我认为当我使用 Fernet() 方法初始化它时,密钥正在改变。我该如何解决?
#The Dependicies and Packages required for this script
import sqlite3
from cryptography.fernet import Fernet
def generate_key():
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key()
with open("secret.key", "wb") as key_file:
key_file.write(key)
def load_key():
"""
Loads the key named `secret.key` from the current directory.
"""
return open("secret.key", "rb").read()
#These are the keys for encryption
Key = load_key()
f = Fernet(Key)
def decode_data(datas):
new_name = f.decrypt(datas)
final_name = new_name.decode()
return final_name
def find_password():
"""
This function is to get the password of the website that the user expected
"""
website_name = input("What is the website's name for which you need a password>")
c.execute("SELECT * FROM passwords")
data = c.fetchall()
print(data)
for row in data:
print(row[0])
name = decode_data(row[0])
if name == website_name:
password = decode_data(row[2])
print(f'The password to {website_name} is {password}')
def main():
go_on = True
while go_on:
direction_question = input("This is your password manager. Press 1 to create a new pasword, Press 2 to search for a password, or Press 3 to exit the program>")
if direction_question.lower() == "1":
create_password()
if direction_question.lower() == "2":
find_password()
if direction_question.lower() == "3":
go_on = False
else:
print("Invalid response")
db.commit()
db.close()
if __name__ == "__main__":
db = sqlite3.connect('password.db')
c = db.cursor()
main()
引发了这些错误
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 114, in _verify_signature
h.verify(data[-32:])
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/primitives/hmac.py", line 68, in verify
ctx.verify(signature)
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/hmac.py", line 78, in verify
raise InvalidSignature("Signature did not match digest.")
cryptography.exceptions.InvalidSignature: Signature did not match digest.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "password_manager.py", line 86, in <module>
main()
File "password_manager.py", line 74, in main
find_password()
File "password_manager.py", line 61, in find_password
name = decode_data(row[0])
File "password_manager.py", line 28, in decode_data
new_name = f.decrypt(datas)
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 77, in decrypt
return self._decrypt_data(data, timestamp, ttl, int(time.time()))
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 126, in _decrypt_data
self._verify_signature(data)
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 116, in _verify_signature
raise InvalidToken
cryptography.fernet.InvalidToken
【问题讨论】:
-
请参阅stackoverflow.com/help/mcve 以获得编写好问题的帮助。特别是您的代码包含许多看似无关的细节,同时也遗漏了重要的细节,例如您如何加密密码。另请注意,使用
SELECT password FROM passwords WHERE name = ?并将website_name作为参数传递会更好
标签: python python-3.x encryption cryptography python-cryptography