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字符的非托管内存缓冲区
    }
    }
    }
  • 相关阅读:
    git 记录
    js 技巧
    首页三张幻灯片放什么
    6系统盈利方向
    WordPress获取某个分类关联的标签
    目标型长尾如何优化
    简介如何去除WordPress主题版权保护的方法
    2017.7.7 长尾关键词系统优化
    2017.7.6 目标关键词分析
    2017.7.2 seo知识总纲
  • 原文地址:https://www.cnblogs.com/smailxiaobai/p/2289963.html
Copyright © 2011-2022 走看看