意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Python中各种加密模块的应用是怎样的

来源:恒创科技 编辑:恒创科技编辑部
2023-12-30 14:15:59
这篇文章主要介绍了Python中各种加密模块的应用是怎样的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python中各种加密模块的应用是怎样的文章都会有所收获,下面我们一起来看看吧。

    


Python中各种加密模块的应用是怎样的

在本章中,您将详细了解Python中各种加密模块.

加密模块

它包含所有配方和基元,并在Python中提供高级编码接口.您可以使用以下命令安装加密模块 :

 pip install cryptography

代码

您可以使用以下代码实现加密模块 :

fromcryptography.fernetimportFernet
key=Fernet.generate_key()
cipher_suite=Fernet(key)
cipher_text=cipher_suite.encrypt("Thisexampleisusedtodemonstratecryptographymodule")
plain_text=cipher_suite.decrypt(cipher_text)

输出

上面给出的代码产生以下输出 :

此处给出的代码用于验证密码并创建其哈希值.

它还包括用于验证密码以进行身份验证的逻辑.

importuuid
importhashlib
defhash_password(password):
#uuidisusedtogeneratearandomnumberofthespecifiedpassword
salt=uuid.uuid4().hex
returnhashlib.sha256(salt.encode()+password.encode()).hexdigest()+':'+salt
defcheck_password(hashed_password,user_password):
password,salt=hashed_password.split(':')
returnpassword==hashlib.sha256(salt.encode()+user_password.encode()).hexdigest()
new_pass=input('Pleaseenterapassword:')
hashed_password=hash_password(new_pass)
print('Thestringtostoreinthedbis:'+hashed_password)
old_pass=input('Nowpleaseenterthepasswordagaintocheck:')
ifcheck_password(hashed_password,old_pass):
print('Youenteredtherightpassword')
else:
print('Passwordsdonotmatch')

输出

场景1: 如果您输入了正确的密码,您可以找到以下输出 :

情景2: 如果我们输入错误的密码,您可以找到以下输出 :

说明

Hashlib包用于在数据库中存储密码.在此程序中,使用salt,在实现哈希函数之前,将随机序列添加到密码字符串中.


以上就是关于“Python中各种加密模块的应用是怎样的”的介绍了,感谢各位的阅读,希望这篇文章能帮助大家解决问题。如果想要了解更多知识,欢迎关注恒创科技,小编每天都会为大家更新不同的知识。
上一篇: 在Python中如何实现Vignere密码,代码是什么 下一篇: Python format的基本用法和进阶用法是什么