简体   繁体   English

Fernet 密钥必须是 32 个 url 安全的 base64 编码字节

[英]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.

相关问题 fe.net 密钥错误“Fe.net 密钥必须是 32 个 url 安全的 base64 编码字节” - fernet key error "Fernet key must be 32 url-safe base64-encoded bytes" 错误:Fernet 密钥必须是 32 个 url 安全的 base64 编码字节 - Error: Fernet key must be 32 url-safe base64-encoded bytes 阅读 Fernet Key Causes ValueError: Fernet key must be 32 url-safe base64-encoded bytes - Read Fernet Key Causes ValueError: Fernet key must be 32 url-safe base64-encoded bytes “Fernet”python 编码给了我以下错误: ValueError: Fernet key must be 32 url-safe base64-encoded bytes - "Fernet" python encoding gives me the following error: ValueError: Fernet key must be 32 url-safe base64-encoded bytes 错误:Fernet 密钥必须是 32 个 url 安全的 base64 编码字节。 每当尝试运行程序时 - Error: Fernet key must be 32 url-safe base64-encoded bytes. Whenever trying to run the program 使用Kubernetes的Airflow GCP连接问题 - Fernet密钥必须是32个url-safe base64编码的字节 - Airflow GCP Connection Issue With Kubernetes - Fernet key must be 32 url-safe base64-encoded bytes PEM 格式的 base64 编码公钥的序列化 - Serialization of a base64-encoded public key in PEM format 如何将 Base64DataURL 转换为 base64 编码的图像字节 - How to convert Base64DataURL to base64-encoded image bytes 从 DER 格式的 base64 编码公钥到 COSE 密钥,在 Python - From base64-encoded public key in DER format to COSE key, in Python 如何反序列化base64编码的数据并将其与DRF一起使用 - How to deserialize base64-encoded data and use it with DRF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM