Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide password text

I have a textbox using UseSystemPasswordChar, so it will not display the password that the user enters. The issue is that the password is still able to be read by something like Spy++. I'm looking for a way to hide this like they do in the password fields in the Services.msc > Log On tab.

like image 365
LazarusG Avatar asked May 02 '12 06:05

LazarusG


2 Answers

Here is what I've got so far.

You can improve this by having some unique events to indicate whether a pressed key has been accepted, if InputFilter or RealText has been changed, etc...

Another great thing to improve would be the default usage of InputFilter, because working with char and Keys doesn't really work for many special keys. For example - at the moment, if you press Alt+F4 when the PasswordBox is in focus, it will type in 's'... So there's a bag of bugs to fix.

And lastly, there's probably a more elegant way to handle capital vs non-capital letters input 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;
        }
    }
}
like image 184
SimpleVar Avatar answered Nov 14 '22 23:11

SimpleVar


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 += "*";
        }
like image 29
Likurg Avatar answered Nov 15 '22 01:11

Likurg