Exemple #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;
        }
        /// <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.
        /// </summary>
        /// <param name="cipher">The cipher that gets the identical symmetric algorithm object.</param>
        protected override void CopyTo(
            SymmetricKeyCipherBase cipher)
        {
            base.CopyTo(cipher);

            if (cipher is ProtectedKeyCipher c)
            {
                c.Base64Encoded = Base64Encoded;
            }
        }
Exemple #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.
        /// </summary>
        /// <param name="cipher">The cipher that gets the identical symmetric algorithm object.</param>
        protected override void CopyTo(
            SymmetricKeyCipherBase cipher)
        {
            base.CopyTo(cipher);

            var c = cipher as ProtectedKeyCipher;

            if (c == null)
            {
                return;
            }

            c.Base64Encoded = Base64Encoded;
        }
        /// <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;
        }