Hide password text

I have a text box using UseSystemPasswordChar, so it does not display the password that the user enters. The problem is that the password can still be read by something like Spy ++. I am looking for a way to hide this, as in the password field on the Services.msc> Log On tab.

+6
source share
3 answers

Here is what I got so far.

You can improve this by specifying several unique events to indicate whether a key was pressed if an InputFilter or RealText has been modified, etc.

Another great thing to improve is using DefaultFilter by default, since working with char and Keys really doesn't work for many special keys. For example, at the moment, if you press Alt + F4 when the PasswordBox is in focus, it will type β€œs” ... So, there is a bag of errors to fix.

And finally, there is probably a more elegant way to handle capital contributions and enter non-capital letters than what I did there.

So here it is:

public class PasswordBox : TextBox { private string _realText; public string RealText { get { return this._realText; } set { var i = this.SelectionStart; this._realText = value ?? ""; this.Text = ""; this.Text = new string('*', this._realText.Length); this.SelectionStart = i > this.Text.Length ? this.Text.Length : i; } } private Func<KeyEventArgs, bool> _inputFilter; public Func<KeyEventArgs, bool> InputFilter { get { return this._inputFilter; } set { this._inputFilter = value ?? (e => true); } } public PasswordBox() { this.RealText = ""; this.InputFilter = e => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".Any(c => c == e.KeyValue); } protected override void OnKeyDown(KeyEventArgs e) { e.SuppressKeyPress = true; switch (e.KeyCode) { case Keys.Back: if (this.SelectionStart > 0 || this.SelectionLength > 0) { this.RealText = this.SelectionLength == 0 ? this.RealText.Remove(--this.SelectionStart, 1) : this.RealText.Remove(this.SelectionStart, this.SelectionLength); } break; case Keys.Delete: if (this.SelectionStart == this.TextLength) { return; } this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength == 0 ? 1 : this.SelectionLength); break; case Keys.X: case Keys.C: case Keys.V: if (e.Control) { return; } goto default; case Keys.Right: case Keys.Left: case Keys.Up: case Keys.Down: case Keys.Shift: case Keys.Home: case Keys.End: e.SuppressKeyPress = false; base.OnKeyDown(e); break; default: if (e.Control) { e.SuppressKeyPress = false; base.OnKeyDown(e); break; } if (this.InputFilter(e)) { var c = (char)e.KeyValue; if (e.Shift == IsKeyLocked(Keys.CapsLock)) { c = char.ToLower(c); } this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength) .Insert(this.SelectionStart, c.ToString()); this.SelectionStart++; } break; } } } 
+2
source

So try something like this

  private string realpass = ""; private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (Char) Keys.Back) realpass += realpass.Substring(0, realpass.Length - 2); realpass += e.KeyChar.ToString(); textBox1.Text = ""; for (int i = 0; i < realpass.Length; i++) textBox1.Text += "*"; } 
+1
source

You should not use your own dialog if you intend to collect Windows / domain user credentials. You should use what Windows provides through PInvoke, or just use such a shell,

http://weblogs.asp.net/hernandl/archive/2005/11/21/usercredentialsdialog.aspx

and this,

http://credentials.codeplex.com/

+1
source

Source: https://habr.com/ru/post/914699/


All Articles