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

namespace MOKA_Factory_Tools
{
    //处理加密解密
    public class AES_DES
    {
        //变量            
        public static byte[] AESIV { get; set; } = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        public static string AESKey { get; set; } ="32Dip3h9VIR4IYac";
        
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText"></param> 需要加密的字符串
        /// <param name="key"></param>  密钥
        /// <param name="IV"></param>   偏移向量
        /// <returns> 
        /// 加密结果字符串       
        /// </returns>
        public static string EncryptStringToBytes_Aes(string plainText, string key, byte[] IV)
        {
            try
            {
                byte[] Key = Encoding.UTF8.GetBytes(key);
                // Check arguments.
                if (plainText == null || plainText.Length <= 0)
                    throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("IV");
                byte[] encrypted;

                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
                    aesAlg.Mode = CipherMode.ECB;
                    aesAlg.Padding = PaddingMode.PKCS7;

                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
                return Convert.ToBase64String(encrypted);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="CipherText"></param> 需解密的字符串
        /// <param name="Key"></param>  密钥
        /// <param name="IV"></param>   偏移向量
        /// <returns> 
        /// 解密结果字符串       
        /// </returns>
        public static string DecryptStringFromBytes_Aes(string CipherText, string Key, byte[] IV)
        {
            try
            {
                byte[] cipherText = Convert.FromBase64String(CipherText);
                byte[] key = Encoding.UTF8.GetBytes(Key);

                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (key == null || key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("IV");

                string plaintext = null;
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    aesAlg.Key = key;
                    aesAlg.IV = IV;
                    aesAlg.Mode = CipherMode.ECB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                plaintext = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
                return plaintext;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }

}

