예제 #1
0
        /// <summary>
        /// Serialize and encrypt a class as binary stream and save to a file
        /// </summary>
        /// <param name="filename">name of the file</param>
        /// <param name="obj">the instance to be serialized and encrypted</param>
        public static void BinarySerialize(string filename, object obj)
        {
            IFormatter formatter = new BinaryFormatter();

            using (MemoryStream mstream = new MemoryStream())
            {
                formatter.Serialize(mstream, obj);
                byte[] unencryptBytes = mstream.ToArray();
                mstream.Close();
                mstream.Dispose();
                string encryptedText = CryptographUtil.Encrypt(CharacterEncodingUtil.Bytes2FormattedString(unencryptBytes), "TAiCHi");
                IOUtil.StringWrite(filename, encryptedText, false, false, Encoding.UTF8);
            }
        }
예제 #2
0
        /// <summary>
        /// Decrypt and deserialize a string to a class
        /// </summary>
        /// <param name="filename">name of the file</param>
        /// <returns>the instance of the class</returns>
        public static object BinaryDerialize(string filename)
        {
            IFormatter formatter     = new BinaryFormatter();
            object     obj           = null;
            string     encryptedText = IOUtil.StringRead(filename, false, Encoding.UTF8, false);

            byte[] unencryptBytes = CharacterEncodingUtil.FormattedString2Bytes(CryptographUtil.Decrypt(encryptedText, "TAiCHi"));
            using (MemoryStream ms = new MemoryStream(unencryptBytes))
            {
                obj = formatter.Deserialize(ms);
                ms.Close();
                ms.Dispose();
            }
            return(obj);
        }