使用 SN.exe -k制出snk文件
使用 sn -m n 切换到用户模式
使用sn -i *.snk MyContainer将密钥对导入名为“MyContainer”的密钥容器中使用程序:
CspParameters cp = new CspParameters();
cp.KeyContainerName = "MyContainer";RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);textBox1.text=rsa.ToXmlString(false);发现得出的xml格式公匙并不是snk中对应的验证过程如下:
记住xml格式公匙
使用sn -d MyContainer删除容器
再重新导入并重新运行程序,发现得出的xml格式公匙和上一次不一样发现容器名称是对应的,好像密钥不一样。如果没有使用SN创建密钥容器,运行这段程序可以自动创建同名的密钥容器,用SN可以删除程序创建的同名容器我的需要是,我想从snk形式文件导出xml形式的密钥密钥对已供程序使用,不知道怎样实现?

解决方案 »

  1.   

    哎,我弄的还不到你那水平呢
    看.NET本质论说得我晕
    搞了半天也就弄出个密匙
    怎么用它不会!
      

  2.   

    RSACryptoServiceProvider 可以自己创建KEYPAIR 啊 
    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.");
        }
    }
      

  3.   

    楼上只是MSDN上的吧?不管用呀