Question

Je fais la signature XML RSA-SHA256 à l'aide de la classe SNDXML.Mais le problème est que j'ai besoin de changer de csp pour soutenir SHA256.

Voici comment je sélectionne un certificat,

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

Voici comment j'essaie de changer de paramètre 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);

Problème est que j'utilise le jeton de périphérique USB et je doute que la clé privée n'est pas exportable.Sur l'exportation de son lancement d'une erreur ' clé non valide pour une utilisation dans l'état spécifié. '.

Quelqu'un peut-il aider à faire cela?

Était-ce utile?

La solution

Si quelqu'un intéressé ici est ma solution, j'ai fini par utiliser une autre nouvelle version de mon 3ème partie csp.La version csp que j'utilisais était une ancienne et je suis passée à une nouvelle version.Maintenant, la signature fonctionne.Merci pour votre aide.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top