public void WriteDatabase() { FileStream fStream = null; CryptoStream cStream = null; StreamWriter writer = null; bool success = true; try { byte[] iv = new byte[16], passwordBytes, keyBytes; fStream = new FileStream(settings.DatabaseLocation, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); new RNGCryptoServiceProvider().GetBytes(iv); fStream.Write(iv, 0, iv.Length); //fStream.Flush(); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); passwordBytes = Encoding.UTF8.GetBytes(settings.Password); keyBytes = md5.ComputeHash(passwordBytes); RijndaelManaged rj = new RijndaelManaged(); rj.Mode = CipherMode.CBC; rj.IV = iv; rj.Key = keyBytes; rj.KeySize = 128; rj.BlockSize = 128; rj.Padding = PaddingMode.PKCS7; ICryptoTransform encryptor = rj.CreateEncryptor(); cStream = new CryptoStream(fStream, encryptor, CryptoStreamMode.Write); writer = new StreamWriter(cStream); writer.WriteLine("CheckString"); // a few loops which serialize some objects to the database as strings // using writer.WriteLine } catch(Exception e) { } finally { if(writer != null) { writer.Close(); writer.Dispose(); } if(cStream != null) { cStream.Close(); cStream.Dispose(); } if(fStream != null) { fStream.Close(); fStream.Dispose(); } }}