Domanda

Sto facendo la firma XML RSA-SHA256 utilizzando la classe SigADXML.Ma il problema è che devo cambiare csp per supportare SHA256.

Ecco come selezionando il certificato,

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

È così che sto cercando di cambiare il parametro 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);
.

Problema è che sto usando il token del dispositivo USB e dubito che la chiave privata non sia esportabile.Sportando il suo lancio di un errore " Key non valido per l'uso nello stato specificato. '.

Qualcuno può aiutare come fare questo?

È stato utile?

Soluzione

Se qualcuno interessato qui è la mia soluzione, ho finito usando un'altra nuova versione della mia 3a partito csp.La versione CSP che stavo usando era antica e passiamo alla nuova versione.Ora la firma funziona.Grazie per il tuo aiuto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top