python中的哈希(加密)
我正试图在我的登录程序中实现加密,我在很多地方都寻求过帮助,但我似乎一点都不懂。 我对python还相当陌生,我正在准备上一门python的大学课程。 我感兴趣的是,如果有可能实现它作为一个类在我已经兴奋的计划,任何提示或解释将不胜感激 所以,基本上我想问的是,如果我想让程序在运行之间加密密码,然后再次解密密码,这样程序在运行时可以使用密码,那会是什么样子 节目:python中的哈希(加密),python,encryption,hash,passwords,Python,Encryption,Hash,Passwords,我正试图在我的登录程序中实现加密,我在很多地方都寻求过帮助,但我似乎一点都不懂。 我对python还相当陌生,我正在准备上一门python的大学课程。 我感兴趣的是,如果有可能实现它作为一个类在我已经兴奋的计划,任何提示或解释将不胜感激 所以,基本上我想问的是,如果我想让程序在运行之间加密密码,然后再次解密密码,这样程序在运行时可以使用密码,那会是什么样子 节目: import json with open("login_data.txt", "r") as login_file:
import json
with open("login_data.txt", "r") as login_file:
try:
users = json.load(login_file)
except:
users = {}
status = ""
def Display_Menu():
status = input("Are you a registered user? (y/n)? Press q to quit: ")
if status == "y":
Old_User()
elif status == "n":
New_User()
elif status == "passwd":
Change_Passwd()
elif status == "q":
skriva = open("login_data.txt", "w")
json.dump(users, skriva)
return status
def New_User():
Create_Login =input("Create login name: ")
if Create_Login in users:
print ("Login name already exist!")
else:
Create_Password =input("Create password: ")
users[Create_Login] = Create_Password
print("New User created!")
current_user = None
def Old_User():
global current_user
login =input("Enter login name: ")
Password =input("Enter password: ")
if login in users and users[login] == Password:
print("Login successful!")
current_user = login
status = input("Wanna quit, change pass, och logout?")
if status == "passwd":
Change_Passwd()
elif status == "logout":
Display_Menu()
elif status == "q":
skriva = open("login_data.txt", "w")
json.dump(users, skriva)
return status
else:
print("User doesn't exist or wrong password!")
def Change_Passwd():
oldpass =input("Old password: ")
if current_user in users and users[current_user] == oldpass:
Create_Password = input("New password: ")
users[current_user] = Create_Password
if Create_Password == input("Confirm password: "):
print("Password changed!")
else:
print("User authorization failure")
users[current_user] = oldpass
else:
print ("No password match!")
while status != "q":
status = Display_Menu()
MD5是一个非常简单的哈希算法,下面是一些示例用法:
>>> hashlib.md5("String you want to encrypt").hexdigest()
'096a773d70e934d03ae3dd8022deed5e'
MD5决不是安全的,但足以说明一些要点。例如,您可以选择某种格式存储用户名和哈希密码,即:
username1, hash1
username2, hash2
这可能是一个相关的阅读。是一个你应该看看的库
import bcrypt
password = b"super secret password"
# Hash a password for the first time, with a randomly-generated salt
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
# Check that a unhashed password matches one that has previously been
# hashed
if bcrypt.hashpw(password, hashed) == hashed:
print("It Matches!")
else:
print("It Does not Match :(")
不确定您是在询问密码散列,还是具体地问如何在Python中实现密码散列。对于前者,请参阅对于后者,请参阅有关密码哈希的更多背景信息:我想要的只是一个简单的加密和解密函数,我正在尝试找到一种简单的方法来实现它。code Def Old_User:global current_User login=inpunter登录名:printusers[login]另外,为什么不使用code password=inputPassword:encrypt=hashlib.md5password.hexdigest code获取错误在散列之前必须对Unicode对象进行编码可能在散列之前必须使用password.enceutf-8。