Using cryptographic streams with XML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • theinsanecoder@gmail.com

    #1

    Using cryptographic streams with XML

    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(G etType(Game.Gam e))
    Dim oStmW As StreamWriter
    Dim oXmlW As XmlTextWriter

    'Setup the stream writer
    oStmW = New StreamWriter(pF ilename)

    'Setup cryptographic stream cypher
    Dim CryptoAlgo As New DESCryptoServic eProvider()
    Dim Cryptographer As
    System.Security .Cryptography.C ryptoStream
    CryptoAlgo.Bloc kSize = 64
    CryptoAlgo.KeyS ize = 64
    Cryptographer = New
    System.Security .Cryptography.C ryptoStream(oSt mW.BaseStream,
    CryptoAlgo.Crea teEncryptor(KEY _8, IV_8), CryptoStreamMod e.Write)

    'Setup the text writer with cryptostream instead of
    original stream (Crypto will link them)
    oXmlW = New XmlTextWriter(C ryptographer, New
    System.Text.UTF 8Encoding())

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



    LOADING PORTION

    'Load the new game
    Dim oXS As XmlSerializer = New
    XmlSerializer(G etType(Game.Gam e))
    Dim oStmR As StreamReader
    Dim oXmlR As XmlTextReader

    'Setup the stream reader
    oStmR = New StreamReader(pF ilename)

    'Setup cryptographic stream cypher
    Dim CryptoAlgo As New DESCryptoServic eProvider()
    Dim Cryptographer As
    System.Security .Cryptography.C ryptoStream
    CryptoAlgo.Bloc kSize = 64
    CryptoAlgo.KeyS ize = 64
    Cryptographer = New
    System.Security .Cryptography.C ryptoStream(oSt mR.BaseStream,
    CryptoAlgo.Crea teEncryptor(KEY _8, IV_8), CryptoStreamMod e.Read)

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

    'Execute the read
    oXmlR.Whitespac eHandling =
    WhitespaceHandl ing.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 InvalidOperatio nException with the following content, "There is an
    error in the XML document (1, 1)"

    Can anyone help please?

  • tomb

    #2
    Re: Using cryptographic streams with XML

    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

    theinsanecoder@ gmail.com wrote:
    >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.Ga me))
    Dim oStmW As StreamWriter
    Dim oXmlW As XmlTextWriter
    >
    'Setup the stream writer
    oStmW = New StreamWriter(pF ilename)
    >
    'Setup cryptographic stream cypher
    Dim CryptoAlgo As New DESCryptoServic eProvider()
    Dim Cryptographer As
    >System.Securit y.Cryptography. CryptoStream
    CryptoAlgo.Bloc kSize = 64
    CryptoAlgo.KeyS ize = 64
    Cryptographer = New
    >System.Securit y.Cryptography. CryptoStream(oS tmW.BaseStream,
    >CryptoAlgo.Cre ateEncryptor(KE Y_8, IV_8), CryptoStreamMod e.Write)
    >
    'Setup the text writer with cryptostream instead of
    >original stream (Crypto will link them)
    oXmlW = New XmlTextWriter(C ryptographer, New
    >System.Text.UT F8Encoding())
    >
    'Execute the write
    oXS.Serialize(o XmlW, Game)
    oXmlW.Close()
    >
    >
    >
    >LOADING PORTION
    >
    'Load the new game
    Dim oXS As XmlSerializer = New
    >XmlSerializer( GetType(Game.Ga me))
    Dim oStmR As StreamReader
    Dim oXmlR As XmlTextReader
    >
    'Setup the stream reader
    oStmR = New StreamReader(pF ilename)
    >
    'Setup cryptographic stream cypher
    Dim CryptoAlgo As New DESCryptoServic eProvider()
    Dim Cryptographer As
    >System.Securit y.Cryptography. CryptoStream
    CryptoAlgo.Bloc kSize = 64
    CryptoAlgo.KeyS ize = 64
    Cryptographer = New
    >System.Securit y.Cryptography. CryptoStream(oS tmR.BaseStream,
    >CryptoAlgo.Cre ateEncryptor(KE Y_8, IV_8), CryptoStreamMod e.Read)
    >
    'Setup the text reader with cryptostream instead of
    >original stream (Crypto will link them)
    oXmlR = New XmlTextReader(C ryptographer)
    >
    'Execute the read
    oXmlR.Whitespac eHandling =
    >WhitespaceHand ling.Significan t
    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 InvalidOperatio nException with the following content, "There is an
    >error in the XML document (1, 1)"
    >
    >Can anyone help please?
    >
    >
    >

    Comment

    • crazyone

      #3
      Re: Using cryptographic streams with XML

      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...

      Comment

      Working...