DataEncrypt

来源:互联网 发布:mysql document是什么 编辑:程序博客网 时间:2024/06/04 19:25
using System;using System.IO;//using System.Drawing;using System.Collections;using System.Collections.Generic;using System.Text;using System.IO.Compression;namespace ConsoleApplication1{ class DataEncrypt { private static string GetMD5(string str) { byte[] b = System.Text.Encoding.Default.GetBytes(str); b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b); string ret = ""; for (int i = 0; i < b.Length; i++) { ret += b[i].ToString("x").PadLeft(2, '0'); } return ret; } /// /// 将十六进制字符串转为字节数组 /// /// 十六进制字符串 /// public static byte[] HexToArray(string HexString) { ArrayList Bytes = new ArrayList(); string HexChar = ""; Int32 j; if (HexString.Substring(0, 2) == "0x") HexString = HexString.Substring(2, HexString.Length - 2); for (int i = 0; i < HexString.Length; i += 2) { HexChar = "%" + HexString.Substring(i, 2); j = 0; Bytes.Add(System.Uri.HexUnescape(HexChar, ref j)); } byte[] ReturnBytes = new byte[Bytes.Count]; for (int i = 0; i < Bytes.Count; i++) { ReturnBytes[i] = Convert.ToByte((char)Bytes[i]); } return ReturnBytes; } /// /// 将字节数组转成16进制的字符 /// /// 字节数组 /// public static string ArrayToHex(byte[] Bytes) { string HexSting = "0x"; foreach (byte Byte in Bytes) { HexSting += System.Uri.HexEscape(Convert.ToChar(Byte)).Substring(1, 2); } return HexSting; } /// /// 将Image对象转化成二进制流 /// /// /// //public byte[] ImageToByteArray(Image image) //{ // //实例化流 // MemoryStream imageStream = new MemoryStream(); // //将图片的实例保存到流中 // image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // //保存流的二进制数组 // byte[] imageContent = new Byte[imageStream.Length]; // imageStream.Position = 0; // //将流泻如数组中 // imageStream.Read(imageContent, 0, (int)imageStream.Length); // return imageStream.ToArray(); //} // 压缩Gzip public static string GzipCompress(string uncompressedString) { byte[] bytData = System.Text.Encoding.Unicode.GetBytes(uncompressedString); MemoryStream ms = new MemoryStream(); Stream s = new GZipStream(ms, CompressionMode.Compress); s.Write(bytData, 0, bytData.Length); s.Close(); byte[] compressedData = (byte[])ms.ToArray(); return System.Convert.ToBase64String(compressedData, 0, compressedData.Length); } // 解压Gzip public static string GzipDeCompress(string compressedString) { System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder(); int totalLength = 0; byte[] bytInput = System.Convert.FromBase64String(compressedString); ; byte[] writeData = new byte[4096]; Stream s = new GZipStream(new MemoryStream(bytInput), CompressionMode.Decompress); while (true) { int size = s.Read(writeData, 0, writeData.Length); if (size > 0) { totalLength += size; uncompressedString.Append(System.Text.Encoding.Unicode.GetString(writeData, 0, size)); } else { break; } } s.Close(); return uncompressedString.ToString(); } }}