完成一个 Python 程序,实现密码存储和验证,用于模拟身份验证服务器,以便破解数据库的对手无法获得用户的明文密码。实现基本的注册和登录功能。

测试样例如下:

input

R
timerring
i love cryptography
L
timerring
i love cryptography
L
timerring
i love cipher
L
Who
i love cryptography
Q

该系统的源码如下:

source code

from __future__ import annotations
from typing import Dict
import getpass
import hashlib
import osdatabase: Dict[str, UserPassword] = dict()class UserPassword:def __init__(self, in_username, in_password_hash, in_salt):self.username: str = in_usernameself.password_hash: bytes = in_password_hashself.salt: bytes = in_saltself.method: str = 'scrypt'# define verify_password functiondef verify_password(self, password: str) -> bool:password_ver: bytes = password.encode("utf-8")# use the same saltsalt_ver: bytes = self.salt# set corresponding parametersn: int = 4r: int = 8p: int = 16password_hash_ver: bytes = hashlib.scrypt(password_ver, salt=salt_ver, n=n, r=r, p=p)# verify the hash value of the passwordif password_hash_ver == self.password_hash:return Trueelse:return Falsedef database_add_item(user: UserPassword) -> None:if user.username in database:raise Exception('User {} already exists.'.format(user.username))database[user.username] = userdef login_user(username: str, password_plaintext: str) -> bool:if username not in database:raise Exception('User {} does not exist.'.format(username))return database[username].verify_password(password_plaintext)def register_user(username: str, password_plaintext: str) -> None:password_bytes: bytes = password_plaintext.encode("utf-8")# The os.urandom function is used to obtain random bytes of a specified length# generate the salt bytessalt_bytes: bytes = os.urandom(64)# set corresponding parametersn: int = 4r: int = 8p: int = 16# Hash encryptionpassword_hash: bytes = hashlib.scrypt(password_bytes, salt=salt_bytes, n=n, r=r, p=p)# construct instance objectUser: UserPassword = UserPassword(username, password_hash, salt_bytes)# Add to databasedatabase_add_item(User)if __name__ == '__main__':while True:try:print('Usage:')print('\tR - register a new user')print('\tL - login with an existing user')print('\tQ - exit')print('')command: str = input('Input command:')if command == 'Q':exit(0)elif command == 'R' or command == 'L':username: str = input('Input username:')# password: str = getpass.getpass('Input password:') # will not work properly for PyCharm, IDLE, etc.password: str = input('Input password:')if command == 'R':register_user(username, password)print('User created successfully.')elif command == 'L':login_valid: bool = login_user(username, password)if login_valid:print('User logged in successfully.')else:print('Password verification failed. Can not logged in.')else:assert Falseelse:raise Exception('Invalid command.')except Exception as e:print('Error: {}'.format(e))

output

Usage:R - register a new userL - login with an existing userQ - exitInput command:R
Input username:timerring
Input password:i love cryptography
User created successfully.
Usage:R - register a new userL - login with an existing userQ - exitInput command:L
Input username:timerring
Input password:i love cryptography
User logged in successfully.
Usage:R - register a new userL - login with an existing userQ - exitInput command:L
Input username:timerring
Input password:i love cipher
Password verification failed. Can not logged in.
Usage:R - register a new userL - login with an existing userQ - exitInput command:L
Input username:Who
Input password:i love cryptography
Error: User Who does not exist.
Usage:R - register a new userL - login with an existing userQ - exitInput command:Q进程已结束,退出代码为 0

由输出可知,系统功能一切正常。

受于文本篇幅原因,本文相关算法实现工程例如环境及相关库,无法展示出来,现已将资源上传,可自行点击下方链接下载。

python实现模拟身份验证服务器综合系统工程文件

