Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > .NET для новичков > Шифрование Dictionary


Автор: Unsane 8.4.2009, 22:55
Делаю настройки для программы. Необходимо зашифровать и расшифровать 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;

Ткните носом где неправильно. Заранее спасибо..

Автор: PashaPash 9.4.2009, 00:28
Unsane, давно не лез в System.Security.Cryptography, но в  Load у тебя расшифровка, вроде должен вызывать CreateDecryptor, а сейчас - CreateEncryptor.

Автор: Unsane 9.4.2009, 07:59
Оно работает..
 smile Позор на мою тупую голову..

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)