コード例 #1
0
        public void EncryptToStream(IFile file, Stream outputStream)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (outputStream == null)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }

            byte[]           key       = _key.GenerateBlock(_encryptionProvider.BlockSize);
            byte[]           iv        = _iv.GenerateIV(_encryptionProvider.BlockSize);
            ICryptoTransform encryptor = _encryptionProvider.CreateEncryptor(key, iv);

            //Write IV in plain text to beginning of stream
            outputStream.Seek(0, SeekOrigin.Begin);
            outputStream.Write(iv, 0, iv.Length);

            //Write encrypted data
            using (Stream stream = file.Read())
            {
                var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write);
                stream.CopyTo(cryptoStream);
                cryptoStream.FlushFinalBlock();
            }
        }