Esempio n. 1
0
        // NOTE!! In future probably would be best to delete the "RawEncrypt / RawDecrypt"
        // methods and use these Stream based alogrithms instead.

        public static void Encrypt(this Stream readStream, Stream writeStreamEncryptDest,
                                   byte[] privateKey, bool useDeflateCompression)
        {
            if (readStream == null)
            {
                throw new ArgumentNullException(nameof(readStream));
            }
            if (writeStreamEncryptDest == null)
            {
                throw new ArgumentNullException(nameof(writeStreamEncryptDest));
            }
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }
            byte[] initVector = GetIVFromEncryptionKey(privateKey);
            if (initVector == null)
            {
                throw new ArgumentNullException(nameof(initVector));
            }

            CryptoStream  cryptS    = null;
            DeflateStream compressS = null;

            try {
                // what, no usings? no Dispose (in finally)?! See notes on Decrypt, STINKING CryptoStream disposes base stream
                SymmetricAlgorithm aesAlgorithm = Rijndael.Create();
                ICryptoTransform   aesEncryptor = aesAlgorithm.CreateEncryptor(privateKey, initVector);
                cryptS = new CryptoStream(writeStreamEncryptDest, aesEncryptor, CryptoStreamMode.Write);
                {
                    if (useDeflateCompression)
                    {
                        using (compressS = new DeflateStream(cryptS, CompressionMode.Compress, true))
                            XStream.Write(compressS, readStream);
                    }
                    else
                    {
                        XStream.Write(cryptS, readStream);
                    }
                }
            }
            finally {
                if (cryptS != null)
                {
                    cryptS.FlushFinalBlock();                     //
                }
            }
        }
