Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi,

I have looked everywhere for an answer to this. I am serializing and converting a custom class/object to a format that can be transferred to a web server. The web server encrypts the response and returns the encrypted serialized response to the application via the Net.

C#
Event ev = new Event();//Custom complex object that is serializable.
String EventToSend = Convert.ToBase64String(Serialize(ev));
EventToSend = Encryption.Encrypt(EventToSend);
Event ReceivedEvent = (Event)Encryption.Decrypt(EventToSend);
//Decrypt to Event object.



The server is emulated in the line of code:
EventToSend = Encryption.Encrypt(EventToSend);


Encryption.Decrypt() returns an object.

My issue is the cast from the object to my Event. How can I cast or convert the returned object to my Event?

I have tried "as" cast (along with the above) without any luck. I don't understand how I could cast from an object (that was a string) to my Event.

Thanks for any help.
Posted

1 solution

String EventToSend = Convert.ToBase64String(Serialize(ev));

If you are serializing it on the way out, then deserialize it on the way in...
Outgoing procedure (you are doing this already):
1) Serialize your Event object.
2) Encrypt the serialized data.
3) Send the encrypted data.

So the incoming procedure must be:
1) Receive data.
2) Decrypt data.
3) Deserialize to Event object.

There is an article that explains serialization here: Object Serialization using C#[^]

BTW: It might be worth changing the name of your complex object: event already exists, and having such a similar name for your objects is open to errors!
 
Share this answer
 
Comments
willworknow1 3-Apr-11 8:17am    
Thanks...you made me step back and have a look at what was REALLY going on. I had an extra Convert.ToBase64String in there when it should have been something more along the lines of:

System.Text.Encoding encoding=null;
encoding = new System.Text.UTF8Encoding();
String EventToSend = encoding.GetString(Serialize(ev));
Sergey Alexandrovich Kryukov 5-Apr-11 0:21am    
Sure, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900