C# - GZip压缩/解压缩字符串 和 签名算法及摘要

   #region 随机key:64位、128位、192位
        /// <summary>
        /// 获取随机key 【BitType:位数  传入1:64位/8个字符;传入2:128位/16个字符;传入3:192位/24个字符;】
        /// </summary>
        public static byte[] randomKey(int BitType)
        {
            try
            {
                string typeName = "";
                int typeSize = 0;
                if (BitType == 1)
                {
                    typeName = "DES";
                    typeSize = 64;
                }
                if (BitType == 2)
                {
                    typeName = "TripleDES";
                    typeSize = 128;
                }
                if (BitType == 3)
                {
                    typeName = "3DES";
                    typeSize = 192;
                }

                SymmetricAlgorithm symmProvider = SymmetricAlgorithm.Create(typeName);
                symmProvider.KeySize = typeSize;
                byte[] bkey = symmProvider.Key;
                return bkey;
            }
            catch
            {
                byte[] bnull = { };
                return bnull;
            }
        }
        #endregion

        #region GZip压缩/解压缩字符串
        /// <summary>
        /// GZip压缩函数(支持版本:2.0),返回字符串
        /// </summary>
        public static string CompressGZip(byte[] strSource)
        {
            try
            {
                if (strSource == null)
                    throw new System.ArgumentException("字符串为空!");
                //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strSource);
                byte[] buffer = strSource;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                byte[] buffer1 = ms.ToArray();
                ms.Close();
                return Convert.ToBase64String(buffer1, 0, buffer1.Length); //将压缩后的byte[]转换为Base64String
            }
            catch
            {
                string bnull = "";
                return bnull;
            }
        }
        /// <summary>
        /// GZip压缩函数(支持版本:2.0),返回字节
        /// </summary>
        public static byte[] CompressGZipExt(byte[] strSource)
        {
            try
            {
                if (strSource == null)
                    throw new System.ArgumentException("字符串为空!");
                //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strSource);
                byte[] buffer = strSource;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                byte[] buffer1 = ms.ToArray();

                return buffer1;
            }
            catch
            {
                byte[] bnull = new byte[0];
                return bnull;
            }
        }
        /// <summary>
        /// Gzip解压缩函数(支持版本:2.0)【参数strSource:原文;codeType:指定编码类型,值1:Base64,值2:UTF8】
        /// </summary>
        public static string DecompressGZip(string strSource, int codeType)
        {
            try
            {
                if (strSource == null)
                    throw new System.ArgumentException("字符串不能为空!");
                byte[] buffer = Convert.FromBase64String(strSource);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ms.Write(buffer, 0, buffer.Length);
                ms.Position = 0;
                System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
                stream.Flush();
                int nSize = 6000 * 1024 + 256; //字符串不超过6000K
                byte[] decompressBuffer = new byte[nSize];
                int nSizeIncept = stream.Read(decompressBuffer, 0, nSize);
                stream.Close();
                string backStr = "";
                if (codeType == 1)
                {
                    backStr = System.Convert.ToBase64String(decompressBuffer, 0, nSizeIncept);
                }
              
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值