游戏数据压缩与加密(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;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值