[英]Fernet key must be 32 url-safe base64-encoded bytes
import certifi
from pymongo import MongoClient
import os
import platform
from cryptography.fernet import Fernet
cluster = MongoClient("(database string)", tlsCAFile=certifi.where())
db = cluster["db"]
pass_collection = db["password"]
def clear():
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
while True:
clear()
print(''''
/$$$$$$ /$$$$$$$ /$$ /$$
/$$__ $$| $$__ $$| $$$ /$$$
| $$ \__/| $$ \ $$| $$$$ /$$$$
| $$$$$$ | $$$$$$$/| $$ $$/$$ $$
\____ $$| $$____/ | $$ $$$| $$
/$$ \ $$| $$ | $$\ $ | $$
| $$$$$$/| $$ | $$ \/ | $$
\______/ |__/ |__/ |__/
''')
print("---------------------------------")
print("1: New password")
print("2: View Password")
print("3: Exit")
print("---------------------------------")
startMenu = int(input())
if startMenu == 1:
clear()
print("Password to save:")
passwd = input()
key_en = Fernet.generate_key()
print("Hasing pass....")
passwd = passwd.encode('utf-8')
hash_pw = Fernet(key_en).encrypt(passwd)
print("Create rapass.")
rapass = input()
print("NOTE THESE PASSWORDS:")
print('RAPASS : ' + rapass)
print('SecurePass : ' + str(key_en))
print("Storing pass...")
pass_collection.insert_one({"_id": rapass, "password": hash_pw})
input()
print("Succesful. Press any key to continue.")
input()
elif startMenu == 2:
clear()
print("Enter rapass:")
rapass = input()
data = pass_collection.find_one({"_id" : rapass})
if rapass == data["_id"]:
print("Enter SecurePass")
key_de = input()
key_de = bytes(key_de, 'utf-8')
raw_pass = Fernet(key_de).decrypt(data["password"])
if Fernet(key_de).encrypt(raw_pass) == data["password"]:
print(raw_pass)
else:
print("SecurePass incorrect. Please try again.")
input()
clear()
else:
print("Invalid credentials.")
input()
clear()
elif startMenu == 3:
exit()
Here is my code, I was trying to make a login/register system but then I got this error.这是我的代码,我试图创建一个登录/注册系统,但后来我收到了这个错误。 I tried encoding it into utf-8 but it still shows up.我尝试将其编码为 utf-8,但它仍然显示出来。 Can you tell me how to obliterate this error.你能告诉我如何消除这个错误。
Here is where the program stops and shows the error:这是程序停止并显示错误的地方:
raw_pass = Fernet(key_de).decrypt(data["password"]) raw_pass = Fernet(key_de).decrypt(data["password"])
Ive tried using the base64 import to encode the data but it comes up with the same error-我尝试使用 base64 导入对数据进行编码,但出现了相同的错误-
key_de = bytes(key_de, 'utf-8') key_de = 字节(key_de,'utf-8')
key_de = base64.urlsafe_b64encode(key_de) key_de = base64.urlsafe_b64encode(key_de)
The key used to encrypt (not hash) the password - called key_en
- is not stored or shown.用于加密(而非散列)密码的密钥 - 称为key_en
- 不会存储或显示。 It consists of binary data, which seemingly is encoded as base 64 by Fernet.它由二进制数据组成,似乎由 Fernet 编码为 base 64。 You then ask for a key_de
, which would need to be the same as key_en
, but since you've never communicated that value, the code will fail.然后,您要求key_de
,它需要与key_en
相同,但由于您从未传达过该值,因此代码将失败。
This is more likely a design error than a programming error.这更可能是设计错误而不是编程错误。 You may want to rethink your strategy.您可能需要重新考虑您的策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.