mask password string

自闭症网瘾萝莉.ら 提交于 2019-12-11 16:16:32

问题


I'm creating a C# windows forms app that logs me in to my servers with a single click using MSTSC. I have my admin name and password in the code in plain text and wondered is there a way to mask/hide the password? I store my code in my Dropbox and would prefer it wasn't readable.

    private void RunAsAdmin(string server)
    {
        Process rdcProcess = new Process();
        rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");
        rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "Administrator" + " /pass:" + "myPassword";
        rdcProcess.Start();

回答1:


Use this function to encrypt and decrypt the password.

 Public Function ConvertPassword(ByVal sPassword As String)
        Dim sTempChar As String
        Dim iCount As Integer

        For iCount = 1 To Len(sPassword)
            If Asc(Mid$(sPassword, iCount, 1)) < 128 Then
                sTempChar = CType(Asc(Mid$(sPassword, iCount, 1)) + 128, String)
            ElseIf Asc(Mid$(sPassword, iCount, 1)) > 128 Then
                sTempChar = CType(Asc(Mid$(sPassword, iCount, 1)) - 128, String)
            End If

            Mid$(sPassword, iCount, 1) = Chr(CType(sTempChar, Integer))
        Next iCount

        Return sPassword
    End Function



回答2:


I would agree that storing the password in the file is a bad idea. What I've done on occasion is to base64 encode it. I realize that it is VERY insecure but I am only trying to prevent a casual reader from seeing the password.




回答3:


As part of the answer you can mask the password by setting the following property in e.g. an windows forms textbox:

TextBox.UseSystemPasswordChar = true;


来源:https://stackoverflow.com/questions/27246365/mask-password-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!