正常的String类型值,在脱离开作用域之后,其值在内存中并不会被立即销毁,这时如果有人恶意扫描你的内存,程序中所保存的机密信息就会暴露;于是就有了System.Security.SecureString,SecureString表示一个应保密的文本,它在初始化时就已被加密,并且脱离作用域后会被立即销毁;

附一个小Demo:

class Program{
static void Main(){
System.Security.SecureString secureStr = new System.Security.SecureString();
secureStr.AppendChar('A');
string re = secureStr.ToString();//SecureString 中的值会被加密保存,不能直接获取,此时re=System.Security.SecureString
IntPtr inP = Marshal.SecureStringToBSTR(secureStr);//inP为secureStr的句柄
string ss = Marshal.PtrToStringBSTR(inP);//ss="A" Marshal.ZeroFreeBSTR(inP);//释放BSTR指针
string ss2 = Marshal.PtrToStringBSTR(inP);//ss2=""
}
}

SecureString用法Demo

(C#)System.Security.SecureString(表示应保密的文本)的更多相关文章

  1. 17 安全字符串 System.Security.SecureString

  2. 使用证书部署出现System.Security.Cryptography.CryptographicException 错误解决方案

    一.System.Security.Cryptography.CryptographicException: 找不到对象 at System.Security.Cryptography.Cryptog ...

  3. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  4. "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法

    .net web程序使用rsa算法进行加解密时,程序报告“System.Security.Cryptography.CryptographicException: 拒绝访问”错.按网上搜的解决方法做了 ...

  5. System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

    [15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...

  6. System.Security.SecurityException The source was not found, but some or all event logs could not be searched.Inaccessible logs Security.

    An exception occurred during the Install phase. System.Security.SecurityException The source was not ...

  7. System.Security.Cryptography.CryptographicException: 指定了无效的提供程序类型

    这两天在调用银联在线的支付接口,把银联提供的demo代码copy过来放到自己网站上,生成通过了,但是运行的时候就报错了: 指定了无效的提供程序类型. 说明: 执行当前 Web 请求期间,出现未经处理的 ...

  8. System.Security.Cryptography.CryptographicException 出现了内部错误

    调试微信支付退款时,需要使用pfx证书,在本地调试时没有问题,但在服务器部署时报异常:System.Security.Cryptography.CryptographicException 出现了内部 ...

  9. 部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法

    转载:http://www.cnblogs.com/jys509/p/4499978.html 在调用RSA加密的.pfx密钥时,在本地调试没有问题,可以布署到服务器,就会报以下的错误: 用户代码未处 ...

随机推荐

  1. Saga的实现模式——进化(Saga implementation patterns – variations)

    在之前的几个博客中,我主要讲了两个saga的实现模式: 基于command的控制者模式 基于事件的观察者模式 当然,这些都不是实现saga的唯一方式.我们甚至可以将这些结合起来. 发布者——收集者 回 ...

  2. [PATCH] ARM: add dtbImage.<dt> and dtbuImage.<dt> rules

    转载: http://permalink.gmane.org/gmane.linux.kbuild.devel/8755 This rules are useful for appended devi ...

  3. SVG 基础图形

    SVG 基础图形 SVG包含了以下的基础图形元素: 矩形(包括可选的圆角),使用<rect>元素创建 圆形,使用<circle>元素创建 椭圆形,使用<ellipse&g ...

  4. Android百度地图(二)结合方向传感器我们自己定位哪里走

    Android百度地图(二)结合方向传感器我们自己定位哪里走 本文代码在http://blog.csdn.net/xyzz609/article/details/51943556的基础上进一步改动.有 ...

  5. eclipse sun.net 下包无法导入问题

    项目中用到了:sun.net.ConnectionResetException.但是sun.net包里的类,在eclipse里默认是不让用的. 解决办法是自定义access rules 工程上右键-& ...

  6. 传统项目目录结构下maven+junit+junitReport

    <build> <defaultGoal>compile</defaultGoal> <sourceDirectory>${basedir}/src&l ...

  7. Selenium webdriver Java 查找元素

    1.简单查找 By ID: WebElement element=driver.findElement(By.id("userId")); By Name:WebElement e ...

  8. Server 非阻塞

    import socket import select import Queue port =500 host = "" sock = socket.socket(socket.A ...

  9. HTML 5 中WebStorage实现数据本地存储

    webstorage 分sessionStorage和localstorage,sessionStorage是暂时保存,localStorage是永久保存. sessionStorage假设浏览器关闭 ...

  10. 设置客户端连接PostgreSQL不需要密码

    平常工作中,有时需要远端连接 PostgreSQL 数据库做些维护,例如远端备份等:如果备份脚本写在远端机器,备份的时候会弹出密码输入提示,那么脚本就不能后台执行,这里总结了几种不弹出密码输入提示的方 ...