Using cryptographic streams with XML

T

theinsanecoder

Hi, i'm using the xmlserializer to load and unload data from my files
but i want to crypt them using a not so secure cryptographic encoder.
My code to encrypt "seems" to work fine, the file is outputted but when
i import the file again using the same concept (but in reversed form) i
get XMLDocument is invalid (1, 1) exception thrown back at me. Here is
the code :

SAVING PORTION

'Save the game
Dim oXS As XmlSerializer = New
XmlSerializer(GetType(Game.Game))
Dim oStmW As StreamWriter
Dim oXmlW As XmlTextWriter

'Setup the stream writer
oStmW = New StreamWriter(pFilename)

'Setup cryptographic stream cypher
Dim CryptoAlgo As New DESCryptoServiceProvider()
Dim Cryptographer As
System.Security.Cryptography.CryptoStream
CryptoAlgo.BlockSize = 64
CryptoAlgo.KeySize = 64
Cryptographer = New
System.Security.Cryptography.CryptoStream(oStmW.BaseStream,
CryptoAlgo.CreateEncryptor(KEY_8, IV_8), CryptoStreamMode.Write)

'Setup the text writer with cryptostream instead of
original stream (Crypto will link them)
oXmlW = New XmlTextWriter(Cryptographer, New
System.Text.UTF8Encoding())

'Execute the write
oXS.Serialize(oXmlW, Game)
oXmlW.Close()



LOADING PORTION

'Load the new game
Dim oXS As XmlSerializer = New
XmlSerializer(GetType(Game.Game))
Dim oStmR As StreamReader
Dim oXmlR As XmlTextReader

'Setup the stream reader
oStmR = New StreamReader(pFilename)

'Setup cryptographic stream cypher
Dim CryptoAlgo As New DESCryptoServiceProvider()
Dim Cryptographer As
System.Security.Cryptography.CryptoStream
CryptoAlgo.BlockSize = 64
CryptoAlgo.KeySize = 64
Cryptographer = New
System.Security.Cryptography.CryptoStream(oStmR.BaseStream,
CryptoAlgo.CreateEncryptor(KEY_8, IV_8), CryptoStreamMode.Read)

'Setup the text reader with cryptostream instead of
original stream (Crypto will link them)
oXmlR = New XmlTextReader(Cryptographer)

'Execute the read
oXmlR.WhitespaceHandling =
WhitespaceHandling.Significant
Game = oXS.Deserialize(oXmlR)
oXmlR.Close()

Like said earlier, SAVING seems to work fine and outputs a file that is
encrypted when i open it from notepad, but i may be wrong, maybe it's
just outputting rubish. The Load definitely doesn't work throwing back
an InvalidOperationException with the following content, "There is an
error in the XML document (1, 1)"

Can anyone help please?
 
T

tomb

Try not using the xmlwriter for the encrypted output. Just use the
xmlreader for the unencrypted input. Mind you, I'm no expert on xml nor
cryptography, but purely from a logical perspective, the . It's worth a
shot.

Tom
 
C

crazyone

Problem is if i don't use an XML reader or XML Writer i can't keep the
line feeds in my descriptive texts. I'd have to provide some complex
HTML content which would output <BR>s insteads of vbCrLfs... Unless you
klnow of a solution to bypass the line feeding problem i have to keep
the text reader/writer...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top