簡體   English   中英

C#使用X509Certificate2登錄Xml

[英]c# Sign Xml with X509Certificate2

我嘗試簽署Xml文件。 這是代碼(來自MSDN):

RSACryptoServiceProvider Key = new RSACryptoServiceProvider();
SignXmlFile(XmlStart, XmlEnd, Key);

如何發送X509Certificate2作為密鑰? 坦克! 弗朗切斯科

在這里,您將使用證書中的密鑰(公共/專用密鑰)。

選項1)

X509Certificate2 cert = RetrieveCertificate("abcd");
var key = cert.PrivateKey;

private static X509Certificate2 RetrieveCertificateFromStore(string certificateName)
{
    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    store.Open(OpenFlags.OpenExistingOnly);
    var cert = store.Certificates.OfType<X509Certificate2>().AsEnumerable().FirstOrDefault(c => c.FriendlyName == certificateName);
    return cert;
}

或從文件中檢索證書:

    private static X509Certificate2 RetrieveCertificateFromFile(string certPath)
    {
        // string certPath = @"C:\Certificates\myCert.pfx";
        string certPass = "mycertPass";
        // Create a collection object and populate it using the PFX file
        X509Certificate2Collection collection = new X509Certificate2Collection();
        collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);
        // Instead of foreach you can directly retrieve the certificate from collection as well.
        foreach (X509Certificate2 cert in collection)
        {
            // Import the certificates into X509Store objects
            return cert;
        }
        return null;
    }

選項2)

RSACryptoServiceProvider key = RetrieveKey(cert, EnumKeyType.Private);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM