Exemplo n.º 1
0
        private string AES256(CryptoFunction cryptoFunction, string input)
        {
            try
            {
                if (string.IsNullOrEmpty(_aesKeys.Value.Key) ||
                    string.IsNullOrEmpty(input))
                {
                    return(input);
                }

                string result;
                switch (cryptoFunction)
                {
                case CryptoFunction.Encrypt:
                    result = EncryptInput(input);
                    break;

                case CryptoFunction.Decrypt:
                    result = DecryptInput(input);
                    break;

                default:
                    result = null;
                    break;
                }

                return(result);
            }
            catch (Exception ex)
            {
                _log.LogError(ex.Message, ex);

                return(input);
            }
        }
        private string AES256(CryptoFunction cryptoFunction, string input)
        {
            try
            {
                if (string.IsNullOrEmpty(_aesKeys.Value.Key) || string.IsNullOrEmpty(_aesKeys.Value.IV))
                {
                    return(input);
                }

                using (var aes = Aes.Create())
                {
                    ICryptoTransform cryptoTransform;
                    switch (cryptoFunction)
                    {
                    case CryptoFunction.Encrypt:
                        cryptoTransform = aes.CreateEncryptor(Convert.FromBase64String(_aesKeys.Value.Key), Convert.FromBase64String(_aesKeys.Value.IV));
                        break;

                    case CryptoFunction.Decrypt:
                        cryptoTransform = aes.CreateDecryptor(Convert.FromBase64String(_aesKeys.Value.Key), Convert.FromBase64String(_aesKeys.Value.IV));
                        break;

                    default:
                        cryptoTransform = null;
                        break;
                    }

                    using (cryptoTransform)
                    {
                        var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);

                        var transformedBytes = cryptoTransform.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
                        return(System.Text.Encoding.UTF8.GetString(transformedBytes));
                    }
                }
            }
            catch (Exception ex)
            {
                _log.LogError(ex.Message, ex);

                return(input);
            }
        }