An invalid provider type has been specified

384 Views Asked by At

I have the following code to generate electronic documents (SUNAT). I have tried to capture the key of a .pfx with "RSACryptoServiceProvider ()" and "X509Certificate2 ()" of the variable "certificate" to the variable "rsaKey" that contains the data (The digital certificate, the password) to encrypt and sign an xml. But it does not allow me to do so and it shows me an error "An invalid provider type has been specified." and Error "System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) in System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)"

I read in some pots that could be for the computer, but I already eliminated the certificates that were in the computer, in addition the operating system is a windows 10 and I use the .net 4.6.1. Please your help is of great importance to me.

var response = new FirmadoResponse();

                var rsaKey = new RSACryptoServiceProvider();

                var certificate = new X509Certificate2();
                certificate.Import(Convert.FromBase64String(request.CertificadoDigital),
                    request.PasswordCertificado, X509KeyStorageFlags.MachineKeySet);

                //===>ERROR
                if (certificate.HasPrivateKey) {
                    rsaKey = (RSACryptoServiceProvider)certificate.PrivateKey;
                }
                else{
                    rsaKey = (RSACryptoServiceProvider)certificate.PublicKey.Key;
                }

            var xmlDoc = new XmlDocument();

            string resultado;

            var betterBytes = Encoding.Convert(Encoding.UTF8,
                Encoding.GetEncoding(Formatos.EncodingIso),
                Convert.FromBase64String(request.TramaXmlSinFirma));

            using (var documento = new MemoryStream(betterBytes))
            {
                xmlDoc.PreserveWhitespace = false;
                xmlDoc.Load(documento);

                var indiceNodo = request.UnSoloNodoExtension ? 0 : 1;

                var nodoExtension = xmlDoc.GetElementsByTagName("ExtensionContent", EspacioNombres.ext)
                    .Item(indiceNodo);
                if (nodoExtension == null)
                    throw new InvalidOperationException("No se pudo encontrar el nodo ExtensionContent en el XML");
                nodoExtension.RemoveAll();

                var signedXml = new SignedXml(xmlDoc) {SigningKey = rsaKey };
                var xmlSignature = signedXml.Signature;

                var env = new XmlDsigEnvelopedSignatureTransform();

                var reference = new Reference(string.Empty);
                reference.AddTransform(env);
                xmlSignature.SignedInfo.AddReference(reference);

                var keyInfo = new KeyInfo();
                var x509Data = new KeyInfoX509Data(certificate);

                x509Data.AddSubjectName(certificate.Subject);

                keyInfo.AddClause(x509Data);
                xmlSignature.KeyInfo = keyInfo;
                xmlSignature.Id = "SignOpenInvoicePeru";
                signedXml.ComputeSignature();

                if (reference.DigestValue != null)
                    response.ResumenFirma = Convert.ToBase64String(reference.DigestValue);
                response.ValorFirma = Convert.ToBase64String(signedXml.SignatureValue);

                nodoExtension.AppendChild(signedXml.GetXml());

                using (var memDoc = new MemoryStream())
                {

                    using (var writer = XmlWriter.Create(memDoc,
                        new XmlWriterSettings {Encoding = Encoding.GetEncoding(Formatos.EncodingIso)}))
                    {
                        xmlDoc.WriteTo(writer);
                    }

                    resultado = Convert.ToBase64String(memDoc.ToArray());

                }
            }
            response.TramaXmlFirmado = resultado;
0

There are 0 best solutions below