【Unity】C#存档与加密

换工作了,这下是纯C#开发了,偏单机游戏,所以又要研究一下C#的存档做法。经过一阵时间的解决各种问题,现在已经稳定,需要的老铁可以参考一下。

1.导入ProtoBuf

https://github.com/protocolbuffers/protobuf/releases/
下载需要的语言,解压后导入到自己的目录中。

2.协议声明

[ProtoContract]
    public class DataRoot
    {
        [ProtoMember(1)]
        public int sA;
        [ProtoMember(2)]
        public string sB;
        [ProtoMember(3)]
        public float[] sC;
        [ProtoMember(4)]
        public DataA sD;
        [ProtoMember(5)]
        public List<DataA> sE;
    }

    [ProtoContract]
    public class DataA
    {
        [ProtoMember(1)]
        public int sA1;
        [ProtoMember(2)]
        public int sA2;
    }
  • ProtoContract 声明要序列化的类
  • ProtoMember 声明要序列化的成员

3.存档

数据生成

    DataRoot data = new DataRoot()
    {
        sA = 0,
        sB = "AAA",
        sC = new float[2] { 1, 2 },
        sE = null,
    };
    data.sD = new DataA();

    Debug.Log("保存存档完成");

封包与保存

    //将内容进行封包
    File.Delete(Application.persistentDataPath + "/Save.dat");
    FileStream stream = File.OpenWrite(Application.persistentDataPath + "/Save.dat");
    Serializer.Serialize(stream, data);
    stream.Dispose();
  • ProtoBuf.Serializer.Serialize protobuf自带的序列化方法 将数据转为byte[]
  • protobuf的写入方式是将bytes.length覆盖写入到文件中,最后用ProtoReader.TO_EOF作为结束符添加到之后的一位。也就是说文件的bytes.length永远保持为最大的长度。在项目中,不知道为什么,存档的byte[]变短之后,这个结束符一直有问题,导致存档读取出错,所以我干脆在每次存档时将原存档文件删除。

4.读档

