473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Xml Serialization

Hi NG,

I got a problem while decrypting an encrypted a Serialized class:
I Serialize a simple class to a Stream then encrypt it and write it to file
everything seems to work here until i try to read the file and decrypt it.
When i read the file into a byte array everything seems to be correct but
when I get to decrypt the byte array i get just \0 for the length of the
byte array. The Problem occurs when the byte array is decrypted by the cryptostream
and written to the memorystream. In my opinion it seems the memorystream
doesn't like the " occuring in the xml, but i don't get any exceptions, any
ideas?
BTW all parameters for decrypting are the same as used for encryption.

regards
sascha
Oct 26 '06 #1
4 5655
How are you manipulating the file, text or binary?

If text are you converting it to a base64 text first?

Hope this helps

Ollie Riches

"Sascha Dietl" <gi****@web.dewrote in message
news:7c*************************@news.microsoft.co m...
Hi NG,

I got a problem while decrypting an encrypted a Serialized class:
I Serialize a simple class to a Stream then encrypt it and write it to
file everything seems to work here until i try to read the file and
decrypt it. When i read the file into a byte array everything seems to be
correct but when I get to decrypt the byte array i get just \0 for the
length of the byte array. The Problem occurs when the byte array is
decrypted by the cryptostream and written to the memorystream. In my
opinion it seems the memorystream doesn't like the " occuring in the xml,
but i don't get any exceptions, any ideas?
BTW all parameters for decrypting are the same as used for encryption.

regards
sascha


Oct 26 '06 #2
Sounds like it should work (although hard to tell without code).

This isn't as simple as forgetting to rewind a stream, is it?

Marc
Oct 26 '06 #3
Hello Marc,
Sounds like it should work (although hard to tell without code).

This isn't as simple as forgetting to rewind a stream, is it?
no it isn't. I use the binary from the serilized stream, if i write directly
to file first, then open it, encrypt and write to another it works really
fine, but that's not the preferred way..... here's some code:

using (MemoryStream stream = X0Serialization.XmlSerializer.Serialize(typeof(Pro bingPaths),
ProbingPaths)) {
FileCryptography.Encrypt(stream, Filename);
}

encrypt is just the function that does the symmetric cryptography I think
code for that is not needed as it only get's the bytes from the MemoryStream
and encrypt them.
Here's the Serialized class, really simple!

[Serializable(), MSSerialization.XmlRoot("Probing")]
public sealed class ProbingPaths {
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private List<stringpaths;

[MSSerialization.XmlElement(ElementName="PrivatePat h")]
public List<stringPaths {
get {
if (this.paths != null) {
return this.paths;
} else {
return Paths = new List<string>();
}
}

set {
paths = value;
}
}

public ProbingPaths() {
}
}

The serialization is done this way:
public static MemoryStream Serialize(Type type, object o) {
MemoryStream stream = new MemoryStream();

try {
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);
serializer.Serialize(stream, o);

return stream;
} catch {
throw;
}
}

so i think there are no possibilities to produce silly errors by code, or?

regards
sascha
Oct 26 '06 #4
Without saying "I told you so" [damn!] the stream hasn't been rewound...

I'm assuming that the Encrypt method Read()s from the stream to spits into
Filename; well, there isn't anything to read, as the Serialize method leaves
the memory stream at the end.

A short-term solution would be to set stream.Position = 0 before calling
Encrypt. Personally, I would be daisy-chaining the streams - i.e. I have a
file stream, and on top of that I sit an encryption stream. I then serialize
into the encryption stream, which scrambles the data, writing into the file
stream. And the reverse for reading. The advantage is that you don't have to
buffer everything into memory first.

At the end of the day, streams are "pipes", not "buckets"; they shouldn't
/hold/ data; they should /channel/ it. Most data-changing streams
(compression, encryption) are happy to be plumbed together to make
interesting combinations, e.g. a compression stream into an encryption
stream into a file stream.

Marc
Oct 26 '06 #5

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...

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.