Unity C# 加密错误

昨天它工作正常,但今天我不知道为什么,我收到此错误消息

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 小时,但我找不到解决此问题的方法。我希望任何人都可以帮助我。


MYYA
浏览 197回答 1
1回答

墨色风雨

在对您的代码进行一些测试后,我最终使用了这两种方法。我认为您的代码的主要问题是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);            }        }    }}
打开App,查看更多内容
随时随地看视频慕课网APP