python密碼學(xué)各種加密模塊教程
在本章中,您將詳細(xì)了解Python中各種加密模塊.
加密模塊
它包含所有配方和基元,并在Python中提供高級(jí)編碼接口.您可以使用以下命令安裝加密模塊 :
pip install cryptography

代碼
您可以使用以下代碼實(shí)現(xiàn)加密模塊 :
from?cryptography.fernet?import?Fernet
key?=?Fernet.generate_key()
cipher_suite?=?Fernet(key)
cipher_text?=?cipher_suite.encrypt("This?example?is?used?to?demonstrate?cryptography?module")
plain_text?=?cipher_suite.decrypt(cipher_text)輸出
上面給出的代碼產(chǎn)生以下輸出 :

此處給出的代碼用于驗(yàn)證密碼并創(chuàng)建其哈希值.
它還包括用于驗(yàn)證密碼以進(jìn)行身份驗(yàn)證的邏輯.
import?uuid
import?hashlib
def?hash_password(password):
???#?uuid?is?used?to?generate?a?random?number?of?the?specified?password
???salt?=?uuid.uuid4().hex
???return?hashlib.sha256(salt.encode()?+?password.encode()).hexdigest()?+?':'?+?salt
def?check_password(hashed_password,?user_password):
???password,?salt?=?hashed_password.split(':')
???return?password?==?hashlib.sha256(salt.encode()?+?user_password.encode()).hexdigest()
new_pass?=?input('Please?enter?a?password:?')
hashed_password?=?hash_password(new_pass)
print('The?string?to?store?in?the?db?is:?'?+?hashed_password)
old_pass?=?input('Now?please?enter?the?password?again?to?check:?')
if?check_password(hashed_password,?old_pass):
???print('You?entered?the?right?password')
else:
???print('Passwords?do?not?match')輸出
場景1 : 如果您輸入了正確的密碼,您可以找到以下輸出 :

情景2 : 如果我們輸入錯(cuò)誤的密碼,您可以找到以下輸出 :

說明
Hashlib 包用于在數(shù)據(jù)庫中存儲(chǔ)密碼.在此程序中,使用 salt ,在實(shí)現(xiàn)哈希函數(shù)之前,將隨機(jī)序列添加到密碼字符串中.
以上就是python密碼學(xué)各種加密模塊教程的詳細(xì)內(nèi)容,更多關(guān)于Python密碼學(xué)加密模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python?hashlib模塊與哈希算法保護(hù)數(shù)據(jù)完整性教程
- Python基礎(chǔ)之hashlib模塊subprocess模塊logging模塊
- Python?HMAC模塊維護(hù)數(shù)據(jù)安全技術(shù)實(shí)例探索
- python借助ChatGPT讀取.env實(shí)現(xiàn)文件配置隔離保障私有數(shù)據(jù)安全
- Python3.10耙梳加密算法Encryption種類及開發(fā)場景
- python密碼學(xué)RSA密碼加密教程
- python密碼學(xué)實(shí)現(xiàn)文件加密教程
- Python hashlib庫數(shù)據(jù)安全加密必備指南
相關(guān)文章
python獲取外網(wǎng)ip地址的方法總結(jié)
這篇文章主要介紹了python獲取外網(wǎng)ip地址的方法,實(shí)例總結(jié)了四種常用的獲取外網(wǎng)IP地址的技巧,需要的朋友可以參考下2015-07-07
Python設(shè)計(jì)密碼強(qiáng)度校驗(yàn)程序
這篇文章主要介紹了Python如何設(shè)計(jì)密碼強(qiáng)度校驗(yàn)程序,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
python并發(fā)爬蟲實(shí)用工具tomorrow實(shí)用解析
這篇文章主要介紹了python并發(fā)爬蟲實(shí)用工具tomorrow實(shí)用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python基于文本內(nèi)容實(shí)現(xiàn)隱私信息提取與評(píng)估
這篇文章主要為大家介紹了Python如何實(shí)現(xiàn)基于文本內(nèi)容的用戶隱私泄露風(fēng)險(xiǎn)評(píng)估系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-03-03

