简体   繁体   中英

Initialisting KeyInfo with X509 certificate parameters

This is a question for C# language.

I am having a reference to X509 Certificate in one class A( call it sslCert ) ie i can access the various members associated with the X509 certificate.

Also, i am having a Class B having following 2 crypto key members --> KeyInfo publicKey; KeyInfo privateKey;

The problem is that i am not able to find the way to set these 2 values (publickey & privatekey) with the Public Key and Private Key values from the X509. Direct assignment doesnt work and complains about data type mismatches.

B.publicKey = A.sslcertificate.Certificate.PublicKey;
B.privateKey = A.sslcertificate.Certificate.PrivateKey;

I have tried alot but not knowing what is the exact assignment way to achieve that. Anyone can throw some light on this?

Thanks !!!

Is it what you trying to do? I'm sorry, still trying to get the idea.

    using System.Net;
    using System.Security.Cryptography.Xml;
    using System.Security.Cryptography.X509Certificates;

    namespace ConsoleApplication1
    {
        public class A
        {
            private string website = "https://www.chase.com/";
            private X509Certificate m_certificate;

            public X509Certificate Certificate
            {
                get
                {
                    if (m_certificate == null)
                    {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(website);
                        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                        response.Close();
                        X509Certificate cert = request.ServicePoint.Certificate;
                        m_certificate = cert;
                    }
                    return m_certificate;
                }
            }
            public X509Certificate2 Certificate2
            {
                get
                {
                    return new X509Certificate2(Certificate);
                }
            }
        }

        public class B
        {
            public KeyInfo publicKey { get; set; }
            public KeyInfo privateKey { get; set; }
        }

        class Program
        {
            private static void Main(string[] args)
            {
                A tempA = new A();
                B tempB = new B();

                tempB.privateKey = tempA.Certificate.GetPublicKey(); // fails
            }
        }
    }

Assuming you have the cert:

X509Certificate2 certificate;

you just

KeyInfo ki = new KeyInfo();
KeyInfoX509Data keyInfoData = new KeyInfoX509Data( certificate );
ki.AddClause( keyInfoData );

This stores the whole certificate in the key info. Both private and public part can be stored separately, you just have to initialize the KeyInfo with other types of clauses. Complete list of supported clauses:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.xml.keyinfoclause.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM