为了账号安全,请及时绑定邮箱和手机立即绑定

Unity C# 加密错误

Unity C# 加密错误

C#
MYYA 2021-10-09 10:16:00
昨天它工作正常,但今天我不知道为什么,我收到此错误消息SerializationException:意外的二进制元素:0 System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject(BinaryElement 元素、System.IO.BinaryReader 读取器、System.Int64& objectId、System.Object& 值、System.Runtime.Serialization.SerializationInfo& 信息) (在 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:254) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement元素,System.IO.BinaryReader 阅读器)(在 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:130)System.Runtime.Serialization。 Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result,System.Runtime.Remoting.Messaging.Header[]& headers)(在 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:104)系统.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/ System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/ mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136) Data.FromByteArray[PlayerData](System.Byte[] 数据)(在 Assets/Scripts/Player/Data.cs:119)数据。Load ()(在 Assets/Scripts/Player/Data.cs:40)我在网上搜索了 4 小时,但我找不到解决此问题的方法。我希望任何人都可以帮助我。
查看完整描述

1 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

在对您的代码进行一些测试后,我最终使用了这两种方法。


我认为您的代码的主要问题是Decrypt从未对plainTextBytes-array做任何事情。


private static string Encrypt(byte[] plainTextBytes)

{

    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);

    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };

    var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));


    byte[] cipherTextBytes;


    using (var memoryStream = new MemoryStream())

    {

        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))

        {

            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            cryptoStream.FlushFinalBlock();

            cipherTextBytes = memoryStream.ToArray();

        }

    }


    return Convert.ToBase64String(cipherTextBytes);

}

解密


public static byte[] Decrypt(string base64)

{

    byte[] cipherTextBytes = Convert.FromBase64String(base64);

    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);

    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };


    var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

    using (var memoryStream = new MemoryStream(cipherTextBytes))

    {

        using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))

        {

            using (BinaryReader srDecrypt = new BinaryReader(cryptoStream))

            {

                return srDecrypt.ReadBytes(cipherTextBytes.Length);

            }

        }

    }

}


查看完整回答
反对 回复 2021-10-09
  • 1 回答
  • 0 关注
  • 196 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信