Example #1
0
 static System.Security.SecureString getPassword()
 {
     System.Security.SecureString pwd = new System.Security.SecureString();
     while (true)
     {
         System.ConsoleKeyInfo i = System.Console.ReadKey(true);
         if (i.Key == System.ConsoleKey.Enter)
         {
             System.Console.WriteLine();
             break;
         }
         else if (i.Key == System.ConsoleKey.Backspace)
         {
             if (pwd.Length > 0)
             {
                 pwd.RemoveAt(pwd.Length - 1);
                 System.Console.Write("\b \b");
             }
         }
         else
         {
             pwd.AppendChar(i.KeyChar);
             System.Console.Write("*");
         }
     }
     pwd.MakeReadOnly();
     return(pwd);
 }
Example #2
0
 static System.Security.SecureString getPassword()
 {
     System.Security.SecureString pwd = new System.Security.SecureString();
      while (true)
      {
     System.ConsoleKeyInfo i = System.Console.ReadKey(true);
     if (i.Key == System.ConsoleKey.Enter)
     {
        System.Console.WriteLine();
        break;
     }
     else if (i.Key == System.ConsoleKey.Backspace)
     {
        if (pwd.Length > 0)
        {
           pwd.RemoveAt(pwd.Length - 1);
           System.Console.Write("\b \b");
        }
     }
     else
     {
        pwd.AppendChar(i.KeyChar);
        System.Console.Write("*");
     }
      }
      pwd.MakeReadOnly();
      return pwd;
 }
