指定了无效的提供程序类型

时间:2018-06-30 02:43:22

标签: .net rsa digital-certificate

我有以下代码来生成电子文档(SUNAT)。我尝试使用变量“ certificate”的“ RSACryptoServiceProvider()”和“ X509Certificate2()”捕获到变量“ rsaKey”的.pfx密钥,该变量包含要加密和加密的数据(数字证书,密码)。签署xml。但是它不允许我这样做,并且向我显示错误“已指定了无效的提供程序类型”。和错误“ System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType,CspParameters参数,布尔randomKeyContainer,Int32 dwKeySize,SafeProvHandle& / p>

我读了一些可能用于计算机的设备,但是我已经消除了计算机中的证书,此外,操作系统是Windows 10,我使用.net 4.6.1。请您的帮助对我非常重要。

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 个答案:

没有答案