EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Signing TXT File

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#4399
Posted: 11/23/2007 11:25:57
by Nuno Guedes (Basic support level)
Joined: 08/13/2007
Posts: 87

Hi,

my application receive a TXT or a XML file and sign it.

Receiving and sign a xml file is implemented and dont denote any error.

Receiving and sign a txt file is implemented but when validation denote errors, i suppose that´s because the "newlines" in txt.
When receive a TXT file, i create a TElXMLDomDocument and the following structure:
Code
<root><ficheiroTXT>"data from inside the txt file"</ficheiroTXT></root>

here´s the code:
Code
f = new FileStream(lbLocalizacao.Text, FileMode.Open, FileAccess.Read);

StreamReader st = new StreamReader(f);

//Criar XMLDocument com a estrutura <root><FicheiroTXT>"dados"...
TElXMLDOMElement rootNode = XMLDocument.CreateElement("root");
XMLDocument.AppendChild(rootNode);
TElXMLDOMElement ficheiroNode = XMLDocument.CreateElement("ficheiroTXT");
rootNode.AppendChild(ficheiroNode);
TElXMLDOMText texto = XMLDocument.CreateTextNode(st.ReadToEnd());
ficheiroNode.AppendChild(texto);

XMLDocument.Version = "";


and then i do like signing a xml file.

When validating the xml file created, it denotes error on validating references. "bad digest or data". I supose that´s because the \n from TXT file.

What can i do for correct this?

Later, i will receive that xml file and should be able to create a TXT file equals the one that generate that xml (including the newlines).

thanks

p.s.:In attachment i send the TXT file and the xml generated.


[ Download ]
#4400
Posted: 11/23/2007 13:58:32
by Dmytro Bogatskyy (EldoS Corp.)

Quote
I supose that´s because the \n from TXT file.

Yes.
If you uncheck "normalize newline characters on load" in demo application, then the reference and signature will be validated ok.
The best solution, is to remove CR character from text ( st.ReadToEnd().Replace("\r\n", "\n") ), because older xml frameworks convert all linefeed to LF and then check a signature and another frameworks could keep an original linefeed. If the xml document will be used by your application only, then you can simply ignore normalization. Also, you can save xml document in canonical form, so CR character would be saved as & #x0D; entity.
#4406
Posted: 11/26/2007 11:12:06
by Nuno Guedes (Basic support level)
Joined: 08/13/2007
Posts: 87

hi,

i used "st.ReadToEnd().Replace("\­r\n", "\n")" and it works just fine.

Now i am trying to process XML or TXT on similar way.

TXT file
Code
PS21321321321312321
PS123123123213123
END


Generated XML file
Code
<root>
<data>
[COLOR=red]PS21321321321312321
PS123123123213123
END[/COLOR]
</data>
<Signature>
...


XML file
Code
<root>
<name>
XPTO
</name
<age>
34
</age>
</root>


Expected generated XML file
Code
<root>
<data>
[COLOR=red]<root>
<name>
XPTO
</name
<age>
34
</age>
</root>[/COLOR]
</data>
<signature>
...


The process for TXT file works fine but i have some difficulties processing XML file.

Code
//for read the XML File
TElXMLDOMDocument temp = new TElXMLDOMDocument();
temp.LoadFromStream(f, "", true);

//creating the wanted XML file
TElXMLDOMElement rootNode = XMLDocument.CreateElement("root");
XMLDocument.AppendChild(rootNode);
TElXMLDOMElement dataNode = XMLDocument.CreateElement("data");
rootNode.AppendChild(dataNode);

//[COLOR=red]Problem here[/COLOR] I tried various ways for append the xml original but without success
dataNode.AppendChild(temp.DocumentElement);

XMLDocument.Version = "";


How can i append the content of orginal XML file to data element?

thanks
#4407
Posted: 11/26/2007 13:24:08
by Dmytro Bogatskyy (EldoS Corp.)

Quote
//Problem here I tried various ways for append the xml original but without success
dataNode.AppendChild(temp.DocumentElement);

You cannot append node owned by another xml document. You should clone it first.
dataNode.AppendChild(temp.DocumentElement.CloneNode(true, XMLDocument));
#4408
Posted: 11/26/2007 13:32:33
by Nuno Guedes (Basic support level)
Joined: 08/13/2007
Posts: 87

hummm i tried that method but not on this form.

thanks
#4409
Posted: 11/26/2007 13:47:17
by Dmytro Bogatskyy (EldoS Corp.)

Quote
hummm i tried that method but not on this form.

Second parameter specify a xml document to which cloned node will belong to.
#4410
Posted: 11/27/2007 12:05:59
by Nuno Guedes (Basic support level)
Joined: 08/13/2007
Posts: 87

yes, in that way it works.

On this point i need to pass the content of XMLDocument to a byte[].

I am using XMLDocument.SaveToStream to storage the content to a temp file and then call the
Convert.ToBase64String(File.ReadAllBytes(path of temp file)).

It exists a way to pass directly from XMLDocument to a byte[]?

a tried XMLDocument.Rawdata but it returns a exception "failed to load..."

thanks
#4411
Posted: 11/27/2007 12:45:11
by Dmytro Bogatskyy (EldoS Corp.)

Quote
I am using XMLDocument.SaveToStream to storage the content to a temp file

Why not to use the MemoryStream? MemoryStream has ToArray method which returns byte[].
Also, you can use XMLDocument.OuterXML property, it returns "string".
Quote
a tried XMLDocument.Rawdata but it returns a exception "failed to load..."

This property helps to access raw data for a node when xml document was loaded from a stream using a delayed load mode.
#4412
Posted: 11/27/2007 12:57:46
by Nuno Guedes (Basic support level)
Joined: 08/13/2007
Posts: 87

humm i didnt remembered of memorystream. Passing the memstream to Convert.ToBase64String() it works as i wanted.

thanks
Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages

Reply

Statistics

Topic viewed 2581 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!