コード例 #1
0
        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());
        }
コード例 #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="SymmetricEncryptStream" /> class.
        /// </summary>
        /// <param name="stream">
        ///     The inner <see cref="Stream" /> to read the clear-text message from.
        /// </param>
        /// <param name="settings">
        ///     The <see cref="SymmetricEncryptionSettings" /> specifying the cryptographic algorithm settings.
        /// </param>
        public SymmetricEncryptStream(Stream stream, SymmetricEncryptionSettings settings)
        {
            Check.NotNull(stream, nameof(stream));
            Check.NotNull(settings, nameof(settings));

            _cryptoTransform = CreateCryptoTransform(settings);
            _cryptoStream    = new CryptoStream(stream, _cryptoTransform, CryptoStreamMode.Read);
        }
コード例 #3
0
        private ICryptoTransform CreateCryptoTransform(SymmetricEncryptionSettings settings)
        {
            using var algorithm = SymmetricAlgorithmFactory.CreateSymmetricAlgorithm(settings);

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

            return(algorithm.CreateEncryptor());
        }
コード例 #4
0
        public static SymmetricAlgorithm CreateSymmetricAlgorithm(SymmetricEncryptionSettings encryptionSettings)
        {
            var algorithm = SymmetricAlgorithm.Create(encryptionSettings.AlgorithmName);

            if (encryptionSettings.BlockSize != null)
            {
                algorithm.BlockSize = encryptionSettings.BlockSize.Value;
            }

            if (encryptionSettings.FeedbackSize != null)
            {
                algorithm.FeedbackSize = encryptionSettings.FeedbackSize.Value;
            }

            if (encryptionSettings.BlockSize != null)
            {
                algorithm.BlockSize = encryptionSettings.BlockSize.Value;
            }

            if (encryptionSettings.InitializationVector != null)
            {
                algorithm.IV = encryptionSettings.InitializationVector;
            }

            algorithm.Key = encryptionSettings.Key;

            if (encryptionSettings.CipherMode != null)
            {
                algorithm.Mode = encryptionSettings.CipherMode.Value;
            }

            if (encryptionSettings.PaddingMode != null)
            {
                algorithm.Padding = encryptionSettings.PaddingMode.Value;
            }

            return(algorithm);
        }