Descriptografar XML com certificado digital X509

0 respostas
programação
E

Bom dia!

Estou tentando criar um algoritmo em C# para criptografar e descriptografar um arquivo para trocarmos informações com nossos clientes.

Eu consegui fazer a parte de criptografia, segue código:

using System;

using <a href="http://System.IO">System.IO</a>;

using <a href="http://System.Net">System.Net</a>;

using System.Security.Cryptography;

using System.Security.Cryptography.X509Certificates;

using System.Security.Cryptography.Xml;

using System.Text;

using System.Xml;
namespace X509_Test

{

class Cripto

{

static void Main(string[] args)

{

try

{

XmlDocument xmlDoc = new XmlDocument();
using (WebClient client = new WebClient())
            {
                byte[] xmlBytes = client.DownloadData("After.xml");
                xmlDoc.LoadXml(Encoding.UTF8.GetString(xmlBytes));
            }

            string pfxPath = @"sistema-test-cert.pem";
            X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "[telefone removido]");

            if (cert == null)
                throw new CryptographicException("The X.509 certificate could not be found.");

            Encrypt(xmlDoc, "creditcard", cert);

            xmlDoc.Save("test.xml");

            Console.WriteLine("Encrypted XML:");
            Console.WriteLine();
            Console.WriteLine(xmlDoc.OuterXml);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, X509Certificate2 Cert)
    {
        if (Doc == null)
            throw new ArgumentNullException("Doc");
        if (ElementToEncrypt == null)
            throw new ArgumentNullException("ElementToEncrypt");
        if (Cert == null)
            throw new ArgumentNullException("Cert");

        XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

        if (elementToEncrypt == null)
            throw new XmlException("The specified element was not found");

        EncryptedXml eXml = new EncryptedXml();
        EncryptedData edElement = eXml.Encrypt(elementToEncrypt, Cert);
        EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
    }
}

}

Mas quando tento descriptografar, não estou conseguindo utilizar a chave privada. Segue código:

using System;

using <a href="http://System.IO">System.IO</a>;

using System.Security.Cryptography;

using System.Security.Cryptography.X509Certificates;

using System.Security.Cryptography.Xml;

using System.Xml;
namespace Descripto

{

class Program

{

static void Main(string[] args)

{

try

{

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
            xmlDoc.Load("test.xml");

            Decrypt(xmlDoc);

            xmlDoc.Save("test.xml");

            Console.WriteLine("Decrypted XML:");
            Console.WriteLine();
            Console.WriteLine(xmlDoc.OuterXml);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void Decrypt(XmlDocument Doc)
    {
        string pfxPath = @"sistema-test-cert.pfx";
        X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "[telefone removido]");

        EncryptedXml exml = new EncryptedXml(Doc);
        using (RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey)
        {
            exml.DecryptDocument();
        }
    }
}

}

Consigo capturar a chave, mas não consigo utiliza-lá.

Criado 8 de outubro de 2018
Respostas 0
Participantes 1