当python程序需要配置密码时,密码部分防止泄露,所以需要进行加密。
import msvcrt
def pwd_input():
"""
输入信息加密
"""
chars = []
while True:
try:
newChar = msvcrt.getch().decode(encoding="utf-8")
except:
return input("你很可能不是在cmd命令行下运行,密码输入将不能隐藏:")
if newChar in '\r\n':
break
elif newChar == '\b':
if chars:
del chars[-1]
msvcrt.putch('\b'.encode(encoding='utf-8'))
msvcrt.putch( ' '.encode(encoding='utf-8'))
msvcrt.putch('\b'.encode(encoding='utf-8'))
else:
chars.append(newChar)
msvcrt.putch('*'.encode(encoding='utf-8'))
return (''.join(chars))
注:以上代码只适用于在命令行中运行输入,不支持在IDE中使用