Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Created September 21, 2020 16:10
Show Gist options
  • Save PrateekKumarSingh/3170c48eb20350310d871ff1ba7811fe to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/3170c48eb20350310d871ff1ba7811fe to your computer and use it in GitHub Desktop.
using System;
using System.Management.Automation;
namespace app {
class Program {
static void Main(string[] args) {
Console.Write ("Enter your password: ");
var password = string.Empty;
ConsoleKey key;
do {
var keyInfo = Console.ReadKey(intercept: true);
key = keyInfo.Key;
// if backspace is pressed and password string has chars
if (key == ConsoleKey.Backspace && password.Length > 0) {
Console.Write("\b \b");
password = password.Substring(0,(password.Length-1));
}
// if input char is not a control char like CR, LF
else if (!char.IsControl(keyInfo.KeyChar)) {
Console.Write("*");
password += keyInfo.KeyChar;
}
// exit infinite loop when 'ENTER' is pressed
} while (key != ConsoleKey.Enter);
Console.WriteLine("\nYour Password is: "+password);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment