예제 #1
0
        public T DeSerializeFromStream <T>(Stream stream, Boolean needDecrypt, SymmetricAlgorithm key = null)
        {
            if (!needDecrypt)
            {
                return(DeSerializeFromStream <T>(stream));
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException("UnReadable stream.");
            }

            using (var ms = new MemoryStream(BUFFER_SIZE))
            {
                var encryptText = String.Empty;
                using (var streamReader = new StreamReader(stream))
                {
                    encryptText = streamReader.ReadToEnd();
                }
                var plantText = XmlCryptography.DecryptXML(encryptText, key);
                var buffer    = Encoding.UTF8.GetBytes(plantText);
                ms.Write(buffer, 0, buffer.Length);
                ms.Seek(0, SeekOrigin.Begin);

                return((T)GetXmlSerializer <T>().Deserialize(ms));
            }
        }
예제 #2
0
        /// <summary>
        /// Des the serialize.
        /// </summary>
        /// <typeparam name="T">typeparam</typeparam>
        /// <param name="stream">The stream.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="password">The password.</param>
        /// <returns>returns</returns>
        /// <exception cref="ArgumentNullException"><paramref name="salt"/> is <see langword="null" />.see</exception>
        public T DeSerializeFromStream <T>(Stream stream, String salt, String password)
        {
            if (String.IsNullOrEmpty(salt))
            {
                throw new ArgumentNullException("salt");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            return(DeSerializeFromStream <T>(stream, XmlCryptography.GetCryptographyKey(salt, password)));
        }
예제 #3
0
        /// <summary>
        /// Des the serialize.
        /// </summary>
        /// <typeparam name="T">typeparam</typeparam>
        /// <param name="file">The file.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="password">The password.</param>
        /// <param name="bufferSize">Size of the buffer.</param>
        /// <returns>returns</returns>
        /// <exception cref="ArgumentNullException"><paramref name="salt"/> is <see langword="null" />.see</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="cb " />is out of range. This parameter requires a non-negative number.</exception>
        public T DeSerializeFromFile <T>(String file, String salt, String password, Int32 bufferSize = BUFFER_SIZE)
        {
            if (String.IsNullOrEmpty(salt))
            {
                throw new ArgumentNullException("salt");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            return(DeSerializeFromFile <T>(file, XmlCryptography.GetCryptographyKey(salt, password), bufferSize));
        }
예제 #4
0
        /// <summary>
        /// Serializes the specified obj.
        /// </summary>
        /// <typeparam name="T">typeparam</typeparam>
        /// <param name="obj">The obj.</param>
        /// <param name="stream">The stream.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="password">The password.</param>
        /// <exception cref="ArgumentNullException"><paramref name="salt"/> is <see langword="null" />.see</exception>
        public void SerializeToStream <T>(T obj, Stream stream, String salt, String password)
        {
            if (String.IsNullOrEmpty(salt))
            {
                throw new ArgumentNullException("salt");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            SerializeToStream(obj, stream, XmlCryptography.GetCryptographyKey(salt, password));
        }
예제 #5
0
        /// <summary>
        /// Serializes the specified obj.
        /// </summary>
        /// <typeparam name="T">typeparam</typeparam>
        /// <param name="obj">The obj.</param>
        /// <param name="file">The file.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="password">The password.</param>
        /// <param name="bufferSize">Size of the buffer.</param>
        /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null" />.see</exception>
        public void SerializeToFile <T>(T obj, String file, String salt, String password, Int32 bufferSize = BUFFER_SIZE)
        {
            if (String.IsNullOrEmpty(salt))
            {
                throw new ArgumentNullException("salt");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            SerializeToFile(obj, file, true, XmlCryptography.GetCryptographyKey(salt, password), bufferSize);
        }
예제 #6
0
        /// <summary>
        /// Serializes the specified obj.
        /// </summary>
        /// <typeparam name="T">typeparam</typeparam>
        /// <param name="obj">The obj.</param>
        /// <param name="stream">The stream.</param>
        /// <param name="needEncrypt">if set to <c>true</c> [need encrypt].c</param>
        /// <param name="key">The key.</param>
        /// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null" />.see</exception>
        /// <exception cref="ArgumentException">UnWritable stram.</exception>
        public void SerializeToStream <T>(T obj, Stream stream, Boolean needEncrypt, SymmetricAlgorithm key = null)
        {
            if (!needEncrypt)
            {
                SerializeToStream(obj, stream);
                return;
            }

            if (ReferenceEquals(obj, null))
            {
                throw new ArgumentNullException("obj");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanWrite)
            {
                throw new ArgumentException("UnWritable stram.");
            }

            using (var ms = new MemoryStream(BUFFER_SIZE))
            {
                var serializer = GetXmlSerializer <T>();
                serializer.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                var buffer = new Byte[Convert.ToInt32(ms.Length) + 1];
                ms.Read(buffer, 0, buffer.Length);
                var plantText   = Encoding.UTF8.GetString(buffer);
                var encryptText = XmlCryptography.EncryptXML(plantText, key);

                var encryptBuffer = Encoding.UTF8.GetBytes(encryptText);
                stream.Write(encryptBuffer, 0, encryptBuffer.Length);
            }
        }