• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net总结之加密解密

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
       加密和解密常用于保护敏感的数据,比如url中的查询参数,防止被人手动修改等等。。。

/// <summary> /// md5加密(密文128位,一般不可逆) /// </summary> /// <param name="data">要加密的字符串</param> /// <returns></returns> public string Md5CryptData(string data) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] dataByte = Encoding.UTF8.GetBytes(data); byte[] md5Data = md5.ComputeHash(dataByte); StringBuilder sb=new StringBuilder(); foreach (var b in md5Data) { //0xA转换为0x0A sb.Append(b.ToString("x2")); } return sb.ToString(); }
        /// <summary>
        /// DES加密(密文64位,密钥64位,IV向量64位)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string DesEnCryptData(string data)
        {
            const string key = "a1b2c3d4";
            const string iv = "a1b2c3d4";
            byte[] keyByte = Encoding.UTF8.GetBytes(key);
            byte[] ivByte = Encoding.UTF8.GetBytes(iv);

            DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider();

            MemoryStream memoryStream=new MemoryStream();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, desEncrypt.CreateEncryptor(keyByte, ivByte),
                                                         CryptoStreamMode.Write);

            byte[] dataByte = Encoding.UTF8.GetBytes(data);

            cryptoStream.Write(dataByte, 0, dataByte.Length);

            cryptoStream.FlushFinalBlock();

            byte[] tmp = memoryStream.ToArray();

            return Convert.ToBase64String(tmp);
;
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string DesDeCryptData(string data)
        {

            byte[] dataByte = Convert.FromBase64String(data);

            MemoryStream memoryStream = new MemoryStream(dataByte);

            const string key = "a1b2c3d4";
            const string iv = "a1b2c3d4";
            byte[] keyByte = Encoding.UTF8.GetBytes(key);
            byte[] ivByte = Encoding.UTF8.GetBytes(iv);

            DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, desEncrypt.CreateDecryptor(keyByte, ivByte),
                                                         CryptoStreamMode.Read);
            byte[] tmp = new byte[dataByte.Length];

            cryptoStream.Read(tmp, 0, tmp.Length);

            return Encoding.UTF8.GetString(tmp);
        }
        /// <summary>
        /// Rijndael加密(密文128位,最长196位,IV向量16位,密钥16、24、32位)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string EncryptData(string data)
        {
            string key = "12345678123456781234567812345678";
            string iv = "1234567812345678";
            byte[] keyByte = Encoding.UTF8.GetBytes(key);
            byte[] ivByte = Encoding.UTF8.GetBytes(iv);

            Rijndael rijndael = Rijndael.Create();

            MemoryStream mStream=new MemoryStream();

            CryptoStream cryptoStream = new CryptoStream(mStream, rijndael.CreateEncryptor(keyByte, ivByte),
                                                         CryptoStreamMode.Write);
            byte[] dataByte = Encoding.UTF8.GetBytes(data);

            cryptoStream.Write(dataByte,0,dataByte.Length);

            cryptoStream.FlushFinalBlock();

            byte[] tmp = mStream.ToArray();

            return Convert.ToBase64String(tmp);
        }

        /// <summary>
        /// Rijndael解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string DecryptData(string data)
        {
            byte[] dataByte = Convert.FromBase64String(data);
            string key = "12345678123456781234567812345678";
            string iv = "1234567812345678";
            byte[] keyByte = Encoding.UTF8.GetBytes(key);
            byte[] ivByte = Encoding.UTF8.GetBytes(iv);

            MemoryStream memoryStream=new MemoryStream(dataByte);

            Rijndael rijndael = Rijndael.Create();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(keyByte, ivByte),
                                                         CryptoStreamMode.Read);
            byte[] tmp=new byte[dataByte.Length];

            cryptoStream.Read(tmp,0,tmp.Length);

            return Encoding.UTF8.GetString(tmp);

        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET2.0的App_Offline.htm发布时间:2022-07-10
下一篇:
Asp.netMvc4基于Authorize实现的模块访问权限发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap