zoukankan      html  css  js  c++  java
  • SecureString

      class Program
    {
    static void Main()
    {
    SecureString s = new SecureString();
    StringBuilder sb = new StringBuilder();
    string sr = "";
    sr.ToCharArray();
    while (true)
    {
    ConsoleKeyInfo k = Console.ReadKey(true);
    if (k.Key == ConsoleKey.Enter) break;
    s.AppendChar(k.KeyChar);
    Console.Write("*");
    }
    //Console.Write(s.ToString());没有重写ToString方法,就是为了避免泄漏敏感数据。
    dispaly(s);
    //public static void Main()//MSDN的例子
    //{
    //// Instantiate the secure string.
    // SecureString securePwd = new SecureString();
    // ConsoleKeyInfo key;

    // Console.Write("Enter password: ");
    // do
    // {
    // key = Console.ReadKey(true);

    //// Ignore any key out of range.
    // if (((int)key.Key) >= 65 && ((int)key.Key <= 90))
    // {
    //// Append the character to the password.
    // securePwd.AppendChar(key.KeyChar);
    // Console.Write("*");
    // }
    //// Exit if Enter key is pressed.
    // } while (key.Key != ConsoleKey.Enter);
    // Console.WriteLine();

    // try
    // {
    // Process.Start("Notepad.exe", "Admin", securePwd, "MYDOMAIN");
    // }
    // catch (Win32Exception e)
    // {
    // Console.WriteLine(e.Message);
    // }
    Console.ReadKey();
    }


    unsafe static void dispaly(SecureString s)
    {
    char* c = null;
    try
    {
    c = (char*)System.Runtime.InteropServices.Marshal.SecureStringToBSTR(s);//解密到一个非托管的内存缓存区中
    for (int index = 0; c[index] != 0; index++)
    {
    Console.Write(c[index]);
    }
    }
    catch (Exception)
    {

    throw;
    }
    finally
    {
    if (c != null)
    System.Runtime.InteropServices.Marshal.ZeroFreeBSTR((IntPtr)c);//释放包含以解密s字符的非托管内存缓冲区
    }
    }
    }
  • 相关阅读:
    Sql2008 全文索引 简明教程
    sql server 全文检索 使用
    数据库分词查询的优缺点以及英文和中文各自的分词方法(一)
    win10中打开SQL Server配置管理器方法
    Asp.net 中高亮显示搜索关键字简单方法
    EntityFramework优缺点
    LoadXml载入Xhtml文件速度很慢
    c#无限循环线程如何正确退出
    线程的等待方法:join
    C#如何优雅的结束一个线程
  • 原文地址:https://www.cnblogs.com/smailxiaobai/p/2289963.html
Copyright © 2011-2022 走看看