Thursday, July 5, 2012

Serialize Class Object using DES algorithm

There is a specific requirement I have in my present implementation. Need to maintain a data store in the application in which some of the data needed to be stored and fetched whenever application launched again. To achieve created class in Singleton and made it serialized. Some of the objects in the class are not serialized.
I will be posting the implementation of Singleton class object later. Following is the implementation where class object has been serialized and de-serialized. I also have a requirement to encrypt the data.

To achieve this used DESCryptoServiceProvider and CryptoStream.
"TestStoreObj" is the class object which is in Singleton and I am serializing it here.

Note :- You can use Triple DES for it but it will impact performance due to multiple iteration of encryption.


Referred following
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
IFormatter formatter = new BinaryFormatter();
Stream stream = null;

stream = new FileStream("MyConfig.txt", FileMode.Create, FileAccess.Write, FileShare.None);
using (CryptoStream cs = new CryptoStream(stream, new DESCryptoServiceProvider().CreateEncryptor(cryptic.Key, cryptic.IV), CryptoStreamMode.Write))
    formatter.Serialize(cs, TestStoreObj);

stream = new FileStream("MyConfig.txt", FileMode.Open, FileAccess.Read, FileShare.None);
using (CryptoStream cs = new CryptoStream(stream, new DESCryptoServiceProvider().CreateEncryptor(cryptic.Key, cryptic.IV), CryptoStreamMode.Read))
    TestStoreObj = (TestSingletonClass)formatter.Deserialize(cs);



  1. Thanks for the docs. While I realize this was written several years ago, I believe there's a bug in the Deserialize code. The code should be calling DESCryptoServiceProvider().CreateDecryptor not DESCryptoServiceProvider().CreateEncryptor.

  2. Thanks for sharing the information. Keep the good work going.Subway Surfers mod apk Mini Militia Mod

  3. If you are in Jaipur and need beautiful Jaipur Escorts or white skin Jaipur Escorts then you can simply book call girls in Jaipur from one of the best escort agency named Jaipur Escorts. It is No.1 premier escort agency and provide both incall or outcall facility to clients.Please click on the following link to check the official websites Jaipur Escorts