internal static object DeserializeFromFile(string fileName, string encryptionKey) { if (String.IsNullOrEmpty(encryptionKey)) { FileStream fs = System.IO.File.OpenRead(fileName); try { BinaryFormatter formatter = new BinaryFormatter(); return(formatter.Deserialize(fs)); } finally { fs.Close(); } } else { byte[] buffer = LoadFile(fileName); try { buffer = PublicEncryptor.DC(encryptionKey, buffer); } catch (System.Exception ex) { throw new System.Exception(String.Format("Cannot decrypte file:'{0}', error:{1}", fileName, ex.Message)); } MemoryStream ms = new MemoryStream(buffer); BinaryFormatter formatter = new BinaryFormatter(); return(formatter.Deserialize(ms)); } }
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); }
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 { } } }
public static T BytesToObject <T>(byte[] bytes, string encryptionKey) { bytes = PublicEncryptor.DC(encryptionKey, bytes); MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); ms.Write(bytes, 0, bytes.Length); ms.Seek(0, SeekOrigin.Begin); var obj = bf.Deserialize(ms); var retObj = (T)obj; return(retObj); }
internal static object Deserialize(byte [] buffer, string encryptionKey) { try { if (encryptionKey != null) { buffer = PublicEncryptor.DC(encryptionKey, buffer); } } catch (System.Exception ex) { throw new System.Exception(String.Format("Cannot decrypte bufferm error:{0}", ex.Message)); } MemoryStream ms = new MemoryStream(buffer); BinaryFormatter formatter = new BinaryFormatter(); return(formatter.Deserialize(ms)); }