Ejemplo n.º 1
0
        /*
         * Decrypt and deserialize function.
         */
        public static DataObjectSerializable decryptNdeserialize(string filename)
        {
            //Create a file stream to read the encrypted file back.
            FileStream fsread = new FileStream(filename,
                                               FileMode.Open,
                                               FileAccess.Read);

            //create des crypto service provider object.
            des         = new DESCryptoServiceProvider();
            des.Padding = PaddingMode.PKCS7;
            des.Mode    = CipherMode.CBC;
            des.Key     = Key;
            des.IV      = IV;

            //create Descryptor.
            desencrypt = des.CreateDecryptor();
            //Construct the cryptostream with filestream that we use to store data that we read from a file.
            CryptoStream cryptStream = new CryptoStream(fsread,
                                                        desencrypt,
                                                        CryptoStreamMode.Read);

            //create byte array object with length of our filestream.
            byte[] byteArray = new byte[fsread.Length];

            //store the byte in cryptStream to our byteArray.
            cryptStream.Read(byteArray, 0, byteArray.Length);

            //create a new memory stream.
            MemoryStream memoryStream = new MemoryStream();

            //write the byteArray data to memory stream.
            memoryStream.Write(byteArray, 0, byteArray.Length);

            //create formatter.
            IFormatter formatter = new BinaryFormatter();

            //reposition our memory stream to position 0.
            memoryStream.Seek(0, SeekOrigin.Begin);

            //assign the deserializated object to our data object.
            DataObjectSerializable dataObject = (DataObjectSerializable)formatter.Deserialize(memoryStream);

            // flush and close all stream.
            cryptStream.Flush();
            cryptStream.Close();
            fsread.Close();
            memoryStream.Flush();
            memoryStream.Close();

            return(dataObject);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //File path
            string filename = "D:\\SelfLearning\\c#\\Serialization\\file.dat";

            //Create data object.
            dataObject = new DataObjectSerializable();
            //Set the data object data.
            dataObject.Name = "http://ubuntuanakramli.blogspot.com/";
            dataObject.Age  = 2015 - 2011;

            //encrypt and serialize.
            encryptNserialize(filename);

            //decrpyt and deserialize , store result to dataObject.
            dataObject = decryptNdeserialize(filename);

            //print out the result.
            Console.WriteLine("Name" + dataObject.Name);
            Console.WriteLine("Age" + dataObject.Age);

            Console.ReadKey();
        }