0

Basicly everything is great if I run it in my machine in localhost, but when I tried to use it from Azure, I got this error:

System.Security.Cryptography.CryptographicException: Invalid provider type specified.

The code where this happen is this one:

X509Certificate2 certificate = new X509Certificate2(pBytes, "1234");
if (!certificate.HasPrivateKey)
{
    //Here happens the Error
    RSACryptoServiceProvider key = (RSACryptoServiceProvider)certificate.PrivateKey; 
}

Any clue?

Btw, the project that Im using is https://github.com/ctt-gob-es/FirmaXadesNet.

And the entire code of that part is, maybe this will give a better background:

X509Certificate2 certificate = new X509Certificate2(pBytes, "1234");
var key = (RSACryptoServiceProvider)certificate.PrivateKey;

        if (key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_STRONG_PROV ||
            key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_ENHANCED_PROV ||
            key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_PROV ||
            key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_RSA_SCHANNEL_PROV)
        {
            Type CspKeyContainerInfo_Type = typeof(CspKeyContainerInfo);

            FieldInfo CspKeyContainerInfo_m_parameters = CspKeyContainerInfo_Type.GetField("m_parameters", BindingFlags.NonPublic | BindingFlags.Instance);
            CspParameters parameters = (CspParameters)CspKeyContainerInfo_m_parameters.GetValue(key.CspKeyContainerInfo);

            var cspparams = new CspParameters(CryptoConst.PROV_RSA_AES, CryptoConst.MS_ENH_RSA_AES_PROV, key.CspKeyContainerInfo.KeyContainerName);
            cspparams.KeyNumber = parameters.KeyNumber;
            cspparams.Flags = parameters.Flags;
            _signingKey = new RSACryptoServiceProvider(cspparams);

            _disposeCryptoProvider = true;
        }
        else
        {
            _signingKey = key;
            _disposeCryptoProvider = false;
        }
Arquitecto
  • 119
  • 2
  • 11
  • Are you sure your certificate is using RSA? Many now use other methods, like CNG. https://stackoverflow.com/questions/22581811/invalid-provider-type-specified-cryptographicexception-when-trying-to-load-pri This thread lists almost all possible causes. – Lex Li Oct 04 '18 at 04:09
  • 99% likely fix: `RSA key = cert.GetRSAPrivateKey();`. Don't talk about `RSACryptoServiceProvider` if you don't absolutely have to. – bartonjs Oct 04 '18 at 05:08
  • Im using the same certificate in both cases, localhost and azure server. So its looks like is the right one – Arquitecto Oct 05 '18 at 04:29

0 Answers0