Делаю настройки для программы. Необходимо зашифровать и расшифровать Dictionary<string, MyClass>. Полдня сижу и не могу свести дебет с кредитом(сериализовать - зашифровать - сохранить в файл - прочитать файл - расшифровать - десериализовать).
Код | #region ICD
int ICD.Load(string Path, string Password) { byte[] buffer = new byte[] { };
SymmetricAlgorithm sa = Rijndael.Create();
using (FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read)) { using (CryptoStream cs = new CryptoStream(fs, sa.CreateEncryptor((new PasswordDeriveBytes(Password, null)).GetBytes(32), new byte[16]), CryptoStreamMode.Read)) { buffer = new byte[fs.Length]; cs.Read(buffer, 0, buffer.Length); cs.FlushFinalBlock(); } }
using (MemoryStream ms = new MemoryStream(buffer)) { BinaryFormatter bf = new BinaryFormatter();
object locker = new object(); lock (locker) { this._database = (Dictionary<string, MyClass>)bf.Deserialize(ms); //System.Runtime.Serialization.SerializationException was unhandled. //Binary stream '163' does not contain a valid BinaryHeader. //Possible causes are invalid stream or object version change between serialization and deserialization.
} }
return 0; }
int ICD.Save(string Path, string Password) { object locker = new object(); lock (locker) { byte[] buffer = new byte[] { };
using (MemoryStream ms = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, this._database); buffer = ms.ToArray(); }
SymmetricAlgorithm sa = Rijndael.Create();
using (FileStream fs = new FileStream(Path, FileMode.Create, FileAccess.Write)) { using (CryptoStream cs = new CryptoStream(fs, sa.CreateEncryptor((new PasswordDeriveBytes(Password,null)).GetBytes(32), new byte[16]), CryptoStreamMode.Write)) { cs.Write(buffer, 0, buffer.Length); cs.FlushFinalBlock(); } } }
return 0; }
#endregion;
|
Ткните носом где неправильно. Заранее спасибо.. |