Esempio n. 2
0
        public static void Decrypt(this Stream readStreamEncrypted, Stream writeStream,
                                   byte[] privateKey, bool useDeflateCompression)
        {
            if (readStreamEncrypted == null)
            {
                throw new ArgumentNullException(nameof(readStreamEncrypted));
            }
            if (writeStream == null)
            {
                throw new ArgumentNullException(nameof(writeStream));
            }
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }
            byte[] initVector = GetIVFromEncryptionKey(privateKey);
            if (initVector == null)
            {
                throw new ArgumentNullException(nameof(initVector));
            }
            try {
                SymmetricAlgorithm aesAlgorithm = Rijndael.Create();
                ICryptoTransform   aesDecryptor = aesAlgorithm.CreateDecryptor(privateKey, initVector);
                CryptoStream       cryptS;

                if (!useDeflateCompression)
                {
                    using (cryptS = new CryptoStream(writeStream, aesDecryptor, CryptoStreamMode.Write))
                        XStream.Write(cryptS, readStreamEncrypted);
                }
                else
                {
                    cryptS = new CryptoStream(readStreamEncrypted, aesDecryptor, CryptoStreamMode.Read);

                    using (DeflateStream deflS = new DeflateStream(cryptS, CompressionMode.Decompress, true))
                        XStream.Write(writeStream, deflS);
                }
            }
            finally {
                // do NOT call flush or flushxyz on CryptoStream, bec here, DeflStrm wraps it
                // and already calls it. CryptStrm is AMAZINGLY STUPIDLY designed, breaking ALL
                // contracts usually expected, like Flush should not throw errors. OH NO, IT DOES.
                // No wonder so many by pass .NET's version with couple open source options. TOTAL EMBARRASSMENT.
            }
        }
        // NEW, later, use these newer stream zip/unzip versions (perhaps)

        #region === Streams ===

        /// <summary>
        /// Compresses and writes the input stream to the output stream using DeflateStream.
        /// The input stream is only read from and is not altered as such, but rather, it is the
        /// output stream that is written to with a compressed version of the input
        /// stream.  The input stream will be read from its current position to its end.
        /// </summary>
        /// <param name="readStream">The input stream which must be readable.</param>
        /// <param name="writeStream">The output stream that will contain the
        /// compressed data of <i>inputStream</i>. This stream must be writable.</param>
        /// <param name="compressionType">The compression type: DeflateStream or GZip.</param>
        public static void Compress(this Stream readStream, Stream writeStream, CompressionType compressionType)
        {
            if (readStream == null)
            {
                throw new ArgumentNullException("readStream");
            }
            if (writeStream == null)
            {
                throw new ArgumentNullException("writeStream");
            }

            Stream deflS = null;

            try
            {
                // writeStream NOT be closed when its decorator stream (deflS) is Disposed with 'true'
                if (compressionType == CompressionType.DeflateStream)
                {
                    deflS = new DeflateStream(writeStream, CompressionMode.Compress, true);
                }
                else
                {
                    deflS = new GZipStream(writeStream, CompressionMode.Compress, true);
                }

                XStream.Write(deflS, readStream);
            }
            finally
            {
                if (deflS != null)
                {
                    deflS.Flush();
                    deflS.Dispose();                      // This flushes the stream!!  Flush() does NOT work.
                }
            }

            /* OLD
             * private static void __Zip(this Stream inputStream, Stream outputZippedStream, CompressionType compressionType)
             * {
             * if (inputStream == null) throw new ArgumentNullException("inputStream");
             * if (outputZippedStream == null) throw new ArgumentNullException("outputZippedStream");
             *
             * Stream deflS;
             *
             * // we set the output stream to NOT be closed when its decorator stream (deflS) is Disposed with 'true' param
             * // note that inputStream is never decorated, so no such action need be taken
             * if (compressionType == CompressionType.DeflateStream)
             *      deflS = new DeflateStream(outputZippedStream, CompressionMode.Compress, true);
             * else
             *      deflS = new GZipStream(outputZippedStream, CompressionMode.Compress, true);
             *
             * try
             * {
             *      int bufferLen = SetBufferSize(inputStream.Length);
             *      byte[] buffer = new byte[bufferLen];
             *      int bytesRead = 0;
             *
             *      do
             *      {
             *              bytesRead = inputStream.Read(buffer, 0, bufferLen);
             *              deflS.Write(buffer, 0, bytesRead);
             *      } while (bytesRead != 0);
             *
             *      deflS.Dispose();  // This flushes the stream!!  Flush() does NOT work.
             * }
             * finally { if (deflS != null) deflS.Dispose(); }
             * }
             */
        }
        /// <summary>
        /// Decompresses and writes the input stream to the output stream using DeflateStream.
        /// The input stream is only read from and is not altered as such, but rather, it is the
        /// output stream that is written to with a decompressed version of the input
        /// stream.  The input stream will be read from its current position to its end.
        /// </summary>
        /// <param name="readStream">The input stream which is expected to contain
        /// data that was compressed with DeflateStream or with a compatible compression type.
        /// This stream must be readable.</param>
        /// <param name="writeStream">The output stream that will contain the
        /// decompressed data of <i>inputStream</i>.  This stream must be writable.</param>
        /// <param name="compressionType">The compression type to use: DeflateStream or GZip.</param>
        public static void Decompress(this Stream readStream, Stream writeStream, CompressionType compressionType)
        {
            if (readStream == null)
            {
                throw new ArgumentNullException("readStream");
            }
            if (writeStream == null)
            {
                throw new ArgumentNullException("writeStream");
            }

            Stream deflS = null;

            try
            {
                if (compressionType == CompressionType.DeflateStream)
                {
                    deflS = new DeflateStream(readStream, CompressionMode.Decompress, true);
                }
                else
                {
                    deflS = new GZipStream(readStream, CompressionMode.Decompress, true);
                }

                XStream.Write(writeStream, deflS);
            }
            finally
            {
                if (deflS != null)
                {
                    deflS.Flush();
                    deflS.Dispose();                      // This flushes the stream!!  Flush() does NOT work.
                }
            }

            /*
             * private static void __Unzip(this Stream inputStream, Stream outputUnzippedStream, CompressionType compressionType)
             * {
             * if (inputStream == null) throw new ArgumentNullException("inputStream");
             * if (outputUnzippedStream == null) throw new ArgumentNullException("outputUnzippedStream");
             *
             * Stream deflS = null;
             *
             * if (compressionType == CompressionType.DeflateStream)
             *      deflS = new DeflateStream(inputStream, CompressionMode.Decompress, true);
             * else
             *      deflS = new GZipStream(inputStream, CompressionMode.Decompress, true);
             *
             * try
             * {
             *      int bufferSize = SetBufferSize(inputStream.Length);
             *      byte[] buffer = new byte[bufferSize];
             *      int bytesRead = 0;
             *
             *      do
             *      {
             *              bytesRead = deflS.Read(buffer, 0, bufferSize);
             *              outputUnzippedStream.Write(buffer, 0, bytesRead);
             *      } while (bytesRead != 0);
             *
             *      outputUnzippedStream.Flush();
             *      deflS.Dispose();
             * }
             * finally { if (deflS != null) deflS.Dispose(); }
             * }
             */
        }