예제 #1
0
        /// <summary>
        /// Wrap a key using RSA encryption.
        /// </summary>
        /// <param name="keyBytes">the key to be wrapped</param>
        /// <returns>A wrapped key</returns>
        /// <exception cref="ArgumentNullException">'keyBytes' is null or has length == 0.</exception>
        /// <exception cref="ObjectDisposedException">If <see cref="RsaKeyWrapProvider.Dispose(bool)"/> has been called.</exception>
        /// <exception cref="SecurityTokenKeyWrapException">Failed to wrap the 'keyBytes'.</exception>
        /// <exception cref="InvalidOperationException">If the internal RSA algorithm is null.</exception>
        public override byte[] WrapKey(byte[] keyBytes)
        {
            if (keyBytes == null || keyBytes.Length == 0)
            {
                throw LogHelper.LogArgumentNullException(nameof(keyBytes));
            }

            if (_disposed)
            {
                throw LogHelper.LogExceptionMessage(new ObjectDisposedException(GetType().ToString()));
            }

#if NETSTANDARD1_4
            var padding = (Algorithm.Equals(SecurityAlgorithms.RsaOAEP, StringComparison.Ordinal) ||
                           Algorithm.Equals(SecurityAlgorithms.RsaOaepKeyWrap, StringComparison.Ordinal))
                        ? RSAEncryptionPadding.OaepSHA1
                        : RSAEncryptionPadding.Pkcs1;
            try
            {
                if (_rsa != null)
                {
                    return(_rsa.Encrypt(keyBytes, padding));
                }
            }
            catch (Exception ex)
            {
                throw LogHelper.LogExceptionMessage(new SecurityTokenKeyWrapException(string.Format(CultureInfo.InvariantCulture, LogMessages.IDX10658, ex)));
            }
#else
            bool fOAEP = Algorithm.Equals(SecurityAlgorithms.RsaOAEP, StringComparison.Ordinal) ||
                         Algorithm.Equals(SecurityAlgorithms.RsaOaepKeyWrap, StringComparison.Ordinal);
            try
            {
                if (_rsaCryptoServiceProvider != null)
                {
                    return(_rsaCryptoServiceProvider.Encrypt(keyBytes, fOAEP));
                }
                else if (_rsaCryptoServiceProviderProxy != null)
                {
                    return(_rsaCryptoServiceProviderProxy.Encrypt(keyBytes, fOAEP));
                }
            }
            catch (Exception ex)
            {
                throw LogHelper.LogExceptionMessage(new SecurityTokenKeyWrapException(string.Format(CultureInfo.InvariantCulture, LogMessages.IDX10658, ex)));
            }
#endif
            throw LogHelper.LogExceptionMessage(new InvalidOperationException(LogMessages.IDX10644));
        }
예제 #2
0
        public void RSAEncryptDecrypt(RSACryptoServiceProviderProxyTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RSAEncryptDecrypt", theoryData);

            try
            {
                var proxy           = new RSACryptoServiceProviderProxy(theoryData.RsaCryptoServiceProvider);
                var cipherTextProxy = proxy.Encrypt(theoryData.Input, theoryData.UseOAEP);
                var cipherTextRsa   = theoryData.RsaCryptoServiceProvider.Encrypt(theoryData.Input, theoryData.UseOAEP);
                IdentityComparer.AreBytesEqual(
                    proxy.Decrypt(cipherTextProxy, theoryData.UseOAEP),
                    theoryData.RsaCryptoServiceProvider.Decrypt(cipherTextRsa, theoryData.UseOAEP),
                    context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }