Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot deserialize model from encrypted string #2057

Open
3 of 6 tasks
Mazzelfassel opened this issue Feb 10, 2023 · 0 comments
Open
3 of 6 tasks

Cannot deserialize model from encrypted string #2057

Mazzelfassel opened this issue Feb 10, 2023 · 0 comments

Comments

@Mazzelfassel
Copy link
Contributor

Please check all of the platforms you are having the issue on (if platform is not listed, it is not supported)

  • WPF
  • UWP
  • iOS
  • Android
  • .NET Standard
  • .NET Core

Component

ISerializer / XMLSerializer

Version of Library

5.12.22

Version of OS(s) listed above with issue

Win 11

Steps to Reproduce

When directly deserializing a model in Catel through an ISerializer with encryption applied the process throws an exception.

Serialization Code:

public byte[] Export()
{
    ISerializer serializer = this.GetServiceLocator().ResolveType<ISerializer>();
    using Aes aes = Aes.Create();
    aes.Key = Key;
    aes.IV = IV;
    ICryptoTransform encryptor = aes.CreateEncryptor();

    using MemoryStream msEncrypt = new();
    using CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write);

    serializer.Serialize("Test123", csEncrypt);
    csEncrypt.FlushFinalBlock();
    return msEncrypt.ToArray();
}

Deserialization Code:

public void Import(byte[] s)
{
    ISerializer serializer = this.GetServiceLocator().ResolveType<ISerializer>();
    using Aes aes = Aes.Create();
    aes.Key = Key;
    aes.IV = IV;
    ICryptoTransform decryptor = aes.CreateDecryptor();

    using MemoryStream msDecrypt = new(s);
    using CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);

    string test = serializer.Deserialize<string>(csDecrypt);
}

Expected Behavior

The data gets decrypted and deserialized.

Actual Behavior

The serializer throws an Exception.

Stack Trace:

at Catel.Runtime.Serialization.Xml.XmlSerializer.BeforeDeserialization(ISerializationContext`1 context)
   at Catel.Runtime.Serialization.SerializerBase`1.Deserialize(Object model, ISerializationContext`1 context)
   at Catel.Runtime.Serialization.SerializerBase`1.Deserialize(Object model, TSerializationContextInfo serializationContext, ISerializationConfiguration configuration)
   at Catel.Runtime.Serialization.SerializerBase`1.Deserialize(Type modelType, Stream stream, ISerializationConfiguration configuration)
   at Catel.Runtime.Serialization.ISerializerExtensions.Deserialize[TModel](ISerializer serializer, Stream stream, ISerializationConfiguration configuration)

The only way it currently works is to do the extra step by first converting the data to a string and then passing the string to the deserializer. This may indicate that there is a problem with encoding but I found no option the specify encoding for the serializer.

Workaround approach

public void Import(byte[] s)
{
    ISerializer serializer = this.GetServiceLocator().ResolveType<ISerializer>();
    using Aes aes = Aes.Create();
    aes.Key = Key;
    aes.IV = IV;
    ICryptoTransform decryptor = aes.CreateDecryptor();

    using MemoryStream msDecrypt = new(s);
    using CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);

    using StreamReader reader = new(csDecrypt);
    string data = reader.ReadToEnd();

    using MemoryStream stringReader = new(Encoding.ASCII.GetBytes(data));
    string test = serializer.Deserialize<string>(stringReader);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant