Python 如何屏蔽用户密码输入并将其另存为输出,以替换其他文件中的默认密码

Python 如何屏蔽用户密码输入并将其另存为输出,以替换其他文件中的默认密码,python,python-3.x,Python,Python 3.x,我是python新手,不知道如何正确提问,所以请原谅我 真正需要指导的是,我想让用户使用输入输入他的密码,输出被屏蔽为星号,用户输入将用于更改另一个文件信息(我在windows 10上使用python 3) 我知道我可以使用getpass.getpass()并将密码显示为星号是有风险的,因为它可以让其他人读取和计算密码的长度,但由于我想让这个小应用看起来“专业”,让用户感到安全,因此我认为有必要屏蔽密码 email = "mynewemail@gmail.com" password = "m

我是python新手,不知道如何正确提问,所以请原谅我

真正需要指导的是,我想让用户使用输入输入他的密码,输出被屏蔽为星号,用户输入将用于更改另一个文件信息(我在windows 10上使用python 3)

我知道我可以使用getpass.getpass()并将密码显示为星号是有风险的,因为它可以让其他人读取和计算密码的长度,但由于我想让这个小应用看起来“专业”,让用户感到安全,因此我认为有必要屏蔽密码

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"
最后,应该保存输出以替换另一个文件基本信息,如电子邮件、用户id和密码

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"
我成功地替换了除密码外的所有密码,因为它只将输入的密码(mynewpassword12345)更改为“”

下面是我从SO获得的一些链接,据说与我的问题有关:

被接受的答案没有关于如何做的指导,或者没有提供示例

这个被接受的答案与我的问题无关

这个公认的答案也与我的问题无关

这个公认的答案也与我的问题无关

这个被接受的答案与我的问题无关

这个被接受的答案与我的问题有关,但在windows以外的平台上

同样,这里的答案与我的问题无关

这个被接受的答案与我的问题无关

下面是我从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"
不想要的结果: 屏蔽输入密码未替换默认密码

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非常陌生。

抛开方法中的其他问题不谈,我将关注您的“不想要的结果:屏蔽输入密码没有取代默认密码。”

  • 您有一个全局的
    list1
    ,您还可以在
    getPASS()
    函数中定义一个
    list1
    ,从该函数返回后要使用该函数。这不能按原样工作,因为函数中的
    list1
    是本地的,并且与全局的
    list1
    不同。我建议从函数返回一个适当的值,e。g、 :

    def getPASS():
        print("Input password")
        list1 = []
        while True:
            …
        return "".join(list1)
    
    …
    psw = getPASS()
    
  • 密码输入和验证的逻辑在这里混淆了:

    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()
    
    您试图在调用
    getPASS()
    输入密码之前分配并验证密码,因此
    psw
    为空。重新安排,e。G(假设上述1.更改为
    getPASS()
    ):


  • 谢谢,非常感谢阿玛利爵士,没有他的指导,这个问题是无法解决的

    下面是我编辑的剧本的结果,正如阿玛利爵士在他被接受的答案中所建议的那样。(我不会接受我被接受的答案,只是作为对这部分感兴趣的人的参考。)


    如果密码最终以明文形式存在于文本文件中,则无论如何,安全性都是一个丢失的原因。密码最终将存在于文本文件中,但在此结束时,上述基本脚本将自动删除提取/修改的文件。它也会创建新的PYC文件(是的,我知道这个PYC在一个可以解码的地方也是不安全的,但是我认为它应该足够让用户认为它是安全的(用户是非编码人)”“让用户假设它是安全的”。如果不是,那是非常不道德的。布鲁诺·德舒利尔斯爵士,谢谢你的评论,我接受这一建议。实际上,我让用户自己在自己的电脑上使用经过编码的pyc脚本。作为一名基础学习者,我现在仍然不知道如何制作一个安全的脚本,我还有很多东西要做我自己无法解决这些问题,这就是为什么我来这里向您和更多高级编码人员学习的原因。谢谢您的回答,先生。我在几个小时前阅读了您的解决方案,最后在这里和那里进行了几个小时的更改后,我成功地按照您的建议完成了这项工作。非常感谢先生。(我已尝试将您的答案作为可接受的答案,但由于我尚未达到至少15个代表,我现在无法这样做。)