using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    class Security
    {
        private static string Password = "df53ca268240ca76670c8566ee54568a"; //کردن فایل encrypt پسورد برای 
        public static string Password_
        {
            get { return Password; }
        }

        private static string PasswordProgram = "81dc9bdb52d04dc20036dbd8313ed055"; //پسورد تنظیمات برنامه
        public static string PasswordProgram_
        {
            get { return PasswordProgram; }
            set { PasswordProgram = value; }
        }

        Rijndael RJ = Rijndael.Create();
        MD5CryptoServiceProvider MD = new MD5CryptoServiceProvider();

        private byte[] MD5Hash(string Value)
        {
            return MD.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Value));
        }

        public string Encrypt_Text(string Text_TO_Encrypt, string Key)
        {
            RJ.Key = MD5Hash(Key);
            RJ.Mode = CipherMode.ECB;
            byte[] To_Encrypt = Encoding.ASCII.GetBytes(Text_TO_Encrypt);
            return Convert.ToBase64String(RJ.CreateEncryptor().TransformFinalBlock(To_Encrypt, 0, To_Encrypt.Length));
        }

        public string Decrypt_Text(string Encrypted_Text, string Key)
        {
            try
            {
                RJ.Key = MD5Hash(Key);
                RJ.Mode = CipherMode.ECB;
                byte[] To_Decrypt = Convert.FromBase64String(Encrypted_Text);
                return ASCIIEncoding.ASCII.GetString(RJ.CreateDecryptor().TransformFinalBlock(To_Decrypt, 0, To_Decrypt.Length));

            }
            catch
            {
                MessageBox.Show("Invalid Key", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public static string MD5(string password)
        {
            byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
            try
            {
                MD5CryptoServiceProvider cryptHandler = new MD5CryptoServiceProvider();
                byte[] hash = cryptHandler.ComputeHash(textBytes);
                string ret = "";
                foreach (byte a in hash)
                {
                    if (a < 16)
                        ret += "0" + a.ToString("x");
                    else
                        ret += a.ToString("x");
                }
                return ret;
            }
            catch { throw; }
        }
    
    }
    
}
