#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);
}