473,806 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Sep 24 '06 #1
2 2103
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

th************@ 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?
Sep 24 '06 #2
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...

Sep 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3616
by: ferran | last post by:
Hi, I'm trying to convert a string to a long double using streams but for some reasing seems to give the wrong values, the idea is to do a precise textual conversion without roundings or truncations, I though about using strtold but this function is not portable hence that is why I'm using streams, this is what I'm doing //------------------------------------------------------- std::stringstream oStream; std::string sInput =...
3
3499
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not, however, find any information on how this can be accomplished. What is involved in getting this...
6
4626
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to console as well as to file, my code is as below, ======================================================================= #include <iostream.h> #include<ostream> #include<sstream>
32
3927
by: David Garamond | last post by:
Are there any drawbacks of using BYTEA for PK compared to using a primitive/atomic data types like INT/SERIAL? (like significant performance hit, peculiar FK behaviour, etc). I plan to use BYTEA for GUID (of course, temporarily I hope, until PostgreSQL officially supports GUID data type), since it seems to be the most convenient+compact compared to other data types currently available. I use GUIDs for most PK columns. --
47
3546
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); It is meant to be called by C, but I would rather use it from C#. So I wrote the following C# program to test it, but it failed to work. The call to compress doesn't return or throw an exception, it simply...
2
3183
by: fineman | last post by:
Hi all, I want to get a 64bit(8 bytes) Encrypt result use DES class in the VS2005. Though I encrypt data is 64bit(8 bytes), but DES return encrypt result that always is 128bit(16 bytes), I don't know why? How to get a 64bit(8 bytes) encrypt result using DES class in the VS2005?
1
1622
by: =?Utf-8?B?c3VyZmtlaXRo?= | last post by:
A machine running XP Home fails to start the cryptographic service. When attempting to start from the services menu it responds with error 2: the file specified cannot be found. I am unable to identify which file it is referring to however I have run through various procedures from articles found in the discussion forums,a on the ms web site and including www.updatexp.com/cryptographic-services.html. Due to cryptographic services...
1
2813
by: lide.wyb | last post by:
We are using only the OpenSSL cryptographic functionality, the EVP and HMAC functions, in a multi-threaded application. Do we need to do anything to ensure thread safety ? The documentation mentions CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() but we are not calling these functions nor have we put critical sections in our own code before calling the cryptographic functions. We are experiencing some crashes and attempting to...
17
6733
by: almurph | last post by:
Hi, Hope you can help me with this one. I am trying to create random number between 0 and 1 inclusive of cryptographiuc quality. The problems is though - I don't know how! Here is what I have so far and I would greatly appreciate any comments/suggestions/code-samples that you may like to share. Thank you. Al
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.