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

1.MD5加密

 1     /// <summary>  
 2     /// 使用MD5加密算法  
 3     /// </summary>  
 4     /// <param name="md5MessageStr">需要加密的字符串</param>  
 5     /// <returns>加密后返回字符串</returns>  
 6     public static string GetMD5String(string md5MessageStr)  
 7     {  
 8         using (MD5 md5 = new MD5CryptoServiceProvider())  
 9         {  
10             byte[] convertValue = Encoding.UTF8.GetBytes(md5MessageStr);  
11             byte[] resultValue = md5.ComputeHash(convertValue);  
12             string strResult = string.Empty;  
13             for (int i = 0; i < 16; i++)  
14             {  
15                 strResult += resultValue[i].ToString("x2");  
16             }  
17             return strResult;  
18         }  
19     }  

 

2.Zlib压缩解压缩

nuget获取zlib.net

 1         using System;  
 2         using System.Collections.Generic;  
 3         using System.IO;  
 4         using System.Linq;  
 5         using System.Web;  
 6         using zlib;  
 7           
 8         namespace LargePlatformService.Logic  
 9         {  
10             public class ZLibNetHelper  
11             {  
12                 /// <summary>    
13                 /// zlib.net 解压函数    
14                 /// </summary>    
15                 /// <param name="strSource">带解压数据源</param>    
16                 /// <returns>解压后的数据</returns>    
17                 public static string DeflateDecompress(string strSource)  
18                 {  
19                     int data = 0;  
20                     int stopByte = -1;  
21                     byte[] Buffer = Convert.FromBase64String(strSource); // 解base64    
22                     MemoryStream intms = new MemoryStream(Buffer);  
23                     zlib.ZInputStream inZStream = new zlib.ZInputStream(intms);  
24                     List<byte> inByteList = new List<byte>();  
25                     int i = 0;  
26                     while (stopByte != (data = inZStream.Read()))  
27                     {  
28                         inByteList.Add((byte)data);  
29                     }  
30                     inZStream.Close();  
31                     return System.Text.Encoding.UTF8.GetString(inByteList.ToArray(), 0, inByteList.Count);  
32           
33                 }  
34           
35                 /// <summary>    
36                 /// zlib.net 压缩函数    
37                 /// </summary>    
38                 /// <param name="strSource">待压缩数据</param>    
39                 /// <returns>压缩后数据</returns>    
40                 public static string DeflateCompress(string strSource)  
41                 {  
42                     MemoryStream outms = new MemoryStream();  
43                     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strSource);  
44                     MemoryStream inms = new MemoryStream(bytes);  
45                     zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outms, zlib.zlibConst.Z_DEFAULT_COMPRESSION);  
46                     try  
47                     {  
48                         CopyStream(inms, outZStream);  
49                     }  
50                     catch (Exception ex)  
51                     {  
52                         throw ex;  
53                     }  
54                     finally  
55                     {  
56                         outZStream.Close();  
57                     }  
58                     return Convert.ToBase64String(outms.ToArray());  
59                 }  
60           
61           
62                 public static void CopyStream(System.IO.Stream input, System.IO.Stream output)  
63                 {  
64                     byte[] buffer = new byte[2000];  
65                     int len;  
66                     while ((len = input.Read(buffer, 0, 2000)) > 0)  
67                     {  
68                         output.Write(buffer, 0, len);  
69                     }  
70                     output.Flush();  
71                 }        
72             }  
73         } 

 

posted @ 2017-09-27 17:55  清幽火焰  阅读(376)  评论(0编辑  收藏  举报