DataEncrypt

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(); } } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值