读取与解包

    private void TryDeserialize(out DataRoot data)
    {
        if (File.Exists(Application.persistentDataPath + "/Save.dat"))
        {
			
			//↓↓↓这一部分比较特殊 根据需要判断是否要添加↓↓↓
            Support.SetInstanceFunc((Type type) =>
            {
                if (Type.GetType(type.FullName) == null)
                {
                    return null;
                }

                return Activator.CreateInstance(type
#if !(CF || SILVERLIGHT || WINRT || PORTABLE || NETSTANDARD1_3 || NETSTANDARD1_4)
                        , nonPublic: true
#endif
                    );
            });
            //↑↑↑这一部分比较特殊 根据需要判断是否要添加↑↑↑
            
            //对内容进行解包
            FileStream stream = File.OpenRead(Application.persistentDataPath + "/Save.dat");
            data = Serializer.Deserialize<DataRoot>(stream);
            stream.Dispose();
        }
        else
        {
            data = null;
        }
		
		//↓↓↓这一部分比较特殊 根据需要判断是否要添加↓↓↓
		Support.SetInstanceFunc(null);
		//↑↑↑这一部分比较特殊 根据需要判断是否要添加↑↑↑
    }
  • ProtoBuf.Serializer.Deserialize<T> protobuf自带的反序列化方法 将byte[]转为数据
    private static Func<Type, object> instanceFunc;
    public static void SetInstanceFunc(Func<Type, object> func)
    {
        instanceFunc = func;
    }

    public static object CreateInstance(Type type)
    {
        if (instanceFunc != null)
        {
            object obj = instanceFunc(type);
            if (obj != null)
            {
                return obj;
            }
        }
        // ReSharper disable once AssignNullToNotNullAttribute
        if (Type.GetType(type.FullName) == null)
        {
            return _appDomain.Instantiate(type.FullName);
        }

        return Activator.CreateInstance(type
#if !(CF || SILVERLIGHT || WINRT || PORTABLE || NETSTANDARD1_3 || NETSTANDARD1_4)
            , nonPublic: true
#endif
        );
  • 因为我们的项目涉及C#代码的热更新,所以存档管理器在热更新的程序集里,而protobuf因为其他内容的需要,是放在更深层的程序集里,而这个程序集是没有引用热更新的程序集的,所以在解包的时候,找不到热更新的程序集里的类。我只好在ProtoBuf.Support的类中添加了一个方法,在Support的CreateInstance方法中,把实例化的方法勾出来,解包的时候从热更新的程序集中实例化。

数据恢复

    public void Load()
    {
        TryDeserialize(out DataRoot data);

        if (data != null)
        {
            int _sA = data.sA;
            string _sB = data.sB;
            float[] _sC = data.sC;
            DataA _sD = data.sD;
            List<DataA> _sE = data.sE;
            if (_sE != null)
            {
                //TODO
            }
            Debug.Log("加载存档完成");
        }
        else
        {
            //TODO 没有存档,可以执行如注册或新手引导之类的方法
        }
    }
  • List和ArrayList,如果在存档时数据的长度为0,解包出来的数据是null,注意判断这个情况。

5.加密

AESCryptionUtility

这里提供一份使用AES加密解密的代码,支持string和byte[]的加密解密。加密解密时需要传入一个长度为32的字符串作为密钥。

    /// <summary>
    /// AES加解密字符串
    /// </summary>
    public static class AESCryptionUtility
    {
        /// <summary>
        /// AES加密 String
        /// </summary>
        /// <param name="str">被加密的明文</param>
        /// <param name="key">密钥</param>
        /// <returns>密文</returns>
        public static string Encrypt(string str, string key)
        {
            MemoryStream mStream = new MemoryStream();
            RijndaelManaged aes = new RijndaelManaged();

            byte[] plainbytes = Encoding.UTF8.GetBytes(str);
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            //aes.Key = _key;
            aes.Key = bKey;
            //aes.IV = _iV;
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
            try
            {
                cryptoStream.Write(plainbytes, 0, plainbytes.Length);
                cryptoStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }

        /// <summary>
        /// AES加密 Bytes
        /// </summary>
        /// <param name="bytes">被加密的明文bytes</param>
        /// <param name="key">密钥</param>
        /// <returns>密文</returns>
        public static byte[] Encrypt(byte[] bytes, string key)
        {
            MemoryStream mStream = new MemoryStream();
            RijndaelManaged aes = new RijndaelManaged();

            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            //aes.Key = _key;
            aes.Key = bKey;
            //aes.IV = _iV;
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
            try
            {
                cryptoStream.Write(bytes, 0, bytes.Length);
                cryptoStream.FlushFinalBlock();
                return mStream.ToArray();
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }

        /// <summary>
        /// AES解密 String
        /// </summary>
        /// <param name="str">被加密的明文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static string Decrypt(string str, string key)
        {
            byte[] encryptedbytes = Convert.FromBase64String(str);
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            MemoryStream mStream = new MemoryStream(encryptedbytes);
            //mStream.Write( encryptedbytes, 0, encryptedbytes.Length );
            //mStream.Seek( 0, SeekOrigin.Begin );
            RijndaelManaged aes = new RijndaelManaged();
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            aes.Key = bKey;
            //aes.IV = _iV;
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
            try
            {
                byte[] tmp = new byte[encryptedbytes.Length + 32];
                int len = cryptoStream.Read(tmp, 0, encryptedbytes.Length + 32);
                byte[] ret = new byte[len];
                Array.Copy(tmp, 0, ret, 0, len);
                return Encoding.UTF8.GetString(ret);
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }

        /// <summary>
        /// AES解密 Bytes
        /// </summary>
        /// <param name="bytes">被加密的明文bytes</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static byte[] Decrypt(byte[] bytes, string key)
        {
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            MemoryStream mStream = new MemoryStream(bytes);
            //mStream.Write( encryptedbytes, 0, encryptedbytes.Length );
            //mStream.Seek( 0, SeekOrigin.Begin );
            RijndaelManaged aes = new RijndaelManaged();
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            aes.Key = bKey;
            //aes.IV = _iV;
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
            try
            {
                byte[] tmp = new byte[bytes.Length + 32];
                int len = cryptoStream.Read(tmp, 0, bytes.Length + 32);
                byte[] ret = new byte[len];
                Array.Copy(tmp, 0, ret, 0, len);
                return ret;
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }
    }

相关参数

    private readonly string cryptionKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    private bool isCrtption;
  • cryptionKey AES密钥,长度为32位的随机字符
  • isCrtption 是否加密,作为存档Debug使用

存档加密

	//加密
    if (isCrtption)
    {
        //将内容进行封包
        FileStream stream = File.OpenWrite(Application.persistentDataPath + "/tempS");
        Serializer.Serialize(stream, data);
        stream.Dispose();
        //加载文件Bytes
        FileStream reader = new FileStream(Application.persistentDataPath + "/tempS", FileMode.Open, FileAccess.Read);
        byte[] fileBytes = new byte[reader.Length];
        reader.Read(fileBytes, 0, fileBytes.Length);
        reader.Dispose();
        //将内容加密
        fileBytes = AESCryptionUtility.Encrypt(fileBytes, cryptionKey);
        //保存存档
        File.Delete(Application.persistentDataPath + "/Save.dat");
        FileStream writer = new FileStream(Application.persistentDataPath + "/Save.dat", FileMode.OpenOrCreate, FileAccess.Write);
        writer.Write(fileBytes, 0, fileBytes.Length);
        writer.Dispose();
        //清理
        File.Delete(Application.persistentDataPath + "/tempS");
    }
    else
    {
        //将内容进行封包
        File.Delete(Application.persistentDataPath + "/Save.dat");
        FileStream stream = File.OpenWrite(Application.persistentDataPath + "/Save.dat");
        Serializer.Serialize(stream, data);
        stream.Dispose();
    }
  1. 先将数据序列化并存到一个临时文件中,以防止对同一个文件进行操作时的问题。
  2. 读取临时文件中的byte[]
  3. 对内容进行加密
  4. 将加密之后的byte[]保存到真正的存档文件中
  5. 删除临时文件

存档解密


	//解密
	if (isCrtption)
    {
        //加载文件Bytes
        FileStream reader = new FileStream(Application.persistentDataPath + "/Save.dat", FileMode.Open, FileAccess.Read);
        byte[] fileBytes = new byte[reader.Length];
        reader.Read(fileBytes, 0, fileBytes.Length);
        reader.Dispose();
        //将内容解密
        fileBytes = AESCryptionUtility.Decrypt(fileBytes, cryptionKey);
        //写道临时文件中
        FileStream writer = new FileStream(Application.persistentDataPath + "/tempS", FileMode.OpenOrCreate, FileAccess.Write);
        writer.Write(fileBytes, 0, fileBytes.Length);
        writer.Dispose();
        //对内容进行解包
        FileStream stream = File.OpenRead(Application.persistentDataPath + "/tempS");
        data = Serializer.Deserialize<DataRoot>(stream);
        stream.Dispose();
        //清理
        File.Delete(Application.persistentDataPath + "/tempS");
    }
    else
    {
        //对内容进行解包
        FileStream stream = File.OpenRead(Application.persistentDataPath + "/Save.dat");
        data = Serializer.Deserialize<DataRoot>(stream);
        stream.Dispose();
    }
  1. 先读取已加密的byte[]
  2. 对内容进行解密
  3. 将解密之后的byte[]写入到临时文件中。因为如果没有再进行存档的话,要保证原存档的正确,所以不能把内容覆盖到原存档中。
  4. 对解密之后的byte[]进行反序列化为真正的数据
  5. 删除临时文件
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: # Unity C# Unity C#(C Sharp)是一种基于.NET框架的面向对象编程语言,被广泛应用于开发Unity游戏引擎中的游戏、应用和其他交互性的体验项目。 与其他编程语言相比,Unity C#的特点在于它的简单易学、强大灵活和高效优化。Unity C#支持各种编程范式,包括过程式、面向对象、泛型、事件驱动和异步编程等,以满足开发者多变的需求。 在Unity中,C#主要用于编写游戏逻辑、控制游戏对象的行为和属性、实现玩家交互等功能。Unity C#支持许多现代编程特性,如LINQ、lambdas和扩展方法等,也为用户提供了许多额外的库和插件,如Unity GUI、Unity Networking和Unity Mobile Integration等,以满足不同平台的开发需求。 Unity C#Unity开发者社区中非常受欢迎,有许多开源项目和教程可供开发者学习和使用。同时,Unity C#的学习门槛相对较低,对于初学者来说非常友好,也是一种获得Unity游戏引擎开发技能的不错选择。 ### 回答2: # Unity C# Unity C#是用于编写Unity游戏引擎的一种脚本语言,类似于Java和C++。它支持面向对象编程和动态编译,并且具有良好的可读性和兼容性。Unity C#可以让开发者更方便地管理游戏对象,场景组件和材质贴图等游戏元素,使游戏的开发更加高效和简单。 使用Unity C#对游戏开发者而言有许多优势。首先,Unity C#不需要开发者考虑内存管理问题,因为Unity引擎自带内存管理。其次,Unity C#有常用的类库和工具,使得游戏开发者可以直接使用这些库和工具,而不需要单独编写。此外,Unity C#的性能不错,可以满足大多数游戏开发者的需求。 总的来说,Unity C#是一种方便简单的脚本语言,它可以加快游戏开发过程,减少开发者的心理负担。 ### 回答3: # Unity C#是什么? Unity C#是一种编程语言,是Unity引擎中广泛使用的编程语言。C#是一种高级的、经过现代化编程理念优化的语言,它的灵活性和可扩展性使它成为了很多程序员的首选语言之一。Unity C#的特点是简单易学、功能强大、代码可重用、快速开发和可跨平台编译等。 # Unity C#的作用? Unity C#Unity引擎中主要用于编写游戏的逻辑操作、动画、玩家输入、音效、物理交互等。Unity C#还可以用于制作游戏的菜单、UI、背包系统、任务系统等各种功能,能够为游戏带来更多的乐趣和便利。 # Unity C#的优点 Unity C#有很多优点,其中包括: 1. 易于学习:C#语言简单易懂,不需要特别高的专业技能,初学者通过快速入门后,即可快速掌握。 2. 功能强大:C#具有面向对象编程优势,可以进行对象抽象,也可以轻松进行多线程操作。 3. 模块化、可重用:C#编程语言具有良好的模块化特性,代码可以进行复用,能够提高编程效率。 4. 跨平台:Unity C#在不同平台之间的兼容性和可移植性很好,可以轻松打开任何支持Unity引擎的平台。 # 总结 Unity C#Unity游戏引擎中最常用的编程语言之一,它的简单易学、功能强大、代码可重用、快速开发和可跨平台编译等优点,使得C#语言成为众多游戏开发者的首选语言之一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值