Ejemplo n.º 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);
            }
        }