Encrypt XML with TJMG public key

4

I need to encrypt this XML

<?xml version="1.0" encoding="utf-8"?><Selos>  <CodigoServentia>0000000099</CodigoServentia><Selo><CodigoSequencialUnico>ABC00005</CodigoSequencialUnico><CodigoDeSeguranca>0000000123456879</CodigoDeSeguranca><SeloConsulta></SeloConsulta><DataUtilizacao>  /  /     0:00:00</DataUtilizacao><Ato><CodigoFiscalDoAto>0</CodigoFiscalDoAto><AnoReferenciaTabela></AnoReferenciaTabela><DataPraticaAto>0</DataPraticaAto><ComposicaoAto><TipoTributacao>0</TipoTributacao><ValorEmolumento>0</ValorEmolumento><ValorTFJ>0</ValorTFJ><ValorFinalUsuario>0</ValorFinalUsuario><Protocolo>0</Protocolo><TipoLivro>301</TipoLivro><OrdemSequencial>0</OrdemSequencial><Folha>0</Folha><NumeroDeOrdem>0</NumeroDeOrdem></ComposicaoAto></Ato></Selo><Selo><CodigoSequencialUnico>ABC00006</CodigoSequencialUnico><CodigoDeSeguranca>0000000121545688</CodigoDeSeguranca><SeloConsulta></SeloConsulta><DataUtilizacao>  /  /     0:00:00</DataUtilizacao><Ato><CodigoFiscalDoAto>0</CodigoFiscalDoAto><AnoReferenciaTabela></AnoReferenciaTabela><DataPraticaAto>0</DataPraticaAto><ComposicaoAto><TipoTributacao>0</TipoTributacao><ValorEmolumento>0</ValorEmolumento><ValorTFJ>0</ValorTFJ><ValorFinalUsuario>0</ValorFinalUsuario><Protocolo>0</Protocolo><TipoLivro>301</TipoLivro><OrdemSequencial>0</OrdemSequencial><Folha>0</Folha><NumeroDeOrdem>0</NumeroDeOrdem></ComposicaoAto></Ato></Selo><Selo><CodigoSequencialUnico>ABC00007</CodigoSequencialUnico><CodigoDeSeguranca>0000000254326448</CodigoDeSeguranca><SeloConsulta></SeloConsulta><DataUtilizacao>  /  /     0:00:00</DataUtilizacao><Ato><CodigoFiscalDoAto>0</CodigoFiscalDoAto><AnoReferenciaTabela></AnoReferenciaTabela><DataPraticaAto>0</DataPraticaAto><ComposicaoAto><TipoTributacao>0</TipoTributacao><ValorEmolumento>0</ValorEmolumento><ValorTFJ>0</ValorTFJ><ValorFinalUsuario>0</ValorFinalUsuario><Protocolo>0</Protocolo><TipoLivro>301</TipoLivro><OrdemSequencial>0</OrdemSequencial><Folha>0</Folha><NumeroDeOrdem>0</NumeroDeOrdem></ComposicaoAto></Ato></Selo><Selo><CodigoSequencialUnico>ABC00008</CodigoSequencialUnico><CodigoDeSeguranca>0000000586786542</CodigoDeSeguranca><SeloConsulta></SeloConsulta><DataUtilizacao>  /  /     0:00:00</DataUtilizacao><Ato><CodigoFiscalDoAto>0</CodigoFiscalDoAto><AnoReferenciaTabela></AnoReferenciaTabela><DataPraticaAto>0</DataPraticaAto><ComposicaoAto><TipoTributacao>0</TipoTributacao><ValorEmolumento>0</ValorEmolumento><ValorTFJ>0</ValorTFJ><ValorFinalUsuario>0</ValorFinalUsuario><Protocolo>0</Protocolo><TipoLivro>301</TipoLivro><OrdemSequencial>0</OrdemSequencial><Folha>0</Folha><NumeroDeOrdem>0</NumeroDeOrdem></ComposicaoAto></Ato></Selo><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /><Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>XJWsbgzfmsU3jKBK3dIByMWGqfI=</DigestValue></Reference></SignedInfo><SignatureValue>DA1mqec3eS8SEtbj3T0h1w5XX7mXRvfNjvq1vkqHtyUWZAZ5KASPaaIp1XhRt471x8uCYsL2zI+b6HRAusRFC4h/JEJDTjfR2aqSUNxhTAmgXuOXHXfohCb7lgpyj5N/VFkJO64if5ro1LPN2Vx00rCo/Ql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ggrBgEFBQcwAoZTaHR0cDovL2ljcC1icmFzaWwuY2VydGlzaWduLmNvbS5ici9yZXBvc2l0b3Jpby9jZXJ0aWZpY2Fkb3MvQUNfQ2VydGlzaWduX1JGQl9HNC5wN2MwKAYIKwYBBQUHMAGGHGh0dHA6Ly9vY3NwLmNlcnRpc2lnbi5jb20uYnIwDQYJKoZIhvcNAQELBQADggIBAAaCUDNCXOzenD8/kSX1hPMsbjO0kvoBjE7wXVqzvwqu7iwoOn5kRlkNPZjmIR8HaJrcvQ/9k2DtM9FbJySiglz6ietsfl2oi8m2lv0RrWPM+RsmSixaF8M8hNSsV5dsHbrha4GZCJN2XWCQvEAQJt13XJBmMaufHZg/umYeVpBhdLvRboG+CxoQHBdNDi58nz+iMyIDtEt5BtOZif6045LN6GuRrIDj9Sm1ogqDDi621G9QsOOooIbz+b3lK4eBXawJDrvjlaIZDaOoKazzbhy2BpJf6q5txAyJauWxxPnb4AYtU1qJNUVSr+2k03F1qhIzPZJ8w0wnSavqbrsV/X8Uy6AWHFqliLdcBcpsQEibzbWO+GwcdtzcCGDL0kZELDKWekwKM7CerNWvSMsOC4uJnPfGuyCfd1P2oZu5+G5g0fee38CVPB0+YSJlE9Ers8UTStJu5t8mq+7NhUyd04C4g5yqTyUkRDv/SPKEN4DfEL1nQ/wPXLNFxCyJnrpCphFDDC8aTDmMPdireltbLLIf8bL9ujHYOgPWkZISn+vYwfXj6jHj4l9Iy59rT8MT9TUFHn9dM/HiIABh3XcoA9756dh3wxZaBewcextxv/3fsVygkL7531vGJijQsH2GS7oAuMexfMJMVOOjHDKukoHfo6qX0VRz9q6yNI/rYmN0</X509Certificate></X509Data></KeyInfo></Signature></Selos>

