public override EncryptResult Encrypt(EncryptParameters parameters, CancellationToken cancellationToken)
        {
            Argument.AssertNotNull(parameters, nameof(parameters));

            ThrowIfTimeInvalid();

            EncryptionAlgorithm  algorithm = parameters.Algorithm;
            RSAEncryptionPadding padding   = algorithm.GetRsaEncryptionPadding();

            if (padding is null)
            {
                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Encrypt), algorithm);
                return(null);
            }

            byte[]        ciphertext = Encrypt(parameters.Plaintext, padding);
            EncryptResult result     = null;

            if (ciphertext != null)
            {
                result = new EncryptResult
                {
                    Algorithm  = algorithm,
                    Ciphertext = ciphertext,
                    KeyId      = KeyMaterial.Id,
                };
            }

            return(result);
        }
Exemplo n.º 2
0
        public override EncryptResult Encrypt(EncryptParameters parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(parameters, nameof(parameters));

            ThrowIfTimeInvalid();

            EncryptionAlgorithm algorithm = parameters.Algorithm;

            if (algorithm.GetAesCbcEncryptionAlgorithm() is AesCbc aesCbc)
            {
                // Make sure the IV is initialized.
                parameters.Initialize();

                using ICryptoTransform encryptor = aesCbc.CreateEncryptor(KeyMaterial.K, parameters.Iv);

                byte[] plaintext  = parameters.Plaintext;
                byte[] ciphertext = encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length);

                return(new EncryptResult
                {
                    Algorithm = algorithm,
                    KeyId = KeyMaterial.Id,
                    Ciphertext = ciphertext,
                    Iv = parameters.Iv,
                });
            }
            else
            {
                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Encrypt), algorithm);
                return(null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Encrypts plaintext.
        /// </summary>
        /// <param name="parameters">An <see cref="EncryptParameters"/> containing the data to encrypt and other parameters for algorithm-dependent encryption.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param>
        /// <returns>
        /// The result of the encrypt operation. The returned <see cref="EncryptResult"/> contains the encrypted data
        /// along with all other information needed to decrypt it. This information should be stored with the encrypted data.
        /// </returns>
        /// <exception cref="ArgumentException">The specified algorithm does not match the key corresponding to the key identifier.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="parameters"/> is null.</exception>
        /// <exception cref="CryptographicException">The local cryptographic provider threw an exception.</exception>
        /// <exception cref="InvalidOperationException">The key is invalid for the current operation.</exception>
        /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception>
        public virtual async Task <EncryptResult> EncryptAsync(EncryptParameters parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(parameters, nameof(parameters));

            EncryptResult result = null;

            if (_provider.SupportsOperation(KeyOperation.Encrypt))
            {
                result = await _provider.EncryptAsync(parameters, cancellationToken).ConfigureAwait(false);
            }

            return(result ?? throw LocalCryptographyProvider.CreateOperationNotSupported(nameof(KeyOperation.Encrypt)));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Encrypts plaintext.
        /// </summary>
        /// <param name="parameters">An <see cref="EncryptParameters"/> containing the data to encrypt and other parameters for algorithm-dependent encryption.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param>
        /// <returns>
        /// The result of the encrypt operation. The returned <see cref="EncryptResult"/> contains the encrypted data
        /// along with all other information needed to decrypt it. This information should be stored with the encrypted data.
        /// </returns>
        /// <exception cref="ArgumentException">The specified algorithm does not match the key corresponding to the key identifier.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="parameters"/> is null.</exception>
        /// <exception cref="CryptographicException">The local cryptographic provider threw an exception.</exception>
        /// <exception cref="InvalidOperationException">The key is invalid for the current operation.</exception>
        /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception>
        public virtual EncryptResult Encrypt(EncryptParameters parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(parameters, nameof(parameters));

            EncryptResult result = null;

            if (_provider.SupportsOperation(KeyOperation.Encrypt))
            {
                result = _provider.Encrypt(parameters, cancellationToken);
            }

            return(result ?? throw LocalCryptographyProvider.CreateOperationNotSupported(nameof(KeyOperation.Encrypt)));
        }
        public virtual async Task <Response <EncryptResult> > EncryptAsync(EncryptParameters parameters, CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Encrypt)}");
            scope.AddAttribute("key", _keyId);
            scope.Start();

            try
            {
                // Make sure the IV is initialized.
                // TODO: Remove this call once the service will initialized it: https://github.com/Azure/azure-sdk-for-net/issues/16175
                parameters.Initialize();

                return(await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new EncryptResult { Algorithm = parameters.Algorithm }, cancellationToken, "/encrypt").ConfigureAwait(false));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Exemplo n.º 6
0
        public virtual Task <EncryptResult> EncryptAsync(EncryptParameters parameters, CancellationToken cancellationToken = default)
        {
            EncryptResult result = Encrypt(parameters, cancellationToken);

            return(Task.FromResult(result));
        }
Exemplo n.º 7
0
 public virtual EncryptResult Encrypt(EncryptParameters parameters, CancellationToken cancellationToken = default)
 {
     throw CreateOperationNotSupported(nameof(Encrypt));
 }
 EncryptResult ICryptographyProvider.Encrypt(EncryptParameters parameters, CancellationToken cancellationToken)
 {
     return(Encrypt(parameters, cancellationToken));
 }
 async Task <EncryptResult> ICryptographyProvider.EncryptAsync(EncryptParameters parameters, CancellationToken cancellationToken)
 {
     return(await EncryptAsync(parameters, cancellationToken).ConfigureAwait(false));
 }