首页 > 其他分享 >14 hashlib 案例2 用户注册

14 hashlib 案例2 用户注册

时间:2024-09-28 08:50:54浏览次数:1  
标签:hashlib encrypt 14 用户注册 utf pwd user old line

问题:无法反向解密时,我们 知道密码明文吗

  • 用户注册
import hashlib
# 1、用户注册
user = input("请输入用户名:").strip()
pwd = input("请输入密码:").strip()

# 2、加密
obj = hashlib.md5('testaa'.encode('utf-8'))
obj.update(pwd.encode('utf-8'))
encrypt_pwd = obj.hexdigest()

# 3、写入文件
with open('userinfo', mode='a', encoding='utf-8') as f:
    f.write(f"{user},{encrypt_pwd}")
  • 用户登录-函数版本
  • 建议结构
  • 全局变量一定要大写,局部变量是小写
  • not,取反

import hashlib

SALT = 'testaa'


def user_input():
    user = input("请输入用户名:").strip()
    pwd = input("请输入密码:").strip()
    return user, pwd


def md5(data_string):
    obj = hashlib.md5(SALT.encode('utf-8'))
    obj.update(data_string.encode('utf-8'))
    return obj.hexdigest()


def user_islogin(user, encrypt_pwd):
    is_success = False
    with open('userinfo', mode='r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            
            # 如果是空的,就让他继续
            if not line:  
                continue
            old_user, old_pwd = line.split(",")
            if old_user == user and old_pwd == encrypt_pwd:
                is_success = True
                break
    return is_success


def get_user_info():
    user_dict = {}
    with open('userinfo', mode='r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            
            # 如果是空的,就让他继续
            if not line:  
                continue
            old_user, old_pwd = line.split(",")
            user_dict[old_user] = old_pwd
    return user_dict


def run():
    # 1、用户注册
    # user, pwd = user_input()

    # 2、密码加密
    # encrypt_pwd = md5(pwd)

    # 3、写入文件
    # with open('userinfo', mode='a', encoding='utf-8') as f:
    #     f.write(f"{user},{encrypt_pwd}")

    #     1、用户登录

    user, pwd = user_input()
    # 2、密码加密
    encrypt_pwd = md5(pwd)

    # 3、读取文件 + 进行密文的比较(简单)登录时,比较方法1
    is_success = user_islogin(user, encrypt_pwd)
    if is_success:
        print('登录成功')
    else:
        print('登录失败')

    # 比较方法2:
    user_dict = get_user_info()
    db_pwd = user_dict.get(user)
    if db_pwd == encrypt_pwd:
        print('登录成功')
    else:
        print('登录失败')


if __name__ == '__main__':
    run()

标签:hashlib,encrypt,14,用户注册,utf,pwd,user,old,line
From: https://www.cnblogs.com/jhchena/p/18436976

相关文章

  • 13 hashlib案例1
    此模块用于实现加密,例如:md5加密importhashlibdefrun():data='中国联通'obj=hashlib.md5()obj.update(data.encode('utf-8'))ret=obj.hexdigest()print(ret)为什么要加密#避免密码被破解wupeiqi,123alex,123eric,123wupeiqi,202c......
  • 吃鸡msvcp140.dll丢失的解决方法?绝地求生dll修复方法
    在运行游戏时,可能会遇到“msvcp140.dll丢失”或“找不到msvcp140.dll”等错误提示,这通常是因为系统中缺少MicrosoftVisualC++Redistributable包的部分组件。以下是几种有效解决这一问题的方法,帮助您顺利启动游戏。方法1:重新安装VisualC++Redistributable包1.卸载现有......
  • P2375 [NOI2014] 动物园
    P2375[NOI2014]动物园题意是对于每个前缀,求前缀后缀不交的且前后缀相等的前后缀数量数组,\(1\lelen\le10^6\)。考虑先求出正常的\(next\)数组,KMP\(O(len)\)求解。对于\(next'\)数组,可以由前一个数的\(next'\)数组转移,如果新数大小超过了\(\frac{i}{2}\)就跳到前......
  • 易优CMS【错误代码】 SQLSTATE【42S02】:Base table or view not found:1146 Table‘111
    当你遇到“数据表或视图不存在”的错误提示时,通常是因为数据库中缺少某个表或视图。以下是一些具体的解决步骤:步骤1:确认表是否存在检查数据库表使用数据库管理工具(如phpMyAdmin)打开数据库。检查数据库中是否存在表 ey_admin_theme。如果表不存在,需要创建该表。步骤......
  • 9.14课程内容
    课程内容:老师给我们分享了录音转文字的小程序(比如:录音转文字助手)、转图识字等便捷的办公软件,现在顺便保存一个我之前用的语音包软件——魔音它可以得到选择不同的声音,输入需要的文字,就像配音一样。课程的第二部分内容主要的思维导图工具,老师向我们介绍了很多软件关于思维导图有在......
  • 题解:UVA1456 Cellular Network
    UVA1456CellularNetwork题解夭寿了!30行写完紫题了!更新:已联系管理员修改难度,现在是绿题题意很简单,不再赘述。首先一个小贪心,将概率\(u​\)进行从大到小的排序,优先查看概率大的区域,显然这样能够保证访问数量期望最小。接着考虑如何将区域分组。一个显而易见的思路是动态......
  • 题解:P4288 [SHOI2014] 信号增幅仪
    很好一题目,使我的最小圆覆盖旋转。先假设\(p=1\)。这是最简单的情况。这个时候我们就得到了一个裸的最小圆覆盖。当\(p\not=1\),但是\(a=0\)的时候。圆就变成了对称轴与坐标轴平行的椭圆,运用高中知识仿射一下,又回到了最小圆覆盖。在一般的情况下,我们先通过坐标的旋转......
  • 火狐浏览器mfc140u.dll缺失引发的故障及高效恢复步骤
    火狐浏览器在运行时如果提示mfc140u.dll文件缺失,可能会引发程序启动失败、运行时异常或崩溃等故障。这个DLL文件是MicrosoftVisualC++RedistributablePackagesforVisualStudio2015的一部分,它包含了运行使用VisualStudio2015开发的C++应用程序所必需的MFC(MicrosoftFo......
  • 20240924_082514 c语言 switch分支结构
    语法演练体验switch的用法比较多路if一个个的比vs精准定位case穿透体验没有break的情况......
  • 20240924_102514 c语言 循环练习题
    ......