Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        public static void DecryptFile(AesKey aesKey, string filePath, int bufferLength = BufferLength)
        {
            if (aesKey == null)
            {
                throw new ArgumentNullException(nameof(aesKey));
            }

            var key       = aesKey.GetKey();
            var iv        = aesKey.GetIv();
            var keyLength = aesKey.KeySize;

            if (!key.Any() || keyLength <= 0)
            {
                throw new KeyLengthException(Resources.key);
            }
            if (iv == null)
            {
                throw new ArgumentNullException(Resources.iv);
            }
            if (!iv.Any())
            {
                throw new ByteArrayLengthException(Resources.iv);
            }

            var readWriter = new SynchronizedReadWriter(filePath);

            using (var rif = new RijndaelManaged())
            {
                rif.Key       = key;
                rif.IV        = iv;
                rif.KeySize   = keyLength;
                rif.BlockSize = aesKey.BlockSize;
                rif.Mode      = aesKey.Mode;
                rif.Padding   = aesKey.Padding;

                using (var decryptor = rif.CreateDecryptor(key, iv))
                {
                    var bytes = new byte[bufferLength];
                    while (!readWriter.WriteComplete)
                    {
                        readWriter.Read();
                        using (var ms = new MemoryStream(readWriter.GetBuffer()))
                        {
                            if (ms.Length < bytes.Length)
                            {
                                bytes = new byte[ms.Length];
                            }

                            using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                            {
                                cs.Read(bytes, 0, bytes.Length);
                            }
                            readWriter.SetBuffer(bytes);
                            readWriter.Write();
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        public static void DecryptFile(AesKey aesKey, string inputPath, string outputPath, int bufferLength = BufferLength)
        {
            if (aesKey == null)
            {
                throw new ArgumentNullException(nameof(aesKey));
            }

            var key       = aesKey.GetKey();
            var iv        = aesKey.GetIv();
            var keyLength = aesKey.KeySize;

            if (!key.Any() || keyLength <= 0)
            {
                throw new KeyLengthException(Resources.key);
            }
            if (iv == null)
            {
                throw new ArgumentNullException(Resources.iv);
            }
            if (!iv.Any())
            {
                throw new ByteArrayLengthException(Resources.iv);
            }

            using (var rif = new RijndaelManaged())
            {
                rif.Key       = key;
                rif.IV        = iv;
                rif.KeySize   = keyLength;
                rif.BlockSize = aesKey.BlockSize;
                rif.Mode      = aesKey.Mode;
                rif.Padding   = aesKey.Padding;

                using (var decryptor = rif.CreateDecryptor(key, iv))
                {
                    using (var inStream = new FileStream(inputPath, FileMode.Open))
                    {
                        using (var csDecrypt = new CryptoStream(inStream, decryptor, CryptoStreamMode.Read))
                        {
                            using (var outFile = new FileStream(outputPath, FileMode.CreateNew))
                            {
                                var buffer = new byte[bufferLength];
                                int read;
                                while ((read = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    outFile.Write(buffer, 0, read);
                                }
                            }
                        }
                    }
                }
            }
        }