private static ICryptoTransform CreateCryptoTransform(SymmetricEncryptionSettings settings, Stream stream)
        {
            Check.NotNull(settings, nameof(settings));
            Check.NotNull(stream, nameof(stream));

            using var algorithm = SymmetricAlgorithmFactory.CreateSymmetricAlgorithm(settings);

            if (settings.InitializationVector == null)
            {
                // Read the IV prepended to the message
                byte[] buffer = new byte[algorithm.IV.Length];

                int totalReadCount = 0;
                while (totalReadCount < algorithm.IV.Length)
                {
                    int readCount = stream.Read(buffer, totalReadCount, algorithm.IV.Length - totalReadCount);

                    if (readCount == 0)
                    {
                        break;
                    }

                    totalReadCount = totalReadCount + readCount;
                }

                algorithm.IV = buffer;
            }

            return(algorithm.CreateDecryptor());
        }
        private ICryptoTransform CreateCryptoTransform(SymmetricEncryptionSettings settings)
        {
            using var algorithm = SymmetricAlgorithmFactory.CreateSymmetricAlgorithm(settings);

            if (settings.InitializationVector == null)
            {
                _prefixBuffer = algorithm.IV;
            }

            return(algorithm.CreateEncryptor());
        }
Пример #3
0
        private static ICryptoTransform CreateCryptoTransform(
            SymmetricDecryptionSettings settings,
            Stream stream,
            string?keyIdentifier)
        {
            Check.NotNull(settings, nameof(settings));
            Check.NotNull(stream, nameof(stream));

            byte[]? encryptionKey = settings.KeyProvider != null
                ? settings.KeyProvider(keyIdentifier)
                : settings.Key;

            using var algorithm =
                      SymmetricAlgorithmFactory.CreateSymmetricAlgorithm(settings, encryptionKey);

            if (settings.InitializationVector != null)
            {
                return(algorithm.CreateDecryptor());
            }

            // Read the IV prepended to the message
            byte[] buffer = new byte[algorithm.IV.Length];

            var totalReadCount = 0;

            while (totalReadCount < algorithm.IV.Length)
            {
                int readCount = stream.Read(buffer, totalReadCount, algorithm.IV.Length - totalReadCount);

                if (readCount == 0)
                {
                    break;
                }

                totalReadCount += readCount;
            }

            algorithm.IV = buffer;

            return(algorithm.CreateDecryptor());
        }