Example #3
0
        static System.Security.SecureString GetSecPswd(string prompt)
        {
            System.Security.SecureString password = new System.Security.SecureString();

            System.Console.ForegroundColor = System.ConsoleColor.Gray;
            System.Console.Write(prompt);
            System.Console.ForegroundColor = System.ConsoleColor.Magenta;

            while (true)
            {
                System.ConsoleKeyInfo cki = System.Console.ReadKey(true);
                if (cki.Key == System.ConsoleKey.Enter)
                {
                    System.Console.ForegroundColor = System.ConsoleColor.Gray;
                    System.Console.WriteLine();
                    return(password);
                }
                else if (cki.Key == System.ConsoleKey.Backspace)
                {
                    // remove the last asterisk from the screen...
                    if (password.Length > 0)
                    {
                        System.Console.SetCursorPosition(System.Console.CursorLeft - 1, System.Console.CursorTop);
                        System.Console.Write(" ");
                        System.Console.SetCursorPosition(System.Console.CursorLeft - 1, System.Console.CursorTop);
                        password.RemoveAt(password.Length - 1);
                    }
                }
                else if (cki.Key == System.ConsoleKey.Escape)
                {
                    System.Console.ForegroundColor = System.ConsoleColor.Gray;
                    System.Console.WriteLine();
                    return(password);
                }
                else if (System.Char.IsLetterOrDigit(cki.KeyChar) || System.Char.IsSymbol(cki.KeyChar))
                {
                    if (password.Length < 20)
                    {
                        password.AppendChar(cki.KeyChar);
                        System.Console.Write("*");
                    }
                    else
                    {
                        System.Console.Beep();
                    }
                }
                else
                {
                    System.Console.Beep();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Reads masked keystrokes from the system's <see cref="Console"/>.
        /// </summary>
        /// <param name="useMask">
        /// true to write out an asterisk on every keystroke, otherwise, false.
        /// </param>
        /// <returns>A string.</returns>
        public static string ReadSecureString(bool useMask = false)
        {
            // borrowed from stackoverflow.com and slightly modified
            int[] filtered = { 0, 27, 9, 10 };

            var secureStr = new System.Security.SecureString();

            char chr;

            while ((chr = Console.ReadKey(true).KeyChar) != ENTER)
            {
                if ((chr == BACKSPACE || chr == CTRL_BACKSPACE) && secureStr.Length > 0)
                {
                    if (useMask)
                    {
                        Console.Write("\b \b");
                    }
                    secureStr.RemoveAt(secureStr.Length - 1);
                }
                else if (((chr == BACKSPACE) || (chr == CTRL_BACKSPACE)) && (secureStr.Length == 0))
                {
                    // don't append * when length is 0 and backspace is selected
                }
                else if (filtered.Count(x => chr == x) > 0)
                {
                    // don't append when a filtered char is detected
                }
                else
                {
                    // append and eventually write * mask
                    secureStr.AppendChar(chr);
                    if (useMask)
                    {
                        Console.Write('*');
                    }
                }
            }

            var ptr    = Marshal.SecureStringToBSTR(secureStr);
            var result = Marshal.PtrToStringBSTR(ptr);

            Marshal.ZeroFreeBSTR(ptr);
            return(result);
        }
Example #5
0
        public static System.Security.SecureString GetPasswordAsSecureString(string prompt, ConsoleColor?promptColor = null, ConsoleColor?promptBgColor = null)
        {
            var secureString = new System.Security.SecureString();

            foreach (var key in ReadObfuscatedLine(prompt, promptColor, promptBgColor))
            {
                switch (key)
                {
                case Backspace:
                    secureString.RemoveAt(secureString.Length - 1);
                    break;

                default:
                    secureString.AppendChar(key);
                    break;
                }
            }

            secureString.MakeReadOnly();
            return(secureString);
        }
Example #6
0
 /// <summary>
 /// Reads a password from the standard input stream without printing the password characters to the screen.
 /// </summary>
 /// <remarks>Pressing the backspace key does work while entering the password.</remarks>
 /// <returns>A <see cref="System.Security.SecureString"/> instance containing the password that has been entered into the standard input stream.</returns>
 public static System.Security.SecureString GetSecurePassword()
 {
     System.Security.SecureString pwd = new System.Security.SecureString();
     while (true)
     {
         ConsoleKeyInfo i = Console.ReadKey(true);
         if (i.Key == ConsoleKey.Enter)
         {
             break;
         }
         else if (i.Key == ConsoleKey.Backspace)
         {
             pwd.RemoveAt(pwd.Length - 1);
             //Console.Write("\b \b");
         }
         else
         {
             pwd.AppendChar(i.KeyChar);
             //Console.Write("*");
         }
     }
     return pwd;
 }
        /// <summary>
        ///  Asks the user for a password. This interrupts anything else.
        ///  Passwords are hidden using stars
        ///  We keep the password in a safe <see cref="System.Security.SecureString"/> so the full password never retained in memory.
        ///  (this works because all steps in adding to the <see cref="System.Security.SecureString"/> involve atomics that get flushed immediately)
        /// </summary>
        /// <param name="msg">Passwort prompt</param>
        public System.Security.SecureString PWPrompt(string msg)
        {
            PreparePrompt(msg);
            var result = new System.Security.SecureString();
            var typed  = 0;

            PasswordModeWrapper(msg, (typedPrompt) => {
                typed = -1;
                Console.WriteLine();
                queryModeActive = false;
            },
                                (k) => {
                result.InsertAt(typed, k);
                typed++;
            },
                                '*',
                                null,
                                (k) => {
                switch (k.Key)
                {
                case ConsoleKey.Backspace:
                    if (typed != 0)
                    {
                        result.RemoveAt(typed - 1);
                        typed--;
                    }
                    break;

                case ConsoleKey.Delete:
                    if (typed != result.Length && result.Length != 0)
                    {
                        result.RemoveAt(typed);
                    }
                    else
                    {
                        Beep();
                    }
                    break;

                case ConsoleKey.LeftArrow:
                    if (typed != 0)
                    {
                        typed--;
                    }
                    break;

                case ConsoleKey.RightArrow:
                    if (typed < result.Length)
                    {
                        typed++;
                    }
                    break;

                default:
                    break;
                }
            });
            PostPreparePrompt();
            while (typed != -1)
            {
                System.Threading.Tasks.Task.Delay(100).Wait();
            }
            result.MakeReadOnly();
            return(result);
        }