6
In my project, I have a rule that I need to sign the string of a XML that I have you on Base64 with the user’s CPF...
I’ve been doing some research, but I couldn’t quite understand how this signature works string with own classes .NET.
Well, I found this link here who signs a string of a XML of NF-e. But I couldn’t quite understand how it works, nor how to adapt this code of this size to my scenario.
What I have so far: Gero o XML, I write in the file and I can generate the hash Base64 of that string, but what I need now is to sign this string with the CPF of the person.
The codes I have are:
//Cria o xml com as tags e faz o encoding para base64
var sb = new StringBuilder();
var settings = new XmlWriterSettings();
string cpf = "000.001.000-00";
using (var writer = XmlWriter.Create(sb, settings))
{
//Inicia o documetno xml
writer.WriteStartDocument();
//escreve o documento raiz
writer.WriteStartElement("no1");
//escreve os subelementos
writer.WriteElementString("no2", "valor");
//encerra o elemento raiz
writer.WriteEndElement();
//escreve o xml para o arquivo e encerra o objeto escritor
writer.Close();
}
//encoding do xml para base64
string s = EncodeTo64(sb.ToString());
//arquivo que vai ser usado para gerar a string base64
string caminho = parametros.Propriedades["ParPastaArquivoXML"].ToString();
caminho = caminho + "\\" + "arquivo.xml";
File.WriteAllText(caminho, s);
Here try to sign:
try
{
// Create a new CspParameters object to specify
// a key container.
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = cpf;
// Create a new RSA signing key and save it in the container.
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
// Create a new XML document.
XmlDocument xmlDoc = new XmlDocument();
// Load an XML file into the XmlDocument object.
xmlDoc.PreserveWhitespace = false;
xmlDoc.Load(caminho);
// Sign the XML document.
SignXml(xmlDoc, rsaKey);
Console.WriteLine("XML file signed.");
// Save the document.
xmlDoc.Save(caminhoAssinado);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Here the method to sign:
// Sign an XML file.
// This document cannot be verified unless the verifying
// code has the key with which it was signed.
public static void SignXml(XmlDocument xmlDoc, RSA Key)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (Key == null)
throw new ArgumentException("Key");
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// 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();
// Append the element to the XML document.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
The example of XML which is created follows below:
<?xml version="1.0" encoding="utf-16"?>
<no1>
<no2>021303</no2>
</no1>
Until that moment I can generate the hash Base64.
But now, how do I sign this hash with the CPF?
When you ask : "how do I sign this hash with the CPF," do you mean how to sign with a digital certificate from an individual? Type e-CPF A3 installed on user machine?
– Guilherme de Jesus Santos
That’s right @Guilhermejsantos. With this certificate of Individual. He’s already installed on the machine, but I can’t get him to sign it for the webservice... And when sending the webservice returns me the error that is not reading the CPF in the file.
– Érik Thiago
Erik, the example file seems to me correct, from the link you passed. Did you manage to run it? If so, did you get an error? For Nfe you need a specific legal entity certificate, see: http://www.nfe.fazenda.gov.br/portal/questionsFrequentes.aspx?tipConteudo=k/E5bakb80o=
– Celso Marigo Jr
As far as I know, for Nfe subscription you need a certificate for CNPJ (e-CNPJ) or Nfe (e-NFE).
– emanuelsn
I used the NF-e link as an example, but I’m not signing an NF-e, but an xml with a specific number for the webservice to reply to me with another xml populated with data so I can handle it on my internal system... I took the link more as example of same code.
– Érik Thiago