Esempio n. 1
0
        /// <summary>
        /// Copies certain characteristics of this instance to the <paramref name="cipher"/> parameter.
        /// The goal is to produce a cipher with the same encryption/decryption behavior but saving the key encryption and decryption ceremony and overhead if possible.
        /// Here it creates a <see cref="SymmetricAlgorithm"/> object identical to the current <see cref="Symmetric"/> and assigns it to <paramref name="cipher"/>'s <see cref="Symmetric"/>.
        /// </summary>
        /// <param name="cipher">The cipher that gets the identical symmetric algorithm object.</param>
        protected virtual void CopyTo(
            SymmetricKeyCipherBase cipher)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (!IsSymmetricKeyInitialized)
            {
                return;
            }

            cipher._symmetric = SymmetricAlgorithm.Create(Symmetric.GetType().FullName);

            cipher.Symmetric.Mode         = Symmetric.Mode;
            cipher.Symmetric.Padding      = Symmetric.Padding;
            cipher.Symmetric.BlockSize    = Symmetric.BlockSize;
            cipher.Symmetric.FeedbackSize = Symmetric.FeedbackSize;
            cipher.Symmetric.KeySize      = Symmetric.KeySize;
            cipher.Symmetric.Key          = (byte[])Symmetric.Key.Clone();
            cipher.Symmetric.IV           = (byte[])Symmetric.IV.Clone();

            cipher.IsSymmetricKeyInitialized = true;
            cipher.ShouldEncryptIV           = ShouldEncryptIV;
        }
Esempio n. 2
0
        public virtual ICipherAsync CloneLightCipher()
        {
            if (ShouldEncryptIV)
            {
                throw new InvalidOperationException("This object cannot create light clones because the property " + nameof(ShouldEncryptIV) + " is true.");
            }

            InitializeSymmetricKey();

            var cipher = new EncryptedKeyCipher(Symmetric.GetType().FullName);

            CopyTo(cipher);
            cipher.KeyStorage = null;

            return(cipher);
        }
Esempio n. 3
0
        /// <summary>
        /// Copies certain characteristics of this instance to the <paramref name="cipher"/> parameter.
        /// The goal is to produce a cipher with the same encryption/decryption behavior but saving the key encryption and decryption ceremony and overhead if possible.
        /// Here it creates a <see cref="SymmetricAlgorithm"/> object identical to the current <see cref="Symmetric"/> and assigns it to <paramref name="cipher"/>'s <see cref="Symmetric"/>.
        /// </summary>
        /// <param name="cipher">The cipher that gets the identical symmetric algorithm object.</param>
        protected virtual void CopyTo(
            SymmetricKeyCipherBase cipher)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (!IsSymmetricKeyInitialized)
            {
                return;
            }

            cipher.Symmetric = SymmetricAlgorithm.Create(Symmetric.GetType().FullName);
            CopyToSymetricAlgorithm(cipher.Symmetric);
            cipher.IsSymmetricKeyInitialized = true;
            cipher.ShouldEncryptIV           = ShouldEncryptIV;
        }