现在在研究数字签名技术,基于.net平台,对生成的*.cer文件,可以读出公钥的xml形式,可是无法获的私钥的xml形式?有那位高手知道,指教小弟一下?

解决方案 »

  1.   

    有一段参考代码发给大家也看一下.
    using System;
    using System.Security.Cryptography;
    using System.Security.Permissions;
    using System.IO;
    using System.Security.Cryptography.X509Certificates;
    public class CertInfo
    {
        //Reads a file.
        internal static byte[] ReadFile(string fileName)
        {
            FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            int size = (int)f.Length;
            byte[] data = new byte[size];
            size = f.Read(data, 0, size);
            f.Close();
            return data;
        }
        //Main method begins here.
        public static void Main(string[] args)
        {
            //Test for correct number of arguments.
            //if (args.Length < 1)
            //{
            //    Console.WriteLine("Usage: CertInfo <filename>");
            //    return;
            //}
            try
            {
                X509Certificate2 x509 = new X509Certificate2();
                //Create X509Certificate2 object from .cer file.
                byte[] rawData = ReadFile("c:\\textWHX.cer");
                x509.Import(rawData);            //Print to console information contained in the certificate
                Console.WriteLine("{0}证书名称Subject: {1}{0}", Environment.NewLine, x509.Subject);
                Console.WriteLine("{0}证书颁发机构的名称Issuer: {1}{0}", Environment.NewLine, x509.Issuer);
                Console.WriteLine("{0}版本Version: {1}{0}", Environment.NewLine, x509.Version);
                Console.WriteLine("{0}证书生效的本地时间中的日期Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore);
                Console.WriteLine("{0}该日期后证书不再有效Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter);
                Console.WriteLine("{0}证书的指纹Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint);
                Console.WriteLine("{0}证书的序列号Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber);
                Console.WriteLine("{0}公钥标识符友好名称Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName);
                Console.WriteLine("{0}公钥的ASN。1码Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(true));
                Console.WriteLine("{0}证书的原始数据的长度Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length);
                Console.WriteLine("{0}以文本格式显示X509证书Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(true));            Console.WriteLine("{0}公钥以XML形式表式Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(false));
                //Console.WriteLine("{0}私钥以XML形式表式Certificate to XML String: {1}{0}", Environment.NewLine, x509.PrivateKey.ToXmlString(false ));
                上面这注释的这一句老是出错?
                //Add the certificate to a X509Store.
                X509Store store = new X509Store();
                store.Open(OpenFlags.MaxAllowed);
                store.Add(x509);
                store.Close();
            }        catch (DirectoryNotFoundException)
            {
                Console.WriteLine("Error: The directory specified could not be found.");
            }
            catch (IOException)
            {
                Console.WriteLine("Error: A file in the directory could not be accessed.");
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.");
            }
        }}
      

  2.   


    标准可交换cer证书文件是不包含私钥的私人信息文件pfx里面才会包含有私钥
      

  3.   

    nod
    不然那还叫什么私钥。