[英]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.