【13】MD5编码、Zlib压缩解压缩

1.MD5加密

        /// <summary>
        /// 使用MD5加密算法
        /// </summary>
        /// <param name="md5MessageStr">需要加密的字符串</param>
        /// <returns>加密后返回字符串</returns>
        public static string GetMD5String(string md5MessageStr)
        {
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                byte[] convertValue = Encoding.UTF8.GetBytes(md5MessageStr);
                byte[] resultValue = md5.ComputeHash(convertValue);
                string strResult = string.Empty;
                for (int i = 0; i < 16; i++)
                {
                    strResult += resultValue[i].ToString("x2");
                }
                return strResult;
            }
        }

2.Zlib压缩解压缩

nuget获取zlib.net

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using zlib;

namespace LargePlatformService.Logic
{
    public class ZLibNetHelper
    {
        /// <summary>  
        /// zlib.net 解压函数  
        /// </summary>  
        /// <param name="strSource">带解压数据源</param>  
        /// <returns>解压后的数据</returns>  
        public static string DeflateDecompress(string strSource)
        {
            int data = 0;
            int stopByte = -1;
            byte[] Buffer = Convert.FromBase64String(strSource); // 解base64  
            MemoryStream intms = new MemoryStream(Buffer);
            zlib.ZInputStream inZStream = new zlib.ZInputStream(intms);
            List<byte> inByteList = new List<byte>();
            int i = 0;
            while (stopByte != (data = inZStream.Read()))
            {
                inByteList.Add((byte)data);
            }
            inZStream.Close();
            return System.Text.Encoding.UTF8.GetString(inByteList.ToArray(), 0, inByteList.Count);

        }

        /// <summary>  
        /// zlib.net 压缩函数  
        /// </summary>  
        /// <param name="strSource">待压缩数据</param>  
        /// <returns>压缩后数据</returns>  
        public static string DeflateCompress(string strSource)
        {
            MemoryStream outms = new MemoryStream();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strSource);
            MemoryStream inms = new MemoryStream(bytes);
            zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outms, zlib.zlibConst.Z_DEFAULT_COMPRESSION);
            try
            {
                CopyStream(inms, outZStream);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                outZStream.Close();
            }
            return Convert.ToBase64String(outms.ToArray());
        }


        public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
        {
            byte[] buffer = new byte[2000];
            int len;
            while ((len = input.Read(buffer, 0, 2000)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            output.Flush();
        }      
    }
}


猜你喜欢

转载自blog.csdn.net/chen_peng7/article/details/78115486