通常,可以通过以下代码来访问保存在 Windows 系统提供的密钥容器中的 RSA 密钥:
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = "MyKeyContainerName";
// Create a new instance of RSACryptoServiceProvider that
// accesses the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
以上代码中,如果在 Windows 系统中指定的命名密钥容器不存在,则会自动创建相应的密钥容器,同时将新 RSA 实例的密钥值储存到新创建的容器中。如果指定命名容器存在的话,则会自动取出容器中的 RSA 密钥,并创建相应的 RSACryptoServiceProvider。
但上述代码不能将一个已有的 RSA 密钥保存到指定命名的密钥容器中。我原来以为 Windows 系统不允许从外部保存密钥进容器,只能使用由系统自动生成的 RSA 密钥。
然而,.NET 强名称实用工具程序 SN.exe 却提供了将 RSA 密钥随意地存入密钥容器的命令项参数:
sn /i <infile> <container>
对应的导出操作为:
sn /pc <container> <outfile>
现在,我想用代码来自行实现 SN.exe 所提供的密钥保存到容器的功能,在此请教各位大侠。
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = "MyKeyContainerName";
// Create a new instance of RSACryptoServiceProvider that
// accesses the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
以上代码中,如果在 Windows 系统中指定的命名密钥容器不存在,则会自动创建相应的密钥容器,同时将新 RSA 实例的密钥值储存到新创建的容器中。如果指定命名容器存在的话,则会自动取出容器中的 RSA 密钥,并创建相应的 RSACryptoServiceProvider。
但上述代码不能将一个已有的 RSA 密钥保存到指定命名的密钥容器中。我原来以为 Windows 系统不允许从外部保存密钥进容器,只能使用由系统自动生成的 RSA 密钥。
然而,.NET 强名称实用工具程序 SN.exe 却提供了将 RSA 密钥随意地存入密钥容器的命令项参数:
sn /i <infile> <container>
对应的导出操作为:
sn /pc <container> <outfile>
现在,我想用代码来自行实现 SN.exe 所提供的密钥保存到容器的功能,在此请教各位大侠。
解决方案 »
- 网站配置了host,用域名可以访问,用ip访问的时候登录之后不跳转
- 什么时候用sealed?
- ----->>>【如何控制版本号】<<<-----
- 如何能让web打开一个网页不读body里的事件呢?
- 关于C#打包的问题
- DataRow数组取值问题
- C#如何调用ice中间件
- 有關于在VS2008上連接SQL Server 2000的調試問題
- 很多人说XML结合treeview可以提高性能,想了解一下在加载树,增删改节点时是怎么提高性能的,怎么保持XML与数据库的数据保持一致,逻辑是怎么
- 请教一个关于杀进程的问题?
- 如何将文件扩展名和系统图标关联在一起?
- 急求!我用什么办法才能知道对方给我发的公钥是可信的.
使用RSACryptoServiceProvider.ImportParameter和ExportParameter传递RSAParameters参数