With this public key:

-----BEGIN CERTIFICATE----- 
MIIDczCCAlugAwIBAgIEZ2F1WDANBgkqhkiG9w0BAQsFADBqMQswCQYDVQQGEwJCUjEVMBMGA1UE
CBMMTWluYXMgR2VyYWlzMRcwFQYDVQQHEw5CZWxvIEhvcml6b250ZTENMAsGA1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k0=
-----END CERTIFICATE-----

How do I pass this key in encryption? (that's what I tried to do)

            string key = "MIIDczCCAlugAwIBAgIEZ2F1WDANBgkqhkiG9w0BAQsFADBqMQswCQYDVQQGEwJCUjEVMBMGA1UE" +
                        "CBMMTWluYXMgR2VyYWlzMRcwFQYDVQQHEw5CZWxvIEhvcml6b250ZTENMAsGA1UEChMEVEpNRzEN" +
                        "MAsGA1UECxMEVEpNRzENMAsGA1UEAxMEVEpNRzAeFw0xMzA3MDIxNjM5MDBaFw0xMzA5MzAxNjM5" +
                        "MDBaMGoxCzAJBgNVBAYTAkJSMRUwEwYDVQQIEwxNaW5hcyBHZXJhaXMxFzAVBgNVBAcTDkJlbG8g" +
                        "SG9yaXpvbnRlMQ0wCwYDVQQKEwRUSk1HMQ0wCwYDVQQLEwRUSk1HMQ0wCwYDVQQDEwRUSk1HMIIB" +
                        "IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApyliHR49lF/XcMCt0EjHcqzWwG0TjMzRX41O" +
                        "SmYDNx3yJOs4vnUKxVE4sLaeCePNbFpIo+PJadEoLvl3z2Z8kbLjbTA+72+d1uJRFG7jhczPtIzh" +
                        "kpeqTkwbwUn3pm3YeF8UslWe/EjpXxbi6BYCWkR+qf352QNX7kYLM6dtOv/bBOtmjh3ZibHbTnEh" +
                        "clMSUIBhLWfkaCn7PgMKZLszshz+g7436v4/7/hKEeMTLmlhmw7P06ahcaJsYEdCSVaD60hnl5W3" +
                        "4DtsHBv1sn4MrUjs5RS5lI/s6l8T8XQP70RG4WpKDHO2PbzbjPo9xvxm0C70ur/daBqejU8yd5dR" +
                        "IQIDAQABoyEwHzAdBgNVHQ4EFgQUVqwwZs4ff9j9qu1asmIoz1dh8RgwDQYJKoZIhvcNAQELBQAD" +
                        "ggEBADyO+v79KDRDqJFRyRkkOGNlM5iYiGVxMdh/GSsTZzqPCeciNlDAh5lzi1q4NIHjbzhTdPtR" +
                        "qgPz+LZFZkt7mlvhxHxmzEJC+SAuzL4PfZy/MqWAYu+fQ+SzzG/yLgVjgJgOzCD8Hlmt8FjLHXOr" +
                        "h71wNmSPc3DUEuE0fDK64sZjMB3cL9ja/RmKPUVglDo9CUFPHghTpKcwz/cTP1OTXBjT7dEFUK5A" +
                        "Tw889z1NVg3QMn4MKbcRtFWsMOb7vaEhjmLHoxp7/fsdGn2b+FSK3QqMjcE6HOh27v4AVXogS2Io" +
                        "WIKxMWI3LhAsGtOcGCynXLXsTvJriXYyO5IvlZvsYk0=";

            // Instancia a class EncryptedXml.
            EncryptedXml exml = new EncryptedXml(xmlDoc);
            XmlElement elementToEncrypt = xmlDoc.GetElementsByTagName("Selos")[0] as XmlElement;

            try
            {
                EncryptedData edElement = exml.Encrypt(elementToEncrypt, key);

                //////////////////////////////////////////////////// 
                // Replace the element from the original XmlDocument 
                // object with the EncryptedData element. 
                ////////////////////////////////////////////////////

                EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);

            }

Returns the following exception

Unable to retrieve encryption key.

    
asked by anonymous 05.05.2015 / 00:30

1 answer

1

I do not know if it's already been solved, but for all intents and purposes try this:

X509Certificate2 cert;
try
{
    //X509Store storeMy = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    //storeMy.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection certs = new X509Certificate2Collection(new X509Certificate2("tjmg.cer"));
    cert = certs[0];
    //storeMy.Close();
}
catch { return false; }

XmlElement elemento = xmlDoc.GetElementsByTagName("Selos")[0] as XmlElement;

if (elemento == null) { return false; }

EncryptedXml criptografado = new EncryptedXml();
EncryptedData data = criptografado.Encrypt(elemento, cert);

EncryptedXml.ReplaceElement(elemento, data, false);

xmlDoc.Save(this.saida);
return true;
    
15.01.2016 / 03:15