ゲームデータ圧縮と暗号化(C#)

4939 ワード

必要があれば、よくあるゲームデータの処理方法は
  • データ圧縮、データ暗号化
  • データ復号、データ解凍
  • 圧縮面の選択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;
            }
        }
    }