问题
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