namespace OubHackMS.Crypto
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    internal class AES
    {
        private byte[] bIV;
        private byte[] bKey;

        public AES(byte[] bIV, byte[] bKey)
        {
            this.bIV = bIV;
            this.bKey = bKey;
        }

        public string decrypt(byte[] bText)
        {
            using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
            {
                provider.Key = this.bKey;
                provider.IV = this.bIV;
                provider.Padding = PaddingMode.PKCS7;
                provider.Mode = CipherMode.CBC;
                ICryptoTransform transform = provider.CreateDecryptor(provider.Key, provider.IV);
                using (MemoryStream stream = new MemoryStream(bText))
                {
                    using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader reader = new StreamReader(stream2))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
        }

        public byte[] encrypt(string sText)
        {
            using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
            {
                provider.Key = this.bKey;
                provider.IV = this.bIV;
                provider.Padding = PaddingMode.PKCS7;
                provider.Mode = CipherMode.CBC;
                ICryptoTransform transform = provider.CreateEncryptor(provider.Key, provider.IV);
                using (MemoryStream stream = new MemoryStream())
                {
                    using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(stream2))
                        {
                            writer.Write(sText);
                        }
                        return stream.ToArray();
                    }
                }
            }
        }

        public byte[] encrypt(byte[] bText)
        {
            using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
            {
                provider.Key = this.bKey;
                provider.IV = this.bIV;
                provider.Padding = PaddingMode.PKCS7;
                provider.Mode = CipherMode.CBC;
                ICryptoTransform transform = provider.CreateEncryptor(provider.Key, provider.IV);
                using (MemoryStream stream = new MemoryStream())
                {
                    using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(stream2))
                        {
                            writer.Write(Encoding.ASCII.GetString(bText));
                        }
                        return stream.ToArray();
                    }
                }
            }
        }
    }
}

