Home All Groups Group Topic Archive Search About

How to mask console password input

Author
14 Aug 2006 8:44 AM
julianmoors
Hey,

Currently I'm writing a VB.NET/1.1 app and I need to mask the input for
the password field. Does anyone know how to do this in VB? I've seen a
C# example, but wouldn't know how to convert it myself. Here's the URL:
http://www.codeproject.com/dotnet/ConsolePasswordInput.asp

Author
14 Aug 2006 8:56 AM
Pritcham
Hi Julian

One of the comments in the codeproject entry you've listed points to a
blog entry that does exactly what you're asking for using SecureString
- take a look at
http://blogs.msdn.com/shawnfa/archive/2004/05/27/143254.aspx which
seems to be what you're after.

hope that helps
Martin
julianmo***@hotmail.com wrote:
Show quoteHide quote
> Hey,
>
> Currently I'm writing a VB.NET/1.1 app and I need to mask the input for
> the password field. Does anyone know how to do this in VB? I've seen a
> C# example, but wouldn't know how to convert it myself. Here's the URL:
> http://www.codeproject.com/dotnet/ConsolePasswordInput.asp
Author
14 Aug 2006 7:05 PM
julianmoors
Hi Pritcham,

Thanks for replying to my message, but the example in the MSDN blog
seems to be describing a Whidbey (VB.NET 2005) sample and I'm currently
using VB.NET 2003. The code on there doesn't make any sense to me.
Could you possibly convert this to VB.NEt 2003 for me? I just need the
masking part from below such as:

while(nextKey.Key != ConsoleKey.Enter)
    {
        if(nextKey.Key == ConsoleKey.BackSpace)
        {
            if(password.Length > 0)
            {
                password.RemoveAt(password.Length - 1);

                // erase the last * as well
                Console.Write(nextKey.KeyChar);
                Console.Write(" ");
                Console.Write(nextKey.KeyChar);
            }
        }
        else
        {
            password.AppendChar(nextKey.KeyChar);
            Console.Write("*");
        }

        nextKey = Console.ReadKey(true);
    }

.... and below is the full example:

public static SecureString GetPassword()
{
    SecureString password = new SecureString();

    // get the first character of the password
    ConsoleKeyInfo nextKey = Console.ReadKey(true);

    while(nextKey.Key != ConsoleKey.Enter)
    {
        if(nextKey.Key == ConsoleKey.BackSpace)
        {
            if(password.Length > 0)
            {
                password.RemoveAt(password.Length - 1);

                // erase the last * as well
                Console.Write(nextKey.KeyChar);
                Console.Write(" ");
                Console.Write(nextKey.KeyChar);
            }
        }
        else
        {
            password.AppendChar(nextKey.KeyChar);
            Console.Write("*");
        }

        nextKey = Console.ReadKey(true);
    }

    Console.WriteLine();

    // lock the password down
    password.MakeReadOnly();
    return password
}


Pritcham wrote:
Show quoteHide quote
> Hi Julian
>
> One of the comments in the codeproject entry you've listed points to a
> blog entry that does exactly what you're asking for using SecureString
> - take a look at
> http://blogs.msdn.com/shawnfa/archive/2004/05/27/143254.aspx which
> seems to be what you're after.
>
> hope that helps
> Martin
> julianmo***@hotmail.com wrote:
> > Hey,
> >
> > Currently I'm writing a VB.NET/1.1 app and I need to mask the input for
> > the password field. Does anyone know how to do this in VB? I've seen a
> > C# example, but wouldn't know how to convert it myself. Here's the URL:
> > http://www.codeproject.com/dotnet/ConsolePasswordInput.asp
Author
14 Aug 2006 9:49 PM
Pritcham
How about something like the following:

Public Shared Function GetPassword() As SecureString
    Dim password As New SecureString()

    ' get the first character of the password
    Dim nextKey As ConsoleKeyInfo = Console.ReadKey(True)

    Do While nextKey.Key <> ConsoleKey.Enter
        If nextKey.Key = ConsoleKey.BackSpace Then
            If password.Length > 0 Then
                password.RemoveAt(password.Length - 1)

                ' erase the last * as well
                Console.Write(nextKey.KeyChar)
                Console.Write(" ")
                Console.Write(nextKey.KeyChar)
            End If
        Else
            password.AppendChar(nextKey.KeyChar)
            Console.Write("*")
        End If

        nextKey = Console.ReadKey(True)
    Loop

    Console.WriteLine()

    ' lock the password down
    password.MakeReadOnly()
    Return password

Hope that helps
Martin
julianmo***@hotmail.com wrote:
Show quoteHide quote
> Hi Pritcham,
>
> Thanks for replying to my message, but the example in the MSDN blog
> seems to be describing a Whidbey (VB.NET 2005) sample and I'm currently
> using VB.NET 2003. The code on there doesn't make any sense to me.
> Could you possibly convert this to VB.NEt 2003 for me? I just need the
> masking part from below such as:
>
> while(nextKey.Key != ConsoleKey.Enter)
>     {
>         if(nextKey.Key == ConsoleKey.BackSpace)
>         {
>             if(password.Length > 0)
>             {
>                 password.RemoveAt(password.Length - 1);
>
>                 // erase the last * as well
>                 Console.Write(nextKey.KeyChar);
>                 Console.Write(" ");
>                 Console.Write(nextKey.KeyChar);
>             }
>         }
>         else
>         {
>             password.AppendChar(nextKey.KeyChar);
>             Console.Write("*");
>         }
>
>         nextKey = Console.ReadKey(true);
>     }
>
> ... and below is the full example:
>
> public static SecureString GetPassword()
> {
>     SecureString password = new SecureString();
>
>     // get the first character of the password
>     ConsoleKeyInfo nextKey = Console.ReadKey(true);
>
>     while(nextKey.Key != ConsoleKey.Enter)
>     {
>         if(nextKey.Key == ConsoleKey.BackSpace)
>         {
>             if(password.Length > 0)
>             {
>                 password.RemoveAt(password.Length - 1);
>
>                 // erase the last * as well
>                 Console.Write(nextKey.KeyChar);
>                 Console.Write(" ");
>                 Console.Write(nextKey.KeyChar);
>             }
>         }
>         else
>         {
>             password.AppendChar(nextKey.KeyChar);
>             Console.Write("*");
>         }
>
>         nextKey = Console.ReadKey(true);
>     }
>
>     Console.WriteLine();
>
>     // lock the password down
>     password.MakeReadOnly();
>     return password
> }
>
>
> Pritcham wrote:
> > Hi Julian
> >
> > One of the comments in the codeproject entry you've listed points to a
> > blog entry that does exactly what you're asking for using SecureString
> > - take a look at
> > http://blogs.msdn.com/shawnfa/archive/2004/05/27/143254.aspx which
> > seems to be what you're after.
> >
> > hope that helps
> > Martin
> > julianmo***@hotmail.com wrote:
> > > Hey,
> > >
> > > Currently I'm writing a VB.NET/1.1 app and I need to mask the input for
> > > the password field. Does anyone know how to do this in VB? I've seen a
> > > C# example, but wouldn't know how to convert it myself. Here's the URL:
> > > http://www.codeproject.com/dotnet/ConsolePasswordInput.asp