C# 由于特殊字符,签名xml文档时出现编码问题

C# 由于特殊字符,签名xml文档时出现编码问题,c#,xml,encoding,digital-signature,C#,Xml,Encoding,Digital Signature,使用c#I对xml文档进行签名时,由于特殊字符的存在,会不断出现签名验证问题,具体来说,这一字符通常用于西班牙的地址 这个xml文件的编码是“ISO-8859-1”,但是如果我使用UTF-8,它就可以正常工作 我用于签名的方法如下: public static string SignXml(XmlDocument Document, X509Certificate2 cert) { SignedXml signedXml = new SignedXml(Docum

使用c#I对xml文档进行签名时,由于特殊字符的存在,会不断出现签名验证问题,具体来说,这一字符通常用于西班牙的地址

这个xml文件的编码是“ISO-8859-1”,但是如果我使用UTF-8,它就可以正常工作

我用于签名的方法如下:

    public static string SignXml(XmlDocument Document, X509Certificate2 cert)
    {
        SignedXml signedXml = new SignedXml(Document);
        signedXml.SigningKey = cert.PrivateKey;

        // Create a reference to be signed.
        Reference reference = new Reference();
        reference.Uri = "";

        // Add an enveloped transformation to the reference.            
        XmlDsigEnvelopedSignatureTransform env =
           new XmlDsigEnvelopedSignatureTransform(true);
        reference.AddTransform(env);

        XmlDsigC14NTransform c14t = new XmlDsigC14NTransform();
        reference.AddTransform(c14t);

        KeyInfo keyInfo = new KeyInfo();
        KeyInfoX509Data keyInfoData = new KeyInfoX509Data(cert);
        keyInfo.AddClause(keyInfoData);
        signedXml.KeyInfo = keyInfo;

        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        // Compute the signature.
        signedXml.ComputeSignature();

        // Get the XML representation of the signature and save 
        // it to an XmlElement object.
        XmlElement xmlDigitalSignature = signedXml.GetXml();

        Document.DocumentElement.AppendChild(
            Document.ImportNode(xmlDigitalSignature, true));

        return Document.OuterXml;
    }
摘自:

我这样称呼它:

    static void Main(string[] args)
    {
        string path = ".\\DATOS\\Ejemplo.xml";
        string signedDocString = null;
        XmlDocument entrada = new XmlDocument();
        entrada.Load(path);

        X509Certificate2 myCert = null;
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        var certificates = store.Certificates;
        foreach (var certificate in certificates)
        {
            if (certificate.Subject.Contains("XXXX"))
            {
                Console.WriteLine(certificate.Subject);
                myCert = certificate;
                break;
            }
        }

        if (myCert != null)
        {
            signedDocString = SignXml(entrada, myCert);
        }

        if (VerifyXml(signedDocString))
        {
            Console.WriteLine("VALIDO");               
        }

        else
        {
            Console.WriteLine("NO VALIDO");
        }
        Console.ReadLine();
    }
xml文档必须使用编码ISO-8859-1,这不是可选的。我无法抑制特殊字符

关于如何处理这件事有什么建议吗?

我的错

在验证方法中,我使用utf8:

public bool VerifyXml( string SignedXmlDocumentString )
{
    byte[] stringData = Encoding.UTF8.GetBytes( SignedXmlDocumentString );
    using ( MemoryStream ms = new MemoryStream( stringData ) )
        return VerifyXmlFromStream( ms );
}
在这一步中,我改变了编码,因此文档内容与原始内容不同。相当新的错误

解决方案:

        public static bool VerifyXml(XmlDocument SignedXmlDocument)
    {
        byte[] stringData = Encoding.Default.GetBytes(SignedXmlDocument.OuterXml);
        using (MemoryStream ms = new MemoryStream(stringData))
            return VerifyXmlFromStream(ms);
    }

XML文档是否正确地将其编码标识为ISO-8859-1?在验证之前它是否工作,只是没有正确验证?什么是
VerifyXml
方法?您可以尝试代替
entrada.Load(path)
do
entrada.Load(XmlReader.Create(新的StreamReader(路径,Encoding.GetEncoding(“Windows-1252”)))文档的标题如下所示。如果我将其修改为UTF-8,它就可以工作。VerifyXml是一种验证签名是否正确的方法。签名已生成,但当编码为ISO-8859-1时,验证返回“无效”。@rene我尝试了您的提示,甚至使用ISO-8859-1作为编码。在这两种情况下都没有成功。@MichaelKnight:当大多数已知的解析器只接受序言中提供的编码信息时,将XML编码为ISO-8859-1是一种强烈的要求。如果是这样的话,除了跳过任何特殊字符之外,我看不到其他解决方案。我以为你在某个地方使用了
编码
错误。。。虽然我没想到会是这样。很高兴你找到了!