C# - Serializing/Deserializing a DES encrypted file from a stream?

Encryption public static void EncryptAndSerialize(string filename, MyObject obj, SymmetricAlgorithm key) { using(FileStream fs = File. Open(filename, FileMode. Create)) { using(CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.

Write)) { XmlSerializer xmlser = new XmlSerializer(typeof(MyObject)); xmlser. Serialize(cs, obj); } } } Decryption: public static MyObject DecryptAndDeserialize(string filename, SymmetricAlgorithm key) { using(FileStream fs = File. Open(filename, FileMode.

Open)) { using(CryptoStream cs = new CryptoStream(fs, key.CreateDecryptor(), CryptoStreamMode. Read)) { XmlSerializer xmlser = new XmlSerializer(typeof(MyObject)); return (MyObject) xmlser. Deserialize(cs); } } } Usage: DESCryptoServiceProvider key = new DESCryptoServiceProvider(); MyObject obj = new MyObject(); EncryptAndSerialize("testfile.

Xml", obj, key); MyObject deobj = DecryptAndDeserialize("testfile. Xml", key); You need to change MyObject to whatever the type of your object is that you are serializing, but this is the general idea. The trick is to use the same SymmetricAlgorithm instance to encrypt and decrypt.

Looks like we posted about the same time, I'll accept as it's near enough what I actually wanted! Thanks Bryce. – GenericTypeTea Jun 8 '09 at 14:38 +1 Like your style.

– Artur Mustafin Jan 20 at 5:32 But how would I make key from a known string? Like "this_is_a_password_to_unlock_the_file"? This works in your "Usage" case, but if you want to save the key, and then use it to unlock the encrypted data, how would I do that?

:/ – Cipi Oct 18 at 11:27 Got it! To encrypt with a certain password: key. CreateEncryptor(Encoding.ASCII.

GetBytes("64bitPas"), Encoding.ASCII. GetBytes("InitVector")), to decrypt with the same password: key. CreateDecryptor(Encoding.ASCII.

GetBytes("64bitPas"), Encoding.ASCII. GetBytes("InitVector")) – Cipi Oct 18 at 11:41.

Here is an example of DES encryption/decription for a string.

Sorry, I need an example using XmlSerializer. I'll amend the main question. – GenericTypeTea Jun 8 '09 at 14:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions