游戏数据压缩与加密(C#)

有需要的话,常见的游戏数据处理方式是

  • 数据压缩、数据加密
  • 数据解密、数据解压

压缩方面的选择SharpZipLib,加密使用AES 

后面可用的实例选择Gzip+AES

using ICSharpCode.SharpZipLib.GZip;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace test
{
    class Program
    {
        public static string GZipCompressString(string rawString)
        {
            MemoryStream memoryStream = new MemoryStream();
            GZipOutputStream gZipOutputStream = new GZipOutputStream(memoryStream);
            byte[] bytes = Encoding.UTF8.GetBytes(rawString);
            gZipOutputStream.Write(bytes, 0, bytes.Length);
            gZipOutputStream.Close();
            byte[] inArray = memoryStream.ToArray();
            return Convert.ToBase64String(inArray);
        }

        public static string GZipDecompressString(string zippedString)
        {
            byte[] buffer = Convert.FromBase64String(zippedString);
            GZipInputStream gZipInputStream = new GZipInputStream(new MemoryStream(buffer));
            MemoryStream memoryStream = new MemoryStream();
            byte[] array = new byte[4096];
            int count;
            while ((count = gZipInputStream.Read(array, 0, array.Length)) != 0)
            {
                memoryStream.Write(array, 0, count);
            }
            byte[] bytes = memoryStream.ToArray();
            return Encoding.UTF8.GetString(bytes);
        }

        public static string AesEncrypt(string str, string key)
        {
            string result;
            try
            {
                if (string.IsNullOrEmpty(str))
                {
                    result = null;
                }
                else
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(str);
                    RijndaelManaged rijndaelManaged = new RijndaelManaged
                    {
                        Key = Encoding.UTF8.GetBytes(key),
                        Mode = CipherMode.ECB,
                        Padding = PaddingMode.PKCS7
                    };
                    ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor();
                    byte[] array = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length);
                    result = Convert.ToBase64String(array, 0, array.Length);
                }
            }
            catch (Exception ex)
            {
                result = null;
            }
            return result;
        }

        public static string AesDecrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            string result;
            try
            {
                byte[] array = Convert.FromBase64String(str);
                RijndaelManaged rijndaelManaged = new RijndaelManaged
                {
                    Key = Encoding.UTF8.GetBytes(key),
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
                ICryptoTransform cryptoTransform = rijndaelManaged.CreateDecryptor();
                byte[] bytes = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
                result = Encoding.UTF8.GetString(bytes);
            }
            catch (Exception ex)
            {
                result = null;
            }
            return result;
        }

        static string AES_KEY_16 = "VxO0=3DFUsPQAms8";

        static void Main(string[] args)
        {
            string text = "你好안녕하세요hellohejOlátereHalloこんにちはahojcześćBonjour";
            // 加密数据
            string encryptStr =  AesEncrypt(GZipCompressString(text), AES_KEY_16);
            string decryptStr = GZipDecompressString(AesDecrypt(encryptStr, AES_KEY_16));

        }
    }
}

以上是一些通讯数据或小数据的压缩和加密,考虑到效率和速度问题,游戏资源的压缩实际上用过的是谷歌的brotli 压缩算法;

前几天压缩数据用到zlib,但是没用到sharpzipLib,使用的是zlib.net,用法有点奇葩:

static void ZlibCompress(string file, string outfile)
{
    using (var ifs = File.OpenRead(file))
    using (var ofs = File.Create(outfile))
    using (var zs = new zlib.ZOutputStream(ofs, zlibConst.Z_DEFAULT_COMPRESSION))
    {
        while (true)
        {
            byte[] buff = new byte[1024];
            int len = ifs.Read(buff, 0, buff.Length);

            zs.Write(buff, 0, len);
            if (len < 1024)
                break;
        }

    }
}

static void ZlibDecompress(string file, string outfile )
{
    using (var fs = File.OpenRead(file))
    using (var outs = File.Create(outfile))
    using (var zs = new zlib.ZOutputStream(outs))
    {
        byte[] buff = new byte[1024];
        while (true)
        {
            int len = fs.Read(buff, 0, buff.Length);
            zs.Write(buff, 0, len);

            if (len < 1024)
                break;
        }
    }
}

 

你可能感兴趣的:(游戏开发)