很高兴来到博客园,希望能与大家分享自己的经验,也希望大家能给予我帮助,谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;

namespace StringForm
{
    public sealed class Programe2
    {
        static int x = 1;
        public static void Main(string[] args)
        {
            using (SecureString ss = new SecureString())
            {
                Console.Write("Please enter password:");
                while (true)
                {
                    ConsoleKeyInfo cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.Enter)
                        break;
                    ss.AppendChar(cki.KeyChar); Console.Write("*");
                }
                Console.WriteLine();
                DisplaySecureString(ss);
                Programe2.x = 2;
                int x = 3;
            }
            Console.ReadKey();
        }
        private unsafe static void DisplaySecureString(SecureString ss)
        {
            Char* pc = null;
            try
            {
                pc = (Char*)Marshal.SecureStringToCoTaskMemUnicode(ss);
                for (Int32 index = 0; pc[index] != 0; index++)
                    Console.Write(pc[index]);
            }
            finally
            {
                if (pc != null)
                    Marshal.ZeroFreeCoTaskMemUnicode((IntPtr)pc);
            }
        }
    }
}