using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Helper
{
    public static class Aes128Helper
    {
        /// <summary>
        ///  AES 加密
        /// </summary>
        /// <param name="str">明文（待加密）</param>
        /// <param name="aesSecretKey">密钥</param>
        /// <returns></returns>
        //public static string AESEncrypt(this string str, string aesSecretKey = "bpa20210418bpa20210418bpa*******")
        //{
        //    try
        //    {
        //        if (string.IsNullOrEmpty(str)) return null;
        //        byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
        //        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(aesSecretKey);


        //        RijndaelManaged rm = new RijndaelManaged
        //        {
        //            Key = keyArray,
        //            Mode = CipherMode.ECB,
        //            Padding = PaddingMode.PKCS7
        //        };

        //        ICryptoTransform cTransform = rm.CreateEncryptor();
        //        Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        //        string strs = "";
        //        for (var x = 0; x < resultArray.Length; x++)
        //        {
        //            var i = Convert.ToString(resultArray[x], 16);
        //            strs += i.PadLeft(2, '0');
        //        }
        //        return strs;
        //    }
        //    catch (Exception ex)
        //    {
        //        return "";
        //    }

        //}
        /// <summary>
        ///  AES 解密
        /// </summary>
        /// <param name="str">密文（待解密）</param>
        /// <param name="aesSecretKey">密钥</param>
        /// <returns></returns>
        //public static string AESDecrypt(this string str, string aesSecretKey = "bpa20210418bpa20210418bpa*******")
        //{
        //    try
        //    {
        //        if (string.IsNullOrEmpty(str)) return null;
        //        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(aesSecretKey);
        //        byte[] toEncryptArray = new byte[str.Length / 2];
        //        for (var x = 0; x < toEncryptArray.Length; x++)
        //        {
        //            var i = Convert.ToInt32(str.Substring(x * 2, 2), 16);
        //            toEncryptArray[x] = (byte)i;
        //        }

        //        RijndaelManaged rDel = new RijndaelManaged();
        //        rDel.Key = keyArray;
        //        rDel.Mode = CipherMode.ECB;
        //        rDel.Padding = PaddingMode.PKCS7;

        //        ICryptoTransform cTransform = rDel.CreateDecryptor();
        //        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        //        return UTF8Encoding.UTF8.GetString(resultArray);
        //    }
        //    catch (Exception ex)
        //    {
        //        return "";
        //    }
        //}
    }
}
