Encription

  public class Encrypt
    {

        private static Encoding encoding = Encoding.Default;
        public static string EncryptString(string originalStr)
        {
            return EncryptString(originalStr, "*&%$$FGad232f%^&&*()GTH$%3245gth", "@#%9286(^$jk$%#?", true);
        }

        private static string EncryptString(string originalStr, string key, string iv, bool returnBase64Code)
        {
            try
            {
                if (string.IsNullOrEmpty(originalStr) == false)
                {
                    byte[] sourcesBytes;
                    byte[] originalBytes = encoding.GetBytes(originalStr);
                    byte[] keyBytes = encoding.GetBytes(key);
                    byte[] ivBytes = encoding.GetBytes(iv);
                    using (MemoryStream ms = new MemoryStream(originalBytes.Length))
                    {
                        using (RijndaelManaged rm = new RijndaelManaged())
                        {
                            using (ICryptoTransform ic = rm.CreateEncryptor(keyBytes, ivBytes))
                            {
                                using (CryptoStream cs = new CryptoStream(ms, ic, CryptoStreamMode.Write))
                                {
                                    cs.Write(originalBytes, 0, originalBytes.Length);
                                    cs.FlushFinalBlock();
                                    sourcesBytes = ms.ToArray();
                                }
                            }
                            rm.Clear();
                        }
                    }
                    return (returnBase64Code ? Convert.ToBase64String(sourcesBytes) : encoding.GetString(sourcesBytes));
                }
            }
            catch
            {
            }
            return string.Empty;
        }


        public static string DecryptString(string targetStr)
        {
            return DecryptString(targetStr, "*&%$$FGad232f%^&&*()GTH$%3245gth", "@#%9286(^$jk$%#?", true);
        }

        private static string DecryptString(string targetstr, string key, string iv, bool returnBaseString)
        {
            try
            {
                byte[] targetBytes = (returnBaseString ? Convert.FromBase64String(targetstr) : encoding.GetBytes(targetstr));
                byte[] sourcesBytes = new byte[targetBytes.Length];
                byte[] keyBytes = encoding.GetBytes(key);
                byte[] ivBytes = encoding.GetBytes(iv);
                using (MemoryStream ms = new MemoryStream(targetBytes))
                {
                    using (RijndaelManaged rg = new RijndaelManaged())
                    {
                        using (ICryptoTransform ic = rg.CreateDecryptor(keyBytes, ivBytes))
                        {
                            using (CryptoStream cs = new CryptoStream(ms, ic, CryptoStreamMode.Read))
                            {
                                cs.Read(sourcesBytes, 0, sourcesBytes.Length);
                            }
                        }
                        rg.Clear();
                    }
                }
                return encoding.GetString(sourcesBytes).Trim(new char[] { '\0' });
            }
            catch
            {
            }
            return string.Empty;
        }

        public static string MD5Encrpyt(string originalStr, bool toUpper)
        {
            try
            {
                if (string.IsNullOrEmpty(originalStr) == false)
                {
                    byte[] inputBytes = encoding.GetBytes(originalStr);
                    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                    byte[] encryptBytes = md5.ComputeHash(inputBytes);
                    md5.Clear();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < encryptBytes.Length; i++)
                    {
                        sb.AppendFormat("{0:x2}", encryptBytes[i]);
                    }
                    return (toUpper ? sb.ToString().ToUpper() : sb.ToString());
                }
            }
            catch
            {
            }
            return string.Empty;
        }

        public static Encoding Encoding
        {
            get { return encoding; }
            set { encoding = value; }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值