Posted by: R Manimaran | July 3, 2010

Masking password in C# Console application

In windows and web application we have the masked text boxes to enter password. When we enter the password it masks the password with character( *). In the Console application, there may be situation where you want the user to enter some password. But, how to mask the password in console application? Here is the code to mask the password while the user enters the password.

static void Main(string[] args)


string LoginId = string.Empty;


Console.WriteLine(“Enter the Login Name:”);

LoginId = Console.ReadLine();

Console.WriteLine(“Enter the Password:”);

StringBuilder pass = new StringBuilder();

char key;

while((key = Console.ReadKey(true).KeyChar)!=’\r’)


if (key == ‘\b’ && pass.Length > 0)


Console.Write(key + “” + key);

pass = pass.Remove(pass.Length – 1, 1);


else if (Char.IsLetterOrDigit(key))



pass = pass.Append(key);




Console.WriteLine(“Thanks for Providing user name and password:”+pass.ToString());

Console.BackgroundColor = ConsoleColor.DarkGreen;




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: