簡體   English   中英

如何更改 RSACryptoServiceProvider 的 CSP 參數

[英]How to change CSP parameter of RSACryptoServiceProvider

我正在使用 SignedXML 類進行 rsa-sha256 xml 簽名。 但問題是我需要更改 CSP 以支持 sha256。

這就是我選擇證書的方式,

public X509Certificate2 GetCertificateFromStore()
        {
            X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            st.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection col = st.Certificates.Find(X509FindType.FindByTimeValid, (object)DateTime.Now, false);

            X509Certificate2 x509Certificate =null;
            X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, "Certificate", "Select single certificate to sign", X509SelectionFlag.SingleSelection);
            if (sel.Count > 0)
            {
                X509Certificate2Enumerator en = sel.GetEnumerator();
                en.MoveNext();
                x509Certificate = en.Current;
            }
            st.Close();
            //x509Certificate.s
            return x509Certificate;
        }

這就是我嘗試更改 CSP 參數的方式。

byte[] privateKeyBlob;
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa = cert.PrivateKey as RSACryptoServiceProvider;
            try
            {
                privateKeyBlob = rsa.ExportCspBlob(true);
            }
            catch
            {
                throw new ApplicationException("Private key fails to export");
            }
            // To use the RSA-SHA256 the CryptoAPI needs to select a special CSP: Microsoft Enhanced RSA and AES Cryptographic Provider
            // By reinstantiating a CSP of type 24 we ensure that we get the right CSP.
            CspParameters cp = new CspParameters(24);
            rsa = new RSACryptoServiceProvider(cp);
            rsa.ImportCspBlob(privateKeyBlob);


            signer.SigningKey = rsa;
            signer.KeyInfo = getKeyInfo(signer, cert);

問題是我正在使用 USB 設備令牌,我懷疑私鑰不可導出。 導出時會拋出錯誤 ' Key not valid for use in specified state. '.

任何人都可以幫助如何做到這一點?

如果有人對我的解決方案感興趣,我最終使用了我的第 3 方 CSP 的另一個新版本。 我使用的 CSP 版本是舊版本,我切換到新版本。 現在簽名正在工作。 謝謝你的幫助。

暫無
暫無

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

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