【发布时间】:2019-06-18 01:50:43
【问题描述】:
我是python新手,真的不知道如何正确提问,所以请原谅我。
真的需要指导,是我想让用户使用输入输入他的密码,输出被屏蔽为星号,用户输入将用于更改另一个文件信息(我在 windows 上使用 python 3 10).
我知道我可以使用 getpass.getpass() 并将密码显示为星号是有风险的,因为它让其他人阅读并计算密码的长度,但因为我想让这个小应用程序看起来“专业”并让用户感到安全,然后我看到需要屏蔽密码。
最后应该保存输出以替换另一个文件基本信息,例如电子邮件、用户名和密码。
我已经成功替换了除了密码之外的所有密码,因为它只将输入的密码(mynewpassword12345)更改为“”。
下面是我从 SO 那里得到的一些链接,据说与我的问题有关:
Masking user input in python with asterisks 接受的答案没有关于如何或没有提供示例的方向。
Getting paths as user input Python 3 这个接受的答案与我的问题无关。
Input only 3 characters and 3 asterisk 这个接受的答案也与我的问题无关。
Change EditText password mask character to asterisk (*) 这个接受的答案也与我的问题无关。
Partial Password Masking on Input Field 这个接受的答案再次与我的问题无关。
Masking user input in python with asterisks 这个接受的答案与我的问题有关,但在 Windows 以外的平台上。
String to asterisk, masking password 同样,这里的这些答案与我的问题无关。
word (password) displays in asterisks - Console application 这个接受的答案再次与我的问题无关。
下面是我从 SO 得到的示例代码示例,将用户密码屏蔽为星号(但很抱歉我忘记保存链接)。
它在 python exe 上工作(但不是在空闲时)并将用户密码输入屏蔽为星号,但它不允许我将打印的密码保存为用户输出。
到目前为止,这就是我要做的,从特定路径中的 certin 文件夹中提取一个 zip 文件,提示用户输入并添加以更改默认信息,即电子邮件、ID 等。
from zipfile import ZipFile
from selenium.webdriver.support.ui import WebDriverWait
import fileinput
import os
import py_compile
import shutil, glob
import textwrap
import datetime
import msvcrt
import time
import sys
script_dir = os.path.dirname(__file__)
file_path2 = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , 'myapp.zip')
##print(file_path2)
NameFile2 = file_path2
with ZipFile(NameFile2, 'r') as zf:
zf.extractall(os.path.join(script_dir, 'alert' , 'temp' , 'temp folder' , 'temp' ) , pwd=b'mypassword' )
file_path = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , 'myapp.py')
##print(file_path)
NameFile = file_path
email = "emailaddress"
password = "userpassword"
userID = "userID"
id1 = "id1"
id2 = "id2"
id3 = "id3"
id4 = "id4"
id5 = "id5"
id6 = "id6"
id7 = "id7"
id8 = "id8"
id9 = "id9"
idlast = "idlast"
level = 3
##
x = datetime.datetime.now()
text1 = '''hi, welcome to my app.
'''
#print('\t' * level, text1 )
#print(textwrap.fill(text1, 60))
print(" ")
print((x.strftime("%c")).center(80))
print(" ")
for line in textwrap.wrap(text1, width=50):
print(line.center(80))
print(" ")
newemail = input (" email : " )
#newpassword = input (" password : ")
list1 = []
def getPASS():
print("Input password")
list1 = []
while True:
char = msvcrt.getch()
char =str(char)
char = char[2:-1]
if char == "\\n'" or char == "\\r":
break
elif char == "\\x08":
del list1[-1]
os.system("cls")
print("Input password:")
sys.stdout.write("*" * len(list1))
sys.stdout.flush()
continue
else:
list1.append(char)
sys.stdout.write("*")
sys.stdout.flush()
print("\n")
psw = "".join(list1)
print(psw)
invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
for x in psw:
if x in invalid:
print("Character %r is not allowed in password" % x)
getPASS()
else:
pass
getPASS()
newpassword = psw
newuserID = input (" userID : ")
newid1 = input (" ID seefirst1 : ")
newid2 = input (" ID seefirst2 : ")
newid3 = input (" ID seefirst3 : ")
newid4 = input (" ID seefirst4 : ")
newid5 = input (" ID seefirst5 : ")
newid6 = input (" ID seefirst6 : ")
newid7 = input (" ID seefirst7 : ")
newid8 = input (" ID seefirst8 : ")
newid9 = input (" ID seefirst9 : ")
newidlast = input (" ID seefirst10 : ")
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(email, newemail), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(password, newpassword), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(userID, newuserID), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id1, newid1), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id2, newid2), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id3, newid3), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id4, newid4), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id5, newid5), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id6, newid6), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id7, newid7), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id8, newid8), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(id9, newid9), end='')
with fileinput.FileInput(NameFile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(idlast, newidlast), end='')
#wait = WebDriverWait(driver, 10)
file_path3 = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , 'myapp.py')
#print(file_path3)
script = file_path3
py_compile.compile(script)
from pathlib import Path
# ...
#pathtofile = Path(__file__).parent.parent.parent.parent.parent # Path to Folder myapp
pathtoduplicate = os.path.join(script_dir ,'myapp.cpython-37.pyc')
#print(pathtoduplicate)
if os.path.exists(pathtoduplicate):
os.remove(pathtoduplicate)
else:
print("The file does not exist")
##print(pathtofile)
source_dir = os.path.join(script_dir, ' alert ' , 'temp' , 'temp folder' , 'temp' , '__pycache__') #Path to Pycache
dst = os.path.join(script_dir) # Path to myapp
files = glob.iglob(os.path.join(source_dir, "*.pyc"))
for file in files:
if os.path.isfile(file):
shutil.move(file, dst)
raise SystemExit()
我得到了我希望输入显示为星号的结果,但它没有改变输出,只显示“”而不是实际密码。
想要的结果: 屏蔽输入密码已替换默认密码。
email = "mynewemail@gmail.com"
password = "mynewpassword12345"
user = "mynewnewid1"
seefirst1 = "newid1"
seefirst2 = "newid2"
seefirst3 = "newid3"
seefirst4 = "newid4"
seefirst5 = "newid5"
seefirst6 = "newid6"
seefirst7 = "newid7"
seefirst8 = "newid8"
seefirst9 = "newid9"
seefirst10 = "newid10"
不需要的结果: 屏蔽的输入密码没有替换默认密码。
email = "mynewemail@gmail.com"
password = ""
user = "mynewnewid1"
seefirst1 = "newid1"
seefirst2 = "newid2"
seefirst3 = "newid3"
seefirst4 = "newid4"
seefirst5 = "newid5"
seefirst6 = "newid6"
seefirst7 = "newid7"
seefirst8 = "newid8"
seefirst9 = "newid9"
seefirst10 = "newid10"
请帮我解决这个问题。我是菜鸟,对 python 很陌生。
【问题讨论】:
-
如果密码以明文形式出现在文本文件中,那么无论如何安全都是一个失败的原因。
-
密码最终会出现在一个文本文件中,但在上述基本脚本结束时会自动删除提取/修改的文件。它还将创建新的 pyc 文件(是的,我知道这个 pyc 在可以解码的时候也不安全,但我认为它应该足以让用户认为它是安全的(用户是非编码人员)。
-
“让用户认为它是安全的”而不是非常不道德。
-
bruno desthuilliers 先生,感谢您的评论,我接受它作为建议。实际上,我让用户拥有自己在自己的计算机上使用的编码 pyc 脚本。我现在是一个真正的基础学习者,我仍然不知道如何制作一个安全的脚本,还有很多我自己无法解决的问题,这就是我来这里学习的原因你和许多更高级的程序员。
标签: python python-3.x