繁体   English   中英

使用X509证书参数初始化KeyInfo

[英]Initialisting KeyInfo with X509 certificate parameters

这是C#语言的问题。

我在一个A类( 称为sslCert )中引用了X509证书,即我可以访问与X509证书关联的各种成员。

另外,我有一个B类,具有以下2个加密密钥成员-> KeyInfo publicKey; KeyInfo privateKey;

问题是我无法找到使用X509中的“公钥”和“私钥”值来设置这2个值(“公钥”和“私钥”)的方法。 直接分配无效,并抱怨数据类型不匹配。

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

我尝试了很多,但不知道实现该目标的确切分配方法是什么。 任何人都可以对此有所了解吗?

谢谢 !!!

这是你想做的吗? 很抱歉,仍在努力理解。

    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
            }
        }
    }

假设您拥有证书:

X509Certificate2 certificate;

你刚才

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

这会将整个证书存储在密钥信息中。 私有部分和公共部分都可以分别存储,您只需要用其他类型的子句初始化KeyInfo。 受支持条款的完整列表:

http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.xml.keyinfoclause.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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