예제 #1
0
        public static byte[] Serialize(object m, string encryptionKey)
        {
            var ms        = new MemoryStream();
            var formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(ms, m);
                ms.Close();
                byte[] buffer = ms.ToArray();

                if (encryptionKey != null)
                {
                    var buffer2 = PublicEncryptor.C(encryptionKey, buffer);
                    OverwriteBuffer(buffer);
                    return(buffer2);
                }
                else
                {
                    return(buffer);
                }
            }
            catch (System.Exception ex)
            {
                ErrorManager.Handle(ex);
            }
            finally
            {
            }
            return(null);
        }
예제 #2
0
 internal static void SerializeToFile(string fileName, object m, string encryptionKey)
 {
     if (String.IsNullOrEmpty(encryptionKey))
     {
         FileStream      fs        = System.IO.File.Create(fileName);
         BinaryFormatter formatter = new BinaryFormatter();
         try
         {
             formatter.Serialize(fs, m);
         }
         finally
         {
             fs.Close();
         }
     }
     else
     {
         MemoryStream    ms        = new MemoryStream();
         BinaryFormatter formatter = new BinaryFormatter();
         try
         {
             formatter.Serialize(ms, m);
             ms.Close();
             byte[] buffer = ms.ToArray();
             buffer = PublicEncryptor.C(encryptionKey, buffer);
             FileStream fs = System.IO.File.Create(fileName);
             fs.Write(buffer, 0, buffer.Length);
             fs.Close();
         }
         finally
         {
         }
     }
 }