Esempio n. 1
0
        public async Task <Tuple <byte[], string> > WrapKeyAsync(byte[] key, string algorithm = RsaOaep.AlgorithmName, CancellationToken token = default(CancellationToken))
        {
            if (_csp == null)
            {
                throw new ObjectDisposedException(string.Format("RsaKey {0} is disposed", Kid));
            }

            if (string.IsNullOrWhiteSpace(algorithm))
            {
                algorithm = DefaultKeyWrapAlgorithm;
            }

            if (key == null || key.Length == 0)
            {
                throw new ArgumentNullException("key");
            }

            AsymmetricEncryptionAlgorithm algo = AlgorithmResolver.Default[algorithm] as AsymmetricEncryptionAlgorithm;

            if (algo == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            using (var encryptor = algo.CreateEncryptor(_csp))
            {
                return(new Tuple <byte[], string>(encryptor.TransformFinalBlock(key, 0, key.Length), algorithm));
            }
        }
Esempio n. 2
0
        public Task <Tuple <byte[], byte[], string> > EncryptAsync(byte[] plaintext, byte[] iv = null, byte[] authenticationData = null, string algorithm = RsaOaep.AlgorithmName, CancellationToken token = default(CancellationToken))
        {
            if (_csp == null)
            {
                throw new ObjectDisposedException(string.Format("RsaKey {0} is disposed", Kid));
            }

            if (string.IsNullOrWhiteSpace(algorithm))
            {
                algorithm = DefaultEncryptionAlgorithm;
            }

            if (plaintext == null || plaintext.Length == 0)
            {
                throw new ArgumentNullException("plaintext");
            }

            if (iv != null)
            {
                throw new ArgumentException("Initialization vector must be null", "iv");
            }

            if (authenticationData != null)
            {
                throw new ArgumentException("Authentication data must be null", "authenticationData");
            }

            AsymmetricEncryptionAlgorithm algo = AlgorithmResolver.Default[algorithm] as AsymmetricEncryptionAlgorithm;

            if (algo == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            using (var encryptor = algo.CreateEncryptor(_csp))
            {
                try
                {
                    var result = new Tuple <byte[], byte[], string>(encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length), null, algorithm);

                    return(Task.FromResult(result));
                }
                catch (Exception ex)
                {
                    return(TaskException.FromException <Tuple <byte[], byte[], string> >(ex));
                }
            }
        }