2009年1月15日 星期四

C# Compress Decompress byte[] 壓縮 解壓縮 byte[]

public static byte[] Compress(byte[] buffer)
{
try
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(buffer, 0, buffer.Length);
zip.Close();
ms.Position = 0;

MemoryStream outStream = new MemoryStream();

byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);

byte[] gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return gzBuffer;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}


public static byte[] Decompress(byte[] gzBuffer)
{
try
{
MemoryStream ms = new MemoryStream();
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

byte[] buffer = new byte[msgLength];

ms.Position = 0;
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
zip.Read(buffer, 0, buffer.Length);

return buffer;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}

C# 對稱 加密 解密 演算法 程式範例

C# 對稱 加密 解密 演算法 程式範例

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace DataCrypto
{
///
/// 對稱加密演算法
///
public class SymmetricMethod
{

private SymmetricAlgorithm mobjCryptoService;
private string Key;
///
/// 對稱加密的構造函數
///
public SymmetricMethod()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
///
/// 獲得密鑰
///
/// 密鑰
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < stemp =" sTemp.PadRight(KeyLength," stemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk" byttemp =" mobjCryptoService.IV;" ivlength =" bytTemp.Length;"> IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < stemp =" sTemp.PadRight(IVLength,">
/// 加密
///
///

待加密的串
/// 經過加密的串
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
///
/// 解密方法
///
/// 待解密的串
/// 經過解密的串
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
}