C# Md5与AES加密解密源码记录

1.AES 加密32个x是解密密钥

AES_k 是公钥。EncryptByAES方法和De方法都需要用到

     public static string EncryptByAES(string input, string key = "")
        {
            key = (string.IsNullOrEmpty(key) ? "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" : key);
            byte[] bytes = Encoding.UTF8.GetBytes(key.Substring(0, 0x20));
            string result;
            using (AesCryptoServiceProvider Dex= new AesCryptoServiceProvider())
            {
                Dex.Key = bytes;
                Dex.IV = AES_k;
                ICryptoTransform t= Dex.CreateEncryptor(Dex.Key, Dex.IV);
                using (MemoryStream m= new MemoryStream())
                {
                    using (CryptoStream c= new CryptoStream(m, t, CryptoStreamMode.Write))
                    {
                        using (StreamWriter s= new StreamWriter(c))
                        {
                            s.Write(input);
                        }
                        byte[] inArray = m.ToArray();
                        result = Convert.ToBase64String(inArray);
                    }
                }
            }
            return result;
        }

2.AES解密

  internal static string De(string input, string key = "")
        {
            string result;
            try
            {
                key = (string.IsNullOrEmpty(key) ? "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" : key);
                byte[] buffer = Convert.FromBase64String(input);
                byte[] bytes = Encoding.UTF8.GetBytes(key.Substring(0, 0x20));
                using (AesCryptoServiceProvider Dex= new AesCryptoServiceProvider())
                {
                    Dex.Key = bytes;
                    Dex.IV = AES_k;
                    ICryptoTransform t= Dex.CreateDecryptor(Dex.Key, Dex.IV);
                    using (MemoryStream m= new MemoryStream(buffer))
                    {
                        using (CryptoStream c= new CryptoStream(m, t, CryptoStreamMode.Read))
                        {
                            using (StreamReader s= new StreamReader(c))
                            {
                                result = s.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch
            {
                result = input;
            }
            return result;
        }
        private static readonly byte[] AES_k = new byte[]
   {
            0x11,
            0x14,
            0x16,
            0x72,
            0x92,
            0xA2,
            0xC1,
            0xE1,
            0x11,
            0x24,
            0x52,
            0x72,
            0x20,
            0xA2,
            0xC2,
            0xE2
   };

 3 MD5加密 代码

internal static string MD5JM(string CDKEY, string sKey)
        {
            DESCryptoServiceProvider Dex= new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(CDKEY);
            Dex.Key = Encoding.ASCII.GetBytes(sKey);
            Dex.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream m= new MemoryStream();
            CryptoStream c= new CryptoStream(m, Dex.CreateEncryptor(), CryptoStreamMode.Write);
            c.Write(bytes, 0, bytes.Length);
            c.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte b in m.ToArray())
            {
                stringBuilder.AppendFormat("{0:X2}", b);
            }
            stringBuilder.ToString();
            return stringBuilder.ToString();
        }

4 MD5解密

 internal static string DeMD5(string CDKEY, string sKey)//skey=8个字符
        {
            DESCryptoServiceProvider Dex= new DESCryptoServiceProvider();
            string result = string.Empty;
            try
            {
                byte[] array = new byte[CDKEY.Length / 2];
                for (int i = 0; i < CDKEY.Length / 2; i++)
                {
                    int num = Convert.ToInt32(CDKEY.Substring(i * 2, 2), 16);
                    array[i] = (byte)num;
                }
                Dex.Key = Encoding.ASCII.GetBytes(sKey);
                Dex.IV = Encoding.ASCII.GetBytes(sKey);
                MemoryStream m= new MemoryStream();
                CryptoStream c= new CryptoStream(m, Dex.CreateDecryptor(), CryptoStreamMode.Write);
                c.Write(array, 0, array.Length);
                c.FlushFinalBlock();
                new StringBuilder();
                result = Encoding.Default.GetString(m.ToArray());
            }
            catch (Exception)
            {
                return CDKEY;
            }
            return result;
        }

 调用方法

AES加密 

EncryptByAES(“需要加密的字符串”,“”)
//第二个参数可以自定义解密密码,32位字符可以为字母数字符号,大小写都可以。也可以不写默认方法里的密码

 

AES 解密 

De("需要解密的字符串","")// 第二个参数可以自定义解密密码,32位字符可以为字母数字符号,大小写都可以。也可以不写默认方法里的密码

MD5加密

MD5JM("需要加密的字符串","88888888")//8个8是密码,可以自己定义,必须8位字母和数字

MD5解密

DeMD5("需要解密的字符串","88888888")//8个8是密码,可以自己定义,必须8位字母和数字

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值