博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
key container
阅读量:4140 次
发布时间:2019-05-25

本文共 3755 字,大约阅读时间需要 12 分钟。


The following example demonstrates how to create an asymmetric key, save it in a key container, retrieve the key at a later time, and delete the key from the container.

Notice that that code in the GenKey_SaveInContainer method and the GetKeyFromContainer method is similar. When you specify a key container name for a  object and pass it to an  object with the  property or property set to true, the following occurs. If a key container with the specified name does not exist, then one is created and the key is persisted. If a key container with the specified name does exist, then the key in the container is automatically loaded into the current  object. Therefore, the code in the GenKey_SaveInContainer method persists the key because it is run first, while the code in the GetKeyFromContainer method loads the key because it is run second.

using System;using System.IO;using System.Security.Cryptography;public class StoreKey{    public static void Main()    {        try        {            // Create a key and save it in a container.            GenKey_SaveInContainer("MyKeyContainer");                        // Retrieve the key from the container.            GetKeyFromContainer("MyKeyContainer");                // Delete the key from the container.            DeleteKeyFromContainer("MyKeyContainer");            // Create a key and save it in a container.            GenKey_SaveInContainer("MyKeyContainer");            // Delete the key from the container.            DeleteKeyFromContainer("MyKeyContainer");        }        catch(CryptographicException e)        {            Console.WriteLine(e.Message);        }    }    public static void GenKey_SaveInContainer(string ContainerName)    {        // Create the CspParameters object and set the key container         // name used to store the RSA key pair.        CspParameters cp = new CspParameters();        cp.KeyContainerName = ContainerName;        // Create a new instance of RSACryptoServiceProvider that accesses        // the key container MyKeyContainerName.        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);        // Display the key information to the console.        Console.WriteLine("Key added to container: \n  {0}", rsa.ToXmlString(true));    }    public static void GetKeyFromContainer(string ContainerName)    {        // Create the CspParameters object and set the key container         // name used to store the RSA key pair.        CspParameters cp = new CspParameters();        cp.KeyContainerName = ContainerName;        // Create a new instance of RSACryptoServiceProvider that accesses        // the key container MyKeyContainerName.        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);        // Display the key information to the console.        Console.WriteLine("Key retrieved from container : \n {0}", rsa.ToXmlString(true));    }    public static void DeleteKeyFromContainer(string ContainerName)    {        // Create the CspParameters object and set the key container         // name used to store the RSA key pair.        CspParameters cp = new CspParameters();        cp.KeyContainerName = ContainerName;        // Create a new instance of RSACryptoServiceProvider that accesses        // the key container.        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);        // Delete the key entry in the container.        rsa.PersistKeyInCsp = false;        // Call Clear to release resources and delete the key from the container.        rsa.Clear();        Console.WriteLine("Key deleted.");    }}
Key added to container:
Key Information A
Key retrieved from container :
Key Information A
Key deleted.Key added to container:
Key Information B
Key deleted.

转载地址:http://gbhvi.baihongyu.com/

你可能感兴趣的文章
本地tomcat 服务器内存不足
查看>>
IntelliJ IDAE 2018.2 汉化
查看>>
基于S5PV210的uboot移植中遇到的若干问题记录(一)DM9000网卡移植
查看>>
Openwrt源码下载与编译
查看>>
我和ip_conntrack不得不说的一些事
查看>>
Linux 查看端口使用情况
查看>>
文件隐藏
查看>>
两个linux内核rootkit--之二:adore-ng
查看>>
两个linux内核rootkit--之一:enyelkm
查看>>
关于linux栈的一个深层次的问题
查看>>
rootkit related
查看>>
关联容器map的下标操作(特殊)---map的本质就是关联数组, 数组的本质就是映射!
查看>>
我赞同Bob Quinn和Dave Shute的说法: WinSock中的SO_REUSEADDR就是个鸡肋, 最好不用它
查看>>
为什么TCP服务端需要调用bind函数而客户端通常不需要呢?
查看>>
什么是socket的name? 怎样给socket取一个name? --- 以生宝宝并取名的过程再谈socket、name、bind和socket name
查看>>
如何偷窥到socket对应的内核缓冲区中有什么数据? 有多少数据?---利用recv的MSG_PEEK和ioctlsocket的FIONREAD
查看>>
《Windows Sockets 网络编程》. Bob Quinn & Dave Shuttle (非常实用的Windows编程书籍)
查看>>
为什么有时ping不通www.baidu.com但可以访问www.baidu.com网页?
查看>>
从telnet www.baidu.com 80 聊聊我经历过的tcp“三次握手”失败---顺便验证telnet是基于tcp协议的
查看>>
C++智能指针auto_ptr源码完全解析---以微软auto_ptr为例来探讨auto_ptr的用法
查看>>