python实现模拟身份验证服务器综合系统设计相关推荐

  1. Google Authenticator:将其与您自己的Java身份验证服务器配合使用

    用于移动设备的Google Authenticator应用程序是一个非常方便的应用程序,它实现了TOTP算法(在RFC 6238中指定). 使用Google Authenticator,您可以生成时间 ...

  2. Python计算谷歌身份验证器(google authenticator)的验证码

    公司的上班时间是弹性的,每天上满8小时即可下班.楼主这种懒人基本都是每天卡点下班,需要根据早上的打卡时间推算8小时.但登录OA查询打卡时间又要填谷歌身份验证码(运维大哥搞的很严), 所以自己用PyQt ...

  3. python连接Windows身份验证 SQL Server

    Windows 身份验证登录 只需要2个参数 import pymssqldef connect():conn = pymssql.connect(server='localhost', databa ...

  4. 模拟苹果验证服务器,[原创]苹果 gsa 服务器login 算法

    闲得蛋疼,对iTunes 登陆过程进行了初步的分析. 解密得到了spd字段内容. 将分析过程写出来,混个熟脸. 在分析这个东西之前, 需要做一些准备工作.  比如收集论坛前辈们的工作经验, githu ...

  5. ASP.NET协作应用集成到trsids身份验证服务器的开发流程

    开发Actor协同模块: (参考TRSIDS4.0 协作应用集成手册[asp.net]) ASP.Net协作应用集成到IDS之前,需要开发Actor类实现协作应用回调接口中定义的本地登录.退出.用户信 ...

  6. 【CCNA】思科PPP身份验证(PAP单向认证与CHAP单向认证)

    实验拓扑: 很简单的拓扑,R2模拟身份验证服务器,R1模拟被认证者. 实验目的:通过PAP验证和CHAP验证,使R1连通R2. 实验步骤: 首先配好R1,R2的IP地址. R1#conf t Ente ...

  7. linux 验证邮箱账号,linux邮件服务器的身份验证(sasl)

    sasl SASL全称SimpleAuthenticationandSecurityLayer,是一种用来扩充C/S模式验证能力的机制.在Postfix可以利用SASL来判断用户是否有权使用转发服务, ...

  8. 两台服务器身份验证,详解三种不同的身份验证协议

    本文最初发布于devever.net网站,经原作者授权由InfoQ中文站翻译并分享. 现在,身份验证协议的数量快赶上应用程序协议,结果,这个领域很容易让人困惑. 最容易把人搞糊涂的是,很少有人注意到这 ...

  9. 客官,来看看AspNetCore的身份验证吧

    作者:句幽 来源:https://www.cnblogs.com/uoyo/p/13209685.html 开篇 这段时间潜水了太久,终于有时间可以更新一篇文章了. 通过本篇文章您将Get: Http ...

最新文章

  1. CMD——ping及用其检测网络故障
  2. 大数据时代的隐身模式下的大数据创业公司
  3. 邪恶的三位一体:机器学习、黑暗网络和网络犯罪
  4. ZBrush关于遮罩的一些操作
  5. html5 video全屏api,H5+ app使用多媒体(video、webview)API视频展示以及全屏
  6. 线性表【项目 - 求集合并集C语言】
  7. 离散图 java,Java实现离散Arnold变换(图像处理)
  8. Fiori Elements - when is project webapp folder annotation.xml loaded
  9. 用VS2013如何编写C语言
  10. 微软符号服务器opencv的符号,Opencv Mat类详解和用法1
  11. 硅谷半夜3点惊现无人驾驶特斯拉!百公里时速飞驰,交警围追10公里才逼停
  12. EverWeb for Mac(网页设计软件)v3.5.1中文版
  13. 把手机作为网站服务器,如何利用废旧手机打造网站服务器
  14. python适合创业吗-python创业
  15. ios html fixed,关于IOS的Safari浏览器fixed定位失效的那些坑
  16. 三星Galaxy Note 10.1刷机教程
  17. Windows 10错误在打开特定路径时导致BSOD崩溃
  18. 服务计算 -- 搭建私有云
  19. 红米3s安装xposed过程总结
  20. 解决“spring-boot-maven-plugin”报红

热门文章

  1. linux实训心得体会范文
  2. Redis的几种数据结构的特点
  3. 软件集成测试采用,集成测试的组成以及流程
  4. 高等数学学习笔记——第七十六讲——直角坐标系下二重积分的计算
  5. Kotlin高仿微信-第14篇-单聊-视频通话
  6. 51单片机_7-1独立按键控制流水灯方向
  7. mysql 交换分区吗_MySQL分区表——交换分区
  8. 架构搜索文献笔记(9):《CurveLane-NAS: Unifying Lane-Sensitive Architecture Search and Adaptive Point》
  9. 一键获取网易互娱内推攻略,快人一步拿offer!
  10. Partial Adversarial Domain Adaptation学习笔记