Ejemplo n.º 1
0
        /// <summary>
        /// Gets the public key.
        /// </summary>
        /// <returns></returns>
        public virtual ISitecorePublicKeyResponse GetPublicKey()
        {
            var query = new SitecoreActionQuery("getpublickey");

            ISitecorePublicKeyResponse response = GetResponse <SitecorePublicKeyResponse>(query);

            return(response.Validate() ? response : null);
        }
        /// <summary>
        /// Encrypts the header value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string EncryptHeaderValue(string value, ISitecorePublicKeyResponse key)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException("value", "value cannot be null or empty when encrypting headers");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key", "key cannot be null when encrypting headers");
            }

            try
            {
                byte[] encrypted;

                using (var rsa = new RSACryptoServiceProvider())
                {
                    var rsaKeyInfo = new RSAParameters
                    {
                        // set rsaKeyInfo to the public key values.
                        Modulus  = Encoding.UTF8.GetBytes(key.Modulus),
                        Exponent = Encoding.UTF8.GetBytes(key.Exponent)
                    };

                    rsa.ImportParameters(rsaKeyInfo);

                    encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(value), false);
                }

                return(Convert.ToBase64String(encrypted));
            }
            catch (Exception ex)
            {
                Log.WriteError("Error encrypting header value", ex);
            }

            return(string.Empty);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Encrypts the header value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string EncryptHeaderValue(string value, ISitecorePublicKeyResponse key)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException("value", "value cannot be null or empty when encrypting headers");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key", "key cannot be null when encrypting headers");
            }

            try
            {
                byte[] encrypted;

                using (var rsa = new RSACryptoServiceProvider())
                {
                    var rsaKeyInfo = new RSAParameters
                                         {
                                             // set rsaKeyInfo to the public key values.
                                             Modulus = Encoding.UTF8.GetBytes(key.Modulus),
                                             Exponent = Encoding.UTF8.GetBytes(key.Exponent)
                                         };

                    rsa.ImportParameters(rsaKeyInfo);

                    encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(value), false);
                }

                return Convert.ToBase64String(encrypted);
            }
            catch (Exception ex)
            {
                LogFactory.Error("Error encrypting header value", ex);
            }

            return string.Empty;
        }