This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Microsoft XML > August 2005 > C#: XmlTextReader with CryptoStream





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author C#: XmlTextReader with CryptoStream
pesso

2005-08-18, 7:50 pm

I have the following code that's taken and modified from a got_dot_net
example.

I'm trying to decrypt an XML file that's been encrypted. I can dump the
decrypted stream to the console, but if I try to load that into
XmlTextReader, it throws XmlException. I'd appreciate your help.

//create file stream to read encrypted file back
FileStream fsread = new FileStream(args[0], FileMode.Open,
FileAccess.Read);

//DES instance with random key
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

//create DES Decryptor from our des instance
ICryptoTransform desdecrypt =
des.CreateDecryptor(SymmetricKeyForDimeData.SecretKey,
SymmetricKeyForDimeData.InitializationVector);

//create crypto stream set to read and do a des decryption transform on
incoming bytes
CryptoStream cryptostream = new CryptoStream(fsread, desdecrypt,
CryptoStreamMode.Read);

StreamReader streamreader = new StreamReader(cryptostream, new
UTF8Encoding());

//Console.Write(streamreader.ReadToEnd()); // this works.

XmlTextReader xtr = new XmlTextReader(cryptostream);
xtr.WhitespaceHandling = WhitespaceHandling.None;
xtr.XmlResolver = null;

// this causes System.Xml.XmlException: The root element is missing.
while(xtr.Read()) {
Console.WriteLine("Element Name: {0}", xtr.Name);
}

Adam

2005-08-18, 7:50 pm

Have you tried:
string xmlInput = streamReader.ReadToEnd();

XmlTextReader xtr = new XmlTextReader(xmlInput);

Marvin Smit

2005-08-19, 7:23 am

Hi,

Have you tried "XmlDocument.loadXml( streamReader );" ?

Hope his helps,

Marvin Smit.

ps: How sure are you the stream you're getting is UTF-8 encoded?


On 18 Aug 2005 07:57:12 -0700, "pesso" <codesolutions@yahoo.com>
wrote:

>I have the following code that's taken and modified from a got_dot_net
>example.
>
>I'm trying to decrypt an XML file that's been encrypted. I can dump the
>decrypted stream to the console, but if I try to load that into
>XmlTextReader, it throws XmlException. I'd appreciate your help.
>
>//create file stream to read encrypted file back
>FileStream fsread = new FileStream(args[0], FileMode.Open,
>FileAccess.Read);
>
>//DES instance with random key
>DESCryptoServiceProvider des = new DESCryptoServiceProvider();
>
>//create DES Decryptor from our des instance
>ICryptoTransform desdecrypt =
>des.CreateDecryptor(SymmetricKeyForDimeData.SecretKey,
>SymmetricKeyForDimeData.InitializationVector);
>
>//create crypto stream set to read and do a des decryption transform on
>incoming bytes
>CryptoStream cryptostream = new CryptoStream(fsread, desdecrypt,
>CryptoStreamMode.Read);
>
>StreamReader streamreader = new StreamReader(cryptostream, new
>UTF8Encoding());
>
>//Console.Write(streamreader.ReadToEnd()); // this works.
>
>XmlTextReader xtr = new XmlTextReader(cryptostream);
>xtr.WhitespaceHandling = WhitespaceHandling.None;
>xtr.XmlResolver = null;
>
>// this causes System.Xml.XmlException: The root element is missing.
>while(xtr.Read()) {
> Console.WriteLine("Element Name: {0}", xtr.Name);
>}


pesso

2005-08-19, 7:34 pm

Yes. It didn't work.

Adam wrote:
> Have you tried:
> string xmlInput = streamReader.ReadToEnd();
>
> XmlTextReader xtr = new XmlTextReader(xmlInput);


pesso

2005-08-19, 7:34 pm

I tried, but it didn't work. I also tried the default encoding, but
that didn't work either. UTF-8 is what's in the XML declaration (<?xml
.... encoding="utf-8" ?> ).

I want to elaborate on the original posting. The line
"Console.WriteLine("Element Name: {0}", xtr.Name);" actually displays
the very first element of the encrypted XML document (which is "<?xml
version="1.0" encoding="utf-8"?>"). But, immediately after that, it
throws an exception "Unhandled Exception: System.Xml.XmlException: The
root element is missing."

Marvin Smit

2005-08-19, 7:34 pm

Hi,

Any chance you can "zip" the XML as-is and post it?

Hope this helps,

Marvin Smit.

ps: A string in .Net is widechar (very close to UTF-16/Unicode)

On 19 Aug 2005 08:00:02 -0700, "pesso" <codesolutions@yahoo.com>
wrote:

>I tried, but it didn't work. I also tried the default encoding, but
>that didn't work either. UTF-8 is what's in the XML declaration (<?xml
>... encoding="utf-8" ?> ).
>
>I want to elaborate on the original posting. The line
>"Console.WriteLine("Element Name: {0}", xtr.Name);" actually displays
>the very first element of the encrypted XML document (which is "<?xml
>version="1.0" encoding="utf-8"?>"). But, immediately after that, it
>throws an exception "Unhandled Exception: System.Xml.XmlException: The
>root element is missing."


Pesso

2005-08-20, 7:46 pm

I finally got it to work. Two things I did to make it work:
1. removed the encoding declaration from the Xml
<?xml version="1.0"?> instead of <?xml version="1.0" encoding="utf-8"?>
2. Instantiated XmlNamespaceManager and XmlParserContext classes and
passed them to XmlTextReader.

So here's a working version for anyone who might be interested in this
topic:

// Source to decrypte an encrypted XML file.
// Taken from got_dot_net sample and modified.
static void Main(string[] args)
{
if(1 != args.Length)
{
Usage();
return;
}

// Make sure input files exist
if(!File.Exists(args[0]))
{
Console.WriteLine("Could not find file: {0}", args[0]);
return;
}

//create file stream to read encrypted file
FileStream fsread = new FileStream(args[0], FileMode.Open,
FileAccess.Read);

//DES instance with random key
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

//create DES Decryptor from our des instance
ICryptoTransform desdecrypt = des.CreateDecryptor(SymmetricKey.SecretKey,
SymmetricKey.InitializationVector);

//create crypto stream set to read and do a des decryption transform on
incoming bytes
CryptoStream cryptostream = new CryptoStream(fsread, desdecrypt,
CryptoStreamMode.Read);

// Uncomment following two lines to dump the XML content to the console
//StreamReader streamreader = new StreamReader(cryptostream);
//Console.Write(streamreader.ReadToEnd());

//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("ns", "http://mine.com/mine.xsd");

//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr,
"ns:bookstore", null, null, null, "", "", XmlSpace.None);
XmlTextReader xtr = new XmlTextReader(cryptostream, XmlNodeType.Element,
context);
xtr.XmlResolver = null;

while(xtr.Read())
{
Console.WriteLine("Element Name: {0}", xtr.Name);
}
}

internal sealed class SymmetricKey
{
static byte[] _secretKey = {253, 95, 202, 140, 48, 132, 187, 171};
static byte[] _initVector = {0, 0, 0, 0, 0, 0, 0, 0};
private SymmetricKey(){}
static public byte[] SecretKey{ get{return _secretKey;} }
static public byte[] InitializationVector{get{return _initVector;}}
}


Sample XML file for testing.
bookstore.xml
<?xml version="1.0"?>
<bookstore xmlns="http://mine.com/mine.xsd">
<book style="autobiography">
<author>
....


"Marvin Smit" <marvin.smit@XXXXXXXXXX> wrote in message
news:em0cg11sr40htut2vdklelpgbinsq7ev7k@4ax.com...
> Hi,
>
> Any chance you can "zip" the XML as-is and post it?
>
> Hope this helps,
>
> Marvin Smit.
>
> ps: A string in .Net is widechar (very close to UTF-16/Unicode)
>
> On 19 Aug 2005 08:00:02 -0700, "pesso" <codesolutions@yahoo.com>
> wrote:
>
>



Marvin Smit

2005-08-29, 7:24 am

Cool!

Sorry for the late reply but i've been 'outa town' for a week and a
half.

Glad you got it fixed.

Marvin Smit

On Sat, 20 Aug 2005 14:27:29 -0700, "Pesso" <pesso@no.where> wrote:

>I finally got it to work. Two things I did to make it work:
>1. removed the encoding declaration from the Xml
><?xml version="1.0"?> instead of <?xml version="1.0" encoding="utf-8"?>
>2. Instantiated XmlNamespaceManager and XmlParserContext classes and
>passed them to XmlTextReader.
>
>So here's a working version for anyone who might be interested in this
>topic:
>
>// Source to decrypte an encrypted XML file.
>// Taken from got_dot_net sample and modified.
>static void Main(string[] args)
>{
> if(1 != args.Length)
> {
> Usage();
> return;
> }
>
> // Make sure input files exist
> if(!File.Exists(args[0]))
> {
> Console.WriteLine("Could not find file: {0}", args[0]);
> return;
> }
>
> //create file stream to read encrypted file
> FileStream fsread = new FileStream(args[0], FileMode.Open,
>FileAccess.Read);
>
> //DES instance with random key
> DESCryptoServiceProvider des = new DESCryptoServiceProvider();
>
> //create DES Decryptor from our des instance
> ICryptoTransform desdecrypt = des.CreateDecryptor(SymmetricKey.SecretKey,
>SymmetricKey.InitializationVector);
>
> //create crypto stream set to read and do a des decryption transform on
>incoming bytes
> CryptoStream cryptostream = new CryptoStream(fsread, desdecrypt,
>CryptoStreamMode.Read);
>
> // Uncomment following two lines to dump the XML content to the console
> //StreamReader streamreader = new StreamReader(cryptostream);
> //Console.Write(streamreader.ReadToEnd());
>
> //Create the XmlNamespaceManager.
> NameTable nt = new NameTable();
> XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
> nsmgr.AddNamespace("ns", "http://mine.com/mine.xsd");
>
> //Create the XmlParserContext.
> XmlParserContext context = new XmlParserContext(null, nsmgr,
>"ns:bookstore", null, null, null, "", "", XmlSpace.None);
> XmlTextReader xtr = new XmlTextReader(cryptostream, XmlNodeType.Element,
>context);
> xtr.XmlResolver = null;
>
> while(xtr.Read())
> {
> Console.WriteLine("Element Name: {0}", xtr.Name);
> }
>}
>
>internal sealed class SymmetricKey
>{
> static byte[] _secretKey = {253, 95, 202, 140, 48, 132, 187, 171};
> static byte[] _initVector = {0, 0, 0, 0, 0, 0, 0, 0};
> private SymmetricKey(){}
> static public byte[] SecretKey{ get{return _secretKey;} }
> static public byte[] InitializationVector{get{return _initVector;}}
>}
>
>
>Sample XML file for testing.
>bookstore.xml
><?xml version="1.0"?>
><bookstore xmlns="http://mine.com/mine.xsd">
> <book style="autobiography">
> <author>
>...
>
>
>"Marvin Smit" <marvin.smit@XXXXXXXXXX> wrote in message
>news:em0cg11sr40htut2vdklelpgbinsq7ev7k@4ax.com...
>


Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews