如何在 Python 中隱藏和加密密碼?

如何在 Python 中隱藏和加密密碼?

有多種 Python 模組用於隱藏使用者輸入的密碼,其中一個是**maskpass()

模組。在 Python 中,藉助

maskpass()

模組和

base64()**模組,我們可以在輸入時使用星號(*) 隱藏使用者的密碼,然後藉助 base64() 模組可以對其進行加密。

maskpass()

maskpass() 是一個 Python 模組,可用於在輸入期間隱藏使用者的密碼。maskpass() 模組還提供了一種安全的方式來處理程式透過終端與使用者互動的密碼提示。

安裝:

在命令提示符下使用pip安裝 maskpass。

pip install maskpass

這些模組有兩種型別的功能/方法:

askpass()

advpass()

askpass():

askpass 使用標準庫獲取非阻塞輸入並返回密碼。

import maskpass pwd = maskpass。askpass()

上述程式碼執行將以字串格式返回輸入的密碼。askpass() 方法中有 2 個可選引數,分別是“提示”和“掩碼”。提示的預設值為“輸入密碼:”,掩碼的預設值為星號 (*)。

注意:如果您想用字串、數字或符號來掩蓋您的密碼,那麼只需在掩碼中傳遞該值。例如,如果你想用井號(#) 遮蔽你的密碼,然後在掩碼中傳遞井號,即 mask=”#”,現在當用戶輸入密碼時,該密碼將用井號(#) 隱藏。

示例 1:沒有在提示中回顯使用者的密碼

# 不回顯的使用者密碼import maskpass # 隱藏密碼 # 遮蔽密碼pwd = maskpass。askpass(mask=“”) print(pwd)

輸出:

F:\files>python password。py Enter Password : haiyong

在上面的例子中,使用者的密碼在輸入密碼時沒有在提示中回顯,因為掩碼中分配的值是空的,即掩碼=””(無空格),因此密碼被隱藏,沒有任何字串、符號。

示例 2:在提示中回顯使用者的密碼

# 回顯密碼並用井號標籤 (#) 遮蔽import maskpass # importing maskpass library# prompt msg = Password and# 用井號(#)遮蔽密碼pwd = maskpass。askpass(prompt=“Password:”, mask=“#”)print(pwd)

輸出:

F:\files>python password。py Password:############### haiyong

在上面的例子中,使用者的密碼在輸入密碼時會在提示中回顯,因為掩碼中分配的值是hashtag(#)即 mask=”#” 因此當用戶輸入密碼時,它會被隱藏井號(#)。

advpass():

advpass 使用 pynput 獲取文字並返回密碼。advpass 在控制檯和 Spyder 中都可以使用。

import maskpass pwd = maskpass。advpass()

上面的程式碼執行也會以字串格式返回輸入的密碼。advpass() 方法中有 4 個可選引數,它們是 ‘prompt’、‘mask’、‘ide’ 和 ‘suppress’。

這裡提示的預設值也是“輸入密碼:”

掩碼的預設值為星號 (*)。

這裡ide需要一個布林值,即 true 或 false,ide 的預設值為False。不需要更改 ide 的值,因為它會自動檢查它是在 IDE 還是在終端上執行。

suppress還需要一個布林值,即 true 或 false,僅在 Spyder IDE 中使用。將此設定為 True 可防止將輸入傳遞給系統的其餘部分。這可以防止 Spyder 控制檯在按下空格鍵時跳下。抑制的預設值為True。

advpass() 方法有一個顯示功能,當按下 Left-Ctrl 鍵時,它將切換使用者輸入密碼的可見性。再次按 Left-Ctrl 鍵以遮蔽/隱藏密碼。注意:這僅適用於 advpass() 並且需要 pynput。

示例 1:輸入密碼時不按左 ctrl 鍵

# 輸入密碼而不用左 CTRL 鍵import maskpass # importing maskpass library# 遮蔽密碼pwd = maskpass。advpass()print(‘Password : ’, pwd)

輸出:

F:\files>python password。py Enter Password: *************** Password : haiyong

在上面的輸出中,密碼用星號(*)符號隱藏,因為使用者沒有按下鍵盤上的左 ctrl 鍵。

示例 2:在輸入密碼的同時按下左 ctrl 鍵:

# 輸入密碼而不用左 CTRL 鍵

import maskpass # importing maskpass library

pwd = maskpass。advpass() # masking the password

print(‘Password : ’, pwd)

輸出:

F:\files>python password。py Enter Password: haiyong Password : haiyong

在上面的輸出中,密碼沒有隱藏,因為使用者按下了鍵盤上的左 ctrl 鍵。

base64()

base64 編碼和解碼功能都需要一個類似位元組的物件。要將字串轉換為位元組,我們必須使用 Python 的內建編碼函式對字串進行編碼。主要使用 UTF-8 編碼,您也可以使用 ‘ASCII’ 進行編碼,但我建議使用 UTF-8 編碼。

# encoding the stringstring = “haiyong”# encoding string with utf-8b = string。encode(“UTF-8”)print(b)

輸出:

F:\files>python strencode。py b‘greeksforgreek’

這裡b字首表示該值是一個位元組物件。

使用 base64() 模組對字串進行編碼:

要對字串進行編碼,即將字串轉換為位元組碼,請使用以下方法:

base64。b64encode(‘string’。encode(“utf-8”))

使用 base64() 模組解碼位元組碼:

要解碼位元組碼,即將位元組碼再次轉換為字串,請使用以下方法:

base64。b64decode(‘byte-code’)。decode(“utf-8”)

例子:

# 匯入用於編碼和解碼字串的 base64 模組import base64string = “haiyong”# 編碼字串encode = base64。b64encode(string。encode(“utf-8”))print(“str-byte : ”, encode)# 解碼字串decode = base64。b64decode(encode)。decode(“utf-8”)print(“byte-str : ”, decode)

輸出:

F:\files>python base64。py str-byte : b‘R3JlZWtzZm9yR3JlZWtz’ byte-str : haiyong

在上面的例子中,“haiyong”字串首先使用base64模組編碼,即字串被轉換為位元組碼,然後在base64模組的幫助下再次將位元組碼解碼為其原始字串,即“haiyong”。

在輸入時間內隱藏使用者密碼

# 使用 maskpass() 隱藏輸入的密碼並使用 base64() 對其進行加密import maskpass # to hide the passwordimport base64 # to encode and decode the password# 以使用者名稱作為鍵和密碼作為值的字典dict = {‘Rahul’: b‘cmFodWw=’, ‘Sandeep’: b‘U2FuZGVlcA==’}# 建立密碼的功能def createpwd(): print(“\n========Create Account=========”) name = input(“Username : ”) # 使用提示 msg ‘Password :’ 遮蔽密碼 pwd = maskpass。askpass(“Password : ”) # 對輸入的密碼進行編碼 encpwd = base64。b64encode(pwd。encode(“utf-8”)) # 在dict中附加使用者名稱和密碼 dict[name] = encpwd # print(dict)# 登入功能def sign_in(): print(“\n\n=========Login Page===========”) name = input(“Username : ”) # 使用提示 msg ‘Password :’ 遮蔽密碼 pwd = maskpass。askpass(“Password : ”) # 對輸入的密碼進行編碼 encpwd = base64。b64encode(pwd。encode(“utf-8”)) # 以使用者名稱作為dict中的鍵獲取密碼 password = dict[name] if(encpwd == password): print(“Successfully logged in。”) else: print(“Login Failed”)# 呼叫函式createpwd()sign_in()

輸出:

F:\files>python “userLogin。py”========Create Account=========Username : haiyongPassword : *****=========Login Page===========Username : haiyongPassword : *****Successfully logged in。

作者:海擁

連結:https://juejin。cn/post/7066337015792402